Arduino thermostat with OLED display

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);
 } 
 
}

Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

I think it is ok now ?

float buttonState5 = 0;A button or switch is either on or off.
Why would you use 32 bits to represent this?
Why not an integer data type?

AWOL:

float buttonState5 = 0;

A button or switch is either on or off.
Why would you use 32 bits to represent this?
Why not an integer data type?

Ok,I changed it from float to int..
Code uploaud be completed without any problems.
But when I set the temperature to 25.00 and the current temperature is 24.80 the LED should light up(brightness = 51),but the LED does not light up..

if (buttonPushCounter - DHT.temperature == 0.2){ These are very exact values to be testing for equality with floats involved. What do you see if you print the value being tested and buttonPushCounter ?

UKHeliBob:
What do you see if you print the value being tested and buttonPushCounter ?

I do not understand what you mean,google translate does not translate well.
The reason why I do not know English is because I am from Serbia. :smiley:
Please explain to me a little better...

Please explain to me a little better...

Serial.print("buttonPushCounter = ");
Serial.println(buttonPushCounter);

Serial.print("DHT.temperature = ");
Serial.println(DHT.temperature);

What do you see?

PaulS:

Serial.print("buttonPushCounter = ");

Serial.println(buttonPushCounter);

Serial.print("DHT.temperature = ");
Serial.println(DHT.temperature);




What do you see?

I see "DHT.Temperature = 24.20 "
"buttonPushCounter = 0.00 "(When I press the button he goes 0.20,0.40...)

const float buttonPin = 2; // the pin that the Up pushbutton is attached to
const float buttonPin1 =4;

Why are you using floats for pin numbers? Can you really connect something to pin 3.14159?

float buttonPushCounter = 0;   // counter for the number of button presses

Can you really push a switch 3.14159 times?

PaulS:
Why are you using floats for pin numbers? Can you really connect something to pin 3.14159?

float buttonPushCounter = 0;   // counter for the number of button presses

Can you really push a switch 3.14159 times?

I change to int ,and when I push the switch it doesn't work it stays on 0.
I want when I push the switch to increase it for 0.2...

I change to int ,and when I push the switch it doesn't work it stays on 0.
I want when I push the switch to increase it for 0.2...

You do NOT want to increase the count of presses by 0.2. You might want to increase, or decrease, a setPoint by 0.2.

Make it CLEAR from the name of the variable that you know what you are doing.

Obviously, you can't increment an int by 0.2.

There is a time and a place for floating-point arithmetic. The time is never. You can figure out the place on your own.