Notebook Heater

Code

void setup() {
}
#include "U8glib.h"
int analogInput = 0;
int Percent ; //We want it as a whole number
float Voltageout = 0.0;
float Voltage = 0.0; //We want it with decimal places
float R1 = 97000.0; //resistance
float R2 = 10000.0; //resistance
int value = 0;
U8GLIB_SH1106_128X64 u8g(10, 9, 12, 11, 13); // pinout, see page
void draw(void)
{
u8g.setFont(u8g_font_helvB14r); // font
u8g.drawStr(19, 16, "BATTERY");
u8g.setPrintPos(7,55);
u8g.setFont(u8g_font_helvB18r);
u8g.println(Percent); //Prints Percent
u8g.println("%");
u8g.setPrintPos(63,55);
u8g.setFont(u8g_font_helvB18r); //A slightly larger font
u8g.println(Voltage); //Prints the voltage
u8g.println("V");
u8g.drawRFrame(0, 23, 128, 1, 0);
}
void setup(){
pinMode(analogInput, INPUT);
}
void loop(){
value = analogRead(analogInput);
Voltageout = (value * 5.0) / 1024.0;
Voltage = Voltageout / (R2/(R1+R2));
if (Voltage<1) {
Voltage=0.0; // get rid of unwanted readings
}
if (Voltage>5.00) { // if Reading are too large
Voltage= Voltage/10;
Percent = ((Voltage-3)/(4.2-3))*100; //votage divided by max cell votage times 100 = the cells current percentage
}
u8g.firstPage();
do
{
draw();
}
while( u8g.nextPage() );
delay(500);
}void loop() {
}#include <OneWire.h>
#include <DallasTemperature.h>
#include <avr/wdt.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int maxTemp=30;
int minTemp=26;
const int relayPin=9;
unsigned long interval=30*1000; //30sec recomended
unsigned long lastConnectionTime=0;
int Temp=25;
float tempPrev;
float temp=20;
void setup() {
Serial.begin(9600);
pinMode(relayPin,OUTPUT);
wdt_enable(WDTO_8S);
sensors.begin();
setPwmFrequency(relayPin, 1024); //division on 1024 for ~30Hz
pinMode(13, OUTPUT); //Blick LED
}
void loop() {
if(millis() - lastConnectionTime > interval) {
digitalWrite(13, HIGH);
tempPrev=temp;
sensors.requestTemperatures();
temp=sensors.getTempCByIndex(1);
digitalWrite(13, LOW);
if (temp == -127) {temp=tempPrev;}
int val = 255-255*(temp-minTemp)/(maxTemp-minTemp);
if (val<0) {val=0;}
if (val>204) {val=204;} //80% -this 230 or 450W; 204 for 400W
analogWrite(relayPin, val);
lastConnectionTime = millis();
Serial.print("Temp=");
Serial.print(temp);
int valperc = map(val, 0, 255, 0, 100);
Serial.print(" Power=");
Serial.print(valperc);
Serial.println("%");
}
delay(1);
wdt_reset();
}
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
}
}