Hi guys !
I want to make the thermostat and when it comes close to the set temperature, for example 1.00, I I want that LED little shines..When it comes closer to the temperature LED must more shine...
Here is the code,I think you understand what I want...
#include "U8glib.h"
#include <Wire.h> // I2C
#include <Time.h> // Time Manipulation
#include <DS1307RTC.h> // DS1307 RTC
#include "dht.h"
dht DHT;
#define dht_apin A0
const float buttonPin = 2; // the pin that the Up pushbutton is attached to
const float buttonPin1 =4;
// Variables will change:
float ledPin = 5;
int brightness = 0; // how bright the LED is
float buttonPushCounter = 0; // counter for the number of button presses
float buttonState5 = 0;
float buttonState6 = 0;
float buttonState7 = 0;
float lastButtonState = 0;
char timebuf[10]; // Time
char datebuf[10]; // Date
int year2digit; // 2 digit year
int year4digit; // 4 digit year
U8GLIB_SH1106_128X64 u8g(12, 11, 10, 9, 8 ); // D0=12, D1=11, CS=10, DC=9, Reset=8
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT);
}
// put your main code here, to run repeatedly:
void draw6(void) {
u8g.setFont(u8g_font_courR10); // select font
u8g.setPrintPos(26, 14); // set position
u8g.print(timebuf); // display time
u8g.setPrintPos(26, 29); // set position
u8g.print(datebuf); // display date
u8g.setPrintPos(0,44);
u8g.print("Set temp:");
u8g.setPrintPos(80,44);
u8g.print(lastButtonState);
u8g.setPrintPos(0,59);
u8g.print ("Current:");
u8g.setPrintPos(80,59);
u8g.print (DHT.temperature) ;
};
void loop(void){
delay(250);
DHT.read22(dht_apin);
tmElements_t tm;
if (RTC.read(tm)) {
year2digit = tm.Year - 30; // 2 digit year variable
// year4digit = tm.Year + 1970; // 4 digit year variable
sprintf(timebuf, "%02d:%02d:%02d",tm.Hour, tm.Minute, tm.Second); // format time
sprintf(datebuf, "%02d/%02d/%02d",tm.Day, tm.Month, year2digit); // format date
u8g.firstPage(); // Put information on OLED
do {
draw6();
} while( u8g.nextPage() );
}
buttonState5 = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState5 != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState5 == LOW)
{
buttonPushCounter+=0.2;
u8g.firstPage();
do {
draw6();
} while( u8g.nextPage() );
}
}
// save the current state as the last state
lastButtonState = buttonState5;
//for next time through the loop
// read the pushbutton down input pin:
buttonState6 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState6 != lastButtonState) {
// if the state has changed, decrement the counter
if (buttonState6 == LOW)
{
buttonPushCounter-=0.2;
u8g.firstPage();
do {
draw6();
} while( u8g.nextPage() );
}
// save the current state as the last stat
}
lastButtonState = buttonState6;
if (buttonPushCounter - DHT.temperature == 0.2){
brightness = 51;
analogWrite(ledPin, brightness);
}
if (buttonPushCounter - DHT.temperature == 0.4){
brightness = 102;
analogWrite(ledPin, brightness);
}
if (buttonPushCounter - DHT.temperature == 0.6){
brightness = 153;
analogWrite(ledPin, brightness);
}
if (buttonPushCounter - DHT.temperature == 0.8){
brightness = 204;
analogWrite(ledPin, brightness);
}
if (buttonPushCounter - DHT.temperature == 1){
brightness = 255;
analogWrite(ledPin, brightness);
}
}