luni, 18 mai 2015

Termostat cu 2 praguri de temperatura pe afisaj LED

   Pornind de la  solicitare primita pe blog, in care se dorea un termostat care sa folosesca senzorul de temperatura LM35, la care sa se poata regla temperatura minima si cea maxima, afisarea sa se faca multiplexat pe un afisaj LED cu 4 cifre din 7 segmente fiecare, sa indice starea cu LED-uri, iar schimbarea valorilor de temperatura sa se faca din 3 butoane, unul de selectie, iar celelalte 2 pentru scadere, respectiv crestere valoare, m-am apucat de studiat.. pentru a nu reinventa roata, am folosit ca baza materialele prezentate in articolele Afisaje LED cu 7 segmente si.. Arduino, respectiv Afisaje LED cu 7 segmente si.. Arduino (II)
   Am folosit acelasi afisaj cu anod comun tip KW4-563ASA (0.56" = 14mm), conectandu-l la fel ca atunci, dar completand cu senzorul de temperatura LM35 (mai precis modelul LM35DZ), cele 3 butoane fara retinere si un led multicolor (RGB), la care am conectat doar pe cel rosu si pe cel albatru, deoarece nu mai am pini, daca as lucra cu un Arduino Uno (eu am folosit un Arduino Mega, care imi permite sa pun la A6 si ledul verde).
   Schema (care a avut o eroare, dar a fost corectata), care functioneaza atat pe Arduino Uno, cat si pe Mega (sau Nano, Micro, etc) este:
   Cateva poze cu montajul:
   Am facut primele teste, dupa cum se vede si in filmuletul termostat cu 2 praguri de temperatura:
   Deoarece senzorul de temperatura LM35 este analog, la alimentarea din usb sau sursa de tensiune subdimensionata, tensiunea de 5V scade spre 4V si apar erori de masurare:
   Am schimbat sketch-ul sa folosesc tensiunea de referinta de 1,1V (in sketch se alege varianta de placa Arduino, adica tip Uno sau Mega) si am facut si un alt filmulet numit termostat cu 2 praguri de temperatura (2)
   Sketch-ul variantei prezentate este:
// http://nicuflorica.blogspot.ro/2013/07/afisaje-led-cu-7-segmente-si-arduino-ii.html
/*
 6-13-2011
 Spark Fun Electronics 2011
 Nathan Seidle

 This code is public domain but you buy me a beer if you use this and we meet 
 someday (Beerware license).

 4 digit 7 segment display:
 http://www.sparkfun.com/products/9483
 Datasheet: 
 http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf

 This is an example of how to drive a 7 segment LED display from an ATmega
 without the use of current limiting resistors. This technique is very common 
 but requires some knowledge of electronics - you do run the risk of dumping 
 too much current through the segments and burning out parts of the display. 
 If you use the stock code you should be ok, but be careful editing the 
 brightness values.

 This code should work with all colors (red, blue, yellow, green) but the 
 brightness will vary from one color to the next because the forward voltage 
 drop of each color is different. This code was written and calibrated for the 
 red color.

 This code will work with most Arduinos but you may want to re-route some of 
 the pins.

 7 segments
 4 digits
 1 colon
 =
 12 pins required for full control 

 */
// modified connexion by niq_ro from http://nicuflorica.blogspot.com
// for my Luckylight KW4-563ASA
// dataseet: http://www.tme.eu/ro/Document/dfc2efde2e22005fd28615e298ea2655/KW4-563XSA.pdf

byte digit1 = 11; //PWM Display pin 12 (digit1 is common anonds A1 from right side)
byte digit2 = 10; //PWM Display pin 9 (digit2 is  common A2)
byte digit3 = 9; //PWM Display pin 8 (digit3 is common anods A3)
byte digit4 = 6; //PWM Display pin 6 (digit4 is common anods, from left side)

//Pin mapping from Arduino to the ATmega DIP28 if you need it
// http://www.arduino.cc/en/Hacking/PinMapping
byte segA = 2; //Display pin 11
byte segB = 3; //Display pin 7
byte segC = 4; //Display pin 4
byte segD = 5; //Display pin 2
byte segE = 12; //Display pin 1
byte segF = 7; //Display pin 10
byte segG = 8; //Display pin 5

byte dP = 13; // decimal point

#define LMPIN A0     // what pin we're LM35 is connected

#define BUT1 A1    // - switch
#define BUT2 A2    // + switch
#define BUT3 A3    // MENU switch

#define RECE A4    // output for lower temperature
#define CALD A5    // output for upper temperature
#define BINE A6    // output for ok temperature

int tmin = 240;  // minimum temperature (x10)
int tmax = 280;  // maximum temperatura (x10)

byte meniu = 0; // if MENIU = 0 is clasical 
                // if MENIU = 1 is for low temperature
                // if MENIU = 2 is for upper temperature 
float t, t0;
 

void setup() {

  // analogReference(INTERNAL); // for Uno (1,1V)
  analogReference(INTERNAL1V1); // for Mega (1,1V)
  
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
  
  pinMode(dP, OUTPUT);
  
  pinMode(BUT1, INPUT);
  pinMode(BUT2, INPUT);
  pinMode(BUT3, INPUT);

  pinMode(RECE, OUTPUT);
  pinMode(CALD, OUTPUT);
  pinMode(BINE, OUTPUT);

  digitalWrite(BUT1, HIGH); // pull-ups on
  digitalWrite(BUT2, HIGH);
  digitalWrite(BUT3, HIGH);

  digitalWrite(RECE, LOW);
  digitalWrite(CALD, LOW);
  digitalWrite(BINE, LOW);
  
 Serial.begin(9600);
 Serial.println("test for niq_ro");

}

void loop() {

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

  Serial.print("tmin = ");
  Serial.println(tmin);
  Serial.print("tmax = ");
  Serial.println(tmax);

if (digitalRead(BUT3) == LOW) 
{ meniu = meniu + 1;
delay(250);
}
if (meniu == 3) meniu = 0;

if (millis() < 1000) citire();
//  t = analogRead(LMPIN);
//  t0 = t *500 / 1023;
leduri();

  Serial.print(t);
  Serial.print("/1023 --> ");
  Serial.print("t = ");
  Serial.println(t0);



//displayNumber(t0*10);
    
if (meniu == 0) {
int numara = 0;
  while (meniu == 0) {
numara = numara + 1;
  if (numara == 1000)
    {
//  float t = analogRead(LMPIN);
//  float t0 = t *500 / 1023;
citire();
leduri();
 // delay(5);
  numara = 0;
  Serial.print("t = ");
  Serial.println(t0);
//  displayNumber(t0*10); // this is number to diplay
    }
  displayNumber(t0*10); // this is number to diplay
   
if (digitalRead(BUT3) == LOW) 
{ meniu = 1;
delay(250);
}
//Serial.print("t = ");
//Serial.println(t0);
//delay(5);delay(5);  
}



if (meniu == 1) {
   while (meniu == 1) {
     minim(tmin/10); // this is number to diplay
   if (digitalRead(BUT1) == LOW)
  { tmin = tmin - 10; 
  delay(250);
  }
   if (digitalRead(BUT2) == LOW)
  { tmin = tmin + 10;
  delay(250);
  }
  if (digitalRead(BUT3) == LOW) 
  { meniu = 2;
  delay(250);
  }
//  delay(15);
     }
  Serial.print("tmin = ");
  Serial.println(tmin/10);
   delay (100);
}

if (meniu == 2) {
  if (tmax <= tmin) tmax = tmin + 10;
   while (meniu ==2) {
     maxim(tmax/10); // this is number to diplay
   if (digitalRead(BUT1) == LOW) 
   { tmax = tmax - 10;
   delay(250);
  }
   if (digitalRead(BUT2) == LOW) 
   { tmax = tmax + 10;
   delay(250);
  }
   if (digitalRead(BUT3) == LOW) 
   { meniu = 0;
   delay(250);
  }
//   delay(15);
if (tmax < tmin) tmax = tmin + 10;
    }
  Serial.print("tmax = ");
  Serial.println(tmax/10);

    delay(100);
}
}
//Given a number, we display 10:22
//After running through the 4 numbers, the display is left turned off

//Display brightness
//Each digit is on for a certain amount of microseconds
//Then it is off until we have reached a total of 20ms for the function call
//Let's assume each digit is on for 1000us
//Each digit is on for 1ms, there are 4 digits, so the display is off for 16ms.
//That's a ratio of 1ms to 16ms or 6.25% on time (PWM).
//Let's define a variable called brightness that varies from:
//5000 blindingly bright (15.7mA current draw per digit)
//2000 shockingly bright (11.4mA current draw per digit)
//1000 pretty bright (5.9mA)
//500 normal (3mA)
//200 dim but readable (1.4mA)
//50 dim but readable (0.56mA)
//5 dim but readable (0.31mA)
//1 dim but readable in dark (0.28mA)


void minim(int toDisplay) {
#define DISPLAY_BRIGHTNESS  750

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
   case 1:
      digitalWrite(digit1, DIGIT_ON);
      lightNumber(14); // display degree symbol
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 3:
      digitalWrite(digit3, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
    }
     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
  }
} 

void maxim(int toDisplay) {
#define DISPLAY_BRIGHTNESS  750

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      lightNumber(15); // display degree symbol
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;

    case 2:
      digitalWrite(digit2, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 3:
      digitalWrite(digit3, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
    }
     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
}
} 

void displayNumber(int toDisplay) {
#define DISPLAY_BRIGHTNESS  500

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW
 for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      digitalWrite(13, HIGH);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      digitalWrite(13, LOW);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      digitalWrite(13, HIGH);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      digitalWrite(13, HIGH);
      break;
    }
 //   lightNumber(toDisplay % 10);
 //   toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 

     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);

}
} 


//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

#define SEGMENT_ON  LOW
#define SEGMENT_OFF HIGH

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  // all segment are ON
  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;
  
  // degree symbol made by niq_ro
  case 11:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  // C letter made by niq_ro
  case 12:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;
  
  // H letter made by niq_ro
  case 13:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 14:  // lower line (d segment)
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

case 15:  // upper line (a segment)
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;
    
  }
}

float citire() {
  t = analogRead(A0);
  t0 = t *110 / 1023;  // pentru referinta interna de 1,1V;
//  t0 = t *500 / 1023;
  return t0;
}

void leduri() {
 digitalWrite(CALD, LOW);
 digitalWrite(RECE, LOW);
 digitalWrite(BINE, LOW);

if ( t0*10 < tmin ) 
{  digitalWrite(RECE, HIGH);
   digitalWrite(CALD, LOW);
   digitalWrite(BINE, LOW);
}
if ( t0*10 > tmax ) 
{  digitalWrite(CALD, HIGH);
   digitalWrite(RECE, LOW);
   digitalWrite(BINE, LOW);
}
if (( t0*10 <= tmax ) && ( t0*10 >= tmin ))
{  digitalWrite(CALD, LOW);
   digitalWrite(RECE, LOW);
   digitalWrite(BINE, HIGH);
}

}
   Ulterior, am modificat sa memorez cele 2 praguri de temperaturi in memoria EEPROM a microcontrolerului, pentru a evita reintroducerea datelor daca se intrerupe alimentarea, dupa cum am prezentat in filmuletul termostat cu 2 praguri de temperatura (3)
   Sketch-ul pentru aceasta varianta este:
// http://nicuflorica.blogspot.ro/2013/07/afisaje-led-cu-7-segmente-si-arduino-ii.html
/*
 6-13-2011
 Spark Fun Electronics 2011
 Nathan Seidle

 This code is public domain but you buy me a beer if you use this and we meet 
 someday (Beerware license).

 4 digit 7 segment display:
 http://www.sparkfun.com/products/9483
 Datasheet: 
 http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf

 This is an example of how to drive a 7 segment LED display from an ATmega
 without the use of current limiting resistors. This technique is very common 
 but requires some knowledge of electronics - you do run the risk of dumping 
 too much current through the segments and burning out parts of the display. 
 If you use the stock code you should be ok, but be careful editing the 
 brightness values.

 This code should work with all colors (red, blue, yellow, green) but the 
 brightness will vary from one color to the next because the forward voltage 
 drop of each color is different. This code was written and calibrated for the 
 red color.

 This code will work with most Arduinos but you may want to re-route some of 
 the pins.

 7 segments
 4 digits
 1 colon
 =
 12 pins required for full control 

 */
// modified connexion by niq_ro from http://nicuflorica.blogspot.com & http://arduinotehniq.blogspot.com
// for my Luckylight KW4-563ASA
// dataseet: http://www.tme.eu/ro/Document/dfc2efde2e22005fd28615e298ea2655/KW4-563XSA.pdf

byte digit1 = 11; //PWM Display pin 12 (digit1 is common anonds A1 from right side)
byte digit2 = 10; //PWM Display pin 9 (digit2 is  common A2)
byte digit3 = 9; //PWM Display pin 8 (digit3 is common anods A3)
byte digit4 = 6; //PWM Display pin 6 (digit4 is common anods, from left side)

//Pin mapping from Arduino to the ATmega DIP28 if you need it
// http://www.arduino.cc/en/Hacking/PinMapping
byte segA = 2; //Display pin 11
byte segB = 3; //Display pin 7
byte segC = 4; //Display pin 4
byte segD = 5; //Display pin 2
byte segE = 12; //Display pin 1
byte segF = 7; //Display pin 10
byte segG = 8; //Display pin 5

byte dP = 13; // decimal point

#define LMPIN A0     // what pin we're LM35 is connected

#define BUT1 A1    // - switch
#define BUT2 A2    // + switch
#define BUT3 A3    // MENU switch

#define RECE A4    // output for lower temperature
#define CALD A5    // output for upper temperature
#define BINE A6    // output for ok temperature (option - MEGA)

//int tmin = 240;  // minimum temperature (x10)
//int tmax = 280;  // maximum temperatura (x10)

int tmin, tmax;

byte meniu = 0; // if MENIU = 0 is clasical 
                // if MENIU = 1 is for low temperature
                // if MENIU = 2 is for upper temperature 
float t, t0;
 
// http://tronixstuff.com/2011/03/16/tutorial-your-arduinos-inbuilt-eeprom/
#include <EEPROM.h>

void setup() {

  // analogReference(INTERNAL); // for Uno (1,1V)
  analogReference(INTERNAL1V1); // for Mega (1,1V)

// just first time... after that you must put commnent (//)
// EEPROM.write(69,25);    
// EEPROM.write(70,28);    

tmin = 10*EEPROM.read(69);
tmax = 10*EEPROM.read(70);

  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
  
  pinMode(dP, OUTPUT);
  
  pinMode(BUT1, INPUT);
  pinMode(BUT2, INPUT);
  pinMode(BUT3, INPUT);

  pinMode(RECE, OUTPUT);
  pinMode(CALD, OUTPUT);
  pinMode(BINE, OUTPUT);

  digitalWrite(BUT1, HIGH); // pull-ups on
  digitalWrite(BUT2, HIGH);
  digitalWrite(BUT3, HIGH);

  digitalWrite(RECE, LOW);
  digitalWrite(CALD, LOW);
  digitalWrite(BINE, LOW);
  
 Serial.begin(9600);
 Serial.println("test for niq_ro");

}

void loop() {

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

  Serial.print("tmin = ");
  Serial.println(tmin);
  Serial.print("tmax = ");
  Serial.println(tmax);

if (digitalRead(BUT3) == LOW) 
{ meniu = meniu + 1;
delay(250);
}
if (meniu == 3) meniu = 0;

if (millis() < 1000) citire();
//citire();
//  t = analogRead(LMPIN);
//  t0 = t *500 / 1023;
leduri();

  Serial.print(t);
  Serial.print("/1023 --> ");
  Serial.print("t = ");
  Serial.println(t0);



//displayNumber(t0*10);
    
if (meniu == 0) {
int numara = 0;
  while (meniu == 0) {
numara = numara + 1;
  if (numara == 1000)
    {
//  float t = analogRead(LMPIN);
//  float t0 = t *500 / 1023;
citire();
leduri();
 // delay(5);
  numara = 0;
  Serial.print("t = ");
  Serial.println(t0);
//  displayNumber(t0*10); // this is number to diplay
    }
  displayNumber(t0*10); // this is number to diplay
   
if (digitalRead(BUT3) == LOW) 
{ meniu = 1;
delay(250);
}
//Serial.print("t = ");
//Serial.println(t0);
//delay(5);delay(5);  
}



if (meniu == 1) {
   while (meniu == 1) {
     minim(tmin/10); // this is number to diplay
   if (digitalRead(BUT1) == LOW)
  { tmin = tmin - 10; 
  delay(250);
  }
   if (digitalRead(BUT2) == LOW)
  { tmin = tmin + 10;
  delay(250);
  }
  if (digitalRead(BUT3) == LOW) 
  { meniu = 2;
  delay(250);
  }
//  delay(15);
     }
  EEPROM.write(69,tmin/10);   
  Serial.print("tmin = ");
  Serial.println(tmin/10);
   delay (100);
}

if (meniu == 2) {
  if (tmax <= tmin) tmax = tmin;
   while (meniu ==2) {
     maxim(tmax/10); // this is number to diplay
   if (digitalRead(BUT1) == LOW) 
   { tmax = tmax - 10;
   delay(250);
  }
   if (digitalRead(BUT2) == LOW) 
   { tmax = tmax + 10;
   delay(250);
  }
   if (digitalRead(BUT3) == LOW) 
   { meniu = 0;
   delay(250);
  }
//   delay(15);
if (tmax < tmin) tmax = tmin + 10;
    }
  EEPROM.write(70,tmax/10);    
  Serial.print("tmax = ");
  Serial.println(tmax/10);

    delay(100);
}
}
//Given a number, we display 10:22
//After running through the 4 numbers, the display is left turned off

//Display brightness
//Each digit is on for a certain amount of microseconds
//Then it is off until we have reached a total of 20ms for the function call
//Let's assume each digit is on for 1000us
//Each digit is on for 1ms, there are 4 digits, so the display is off for 16ms.
//That's a ratio of 1ms to 16ms or 6.25% on time (PWM).
//Let's define a variable called brightness that varies from:
//5000 blindingly bright (15.7mA current draw per digit)
//2000 shockingly bright (11.4mA current draw per digit)
//1000 pretty bright (5.9mA)
//500 normal (3mA)
//200 dim but readable (1.4mA)
//50 dim but readable (0.56mA)
//5 dim but readable (0.31mA)
//1 dim but readable in dark (0.28mA)


void minim(int toDisplay) {
#define DISPLAY_BRIGHTNESS  750

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
   case 1:
      digitalWrite(digit1, DIGIT_ON);
      lightNumber(14); // display degree symbol
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 3:
      digitalWrite(digit3, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
    }
     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
  }
} 

void maxim(int toDisplay) {
#define DISPLAY_BRIGHTNESS  750

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      lightNumber(15); // display degree symbol
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;

    case 2:
      digitalWrite(digit2, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 3:
      digitalWrite(digit3, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
    }
     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
}
} 

void displayNumber(int toDisplay) {
#define DISPLAY_BRIGHTNESS  500

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW
 for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      digitalWrite(13, HIGH);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      digitalWrite(13, LOW);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      digitalWrite(13, HIGH);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      digitalWrite(13, HIGH);
      break;
    }
 //   lightNumber(toDisplay % 10);
 //   toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 

     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);

}
} 


//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

#define SEGMENT_ON  LOW
#define SEGMENT_OFF HIGH

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  // all segment are ON
  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;
  
  // degree symbol made by niq_ro
  case 11:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  // C letter made by niq_ro
  case 12:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;
  
  // H letter made by niq_ro
  case 13:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 14:  // lower line (d segment)
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

case 15:  // upper line (a segment)
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;
    
  }
}

float citire() {
 // delay(250);
  t = analogRead(A0);
  t0 = t *110 / 1023;  // pentru referinta interna de 1,1V;
//  t0 = t *500 / 1023;
  return t0;
}

void leduri() {
 digitalWrite(CALD, LOW);
 digitalWrite(RECE, LOW);
 digitalWrite(BINE, LOW);

if ( t0*10 < tmin ) 
{  digitalWrite(RECE, HIGH);
   digitalWrite(CALD, LOW);
   digitalWrite(BINE, LOW);
}
if ( t0*10 > tmax ) 
{  digitalWrite(CALD, HIGH);
   digitalWrite(RECE, LOW);
   digitalWrite(BINE, LOW);
}
}
  Sketch-urile prezentante nu sunt unele optime din punct de vedere al programarii, dar sunt functionale... asa ca astept pareri si realizari practice de la voi..

PS: Am facut un alt filmulet, termostat cu 2 praguri de temperatura (4), de calitate mai buna:
PS2: Am scos placa Arduino Mega si am pus modelul Arduino Uno:
si am facut modificarea in sketch-ul cu EEPROM:
   Mai trebuie mentionat ca pentru a fi siguri ca in memoria EEPROM a microcontrolerului se incepe cu valori corecte, la prima incarcare a sketch-ului vom avea:
apoi se "comenteaza" cele 2 linii:
   Am facut un nou filmulet, numit termostat cu 2 praguri de temperatura (5)
19.05.2015
   Dupa ce am mai analizat sketch-urile si afisarea, am realizat ca citirea dintre multiplexari este eroana, fata de cea initiala, asa ca am introdus un factor de corectie si am facut media intre citirea actuala si cea anterioara, asa ca acum sketch-ul este:
// version sketch 1.4.f.1 - 19.05.2015, Craiova, Romania
// http://nicuflorica.blogspot.ro/2013/07/afisaje-led-cu-7-segmente-si-arduino-ii.html
// http://nicuflorica.blogspot.ro/2015/05/termostat-cu-2-praguri-de-temperatura.html
/*
base sketch: 6-13-2011, Spark Fun Electronics 2011, Nathan Seidle

 This code is public domain but you buy me a beer if you use this and we meet 
 someday (Beerware license).

 4 digit 7 segment display:
 http://www.sparkfun.com/products/9483
 Datasheet: 
 http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf

 This is an example of how to drive a 7 segment LED display from an ATmega
 without the use of current limiting resistors. This technique is very common 
 but requires some knowledge of electronics - you do run the risk of dumping 
 too much current through the segments and burning out parts of the display. 
 If you use the stock code you should be ok, but be careful editing the 
 brightness values.

 This code should work with all colors (red, blue, yellow, green) but the 
 brightness will vary from one color to the next because the forward voltage 
 drop of each color is different. This code was written and calibrated for the 
 red color.

 This code will work with most Arduinos but you may want to re-route some of 
 the pins.

 7 segments
 4 digits
 1 colon
 =
 12 pins required for full control 

 */
// modified connexion by niq_ro from http://nicuflorica.blogspot.com
// & http://arduinotehniq.blogspot.com/ 
// for my Luckylight KW4-563ASA
// dataseet: http://www.tme.eu/ro/Document/dfc2efde2e22005fd28615e298ea2655/KW4-563XSA.pdf

byte digit1 = 11; //PWM Display pin 12 (digit1 is common anonds A1 from right side)
byte digit2 = 10; //PWM Display pin 9 (digit2 is  common A2)
byte digit3 = 9; //PWM Display pin 8 (digit3 is common anods A3)
byte digit4 = 6; //PWM Display pin 6 (digit4 is common anods, from left side)

//Pin mapping from Arduino to the ATmega DIP28 if you need it
// http://www.arduino.cc/en/Hacking/PinMapping
byte segA = 2; //Display pin 11
byte segB = 3; //Display pin 7
byte segC = 4; //Display pin 4
byte segD = 5; //Display pin 2
byte segE = 12; //Display pin 1
byte segF = 7; //Display pin 10
byte segG = 8; //Display pin 5

byte dP = 13; // decimal point

#define LMPIN A0     // what pin we're LM35 is connected

#define BUT1 A1    // - switch
#define BUT2 A2    // + switch
#define BUT3 A3    // MENU switch

#define RECE A4    // output for lower temperature
#define CALD A5    // output for upper temperature
//#define BINE A6    // output for ok temperature (option - MEGA)

//int tmin = 240;  // minimum temperature (x10)
//int tmax = 280;  // maximum temperatura (x10)

int tmin, tmax;

byte meniu = 0; // if MENIU = 0 is clasical 
                // if MENIU = 1 is for low temperature
                // if MENIU = 2 is for upper temperature 
float t, t0, t2, k;

// http://tronixstuff.com/2011/03/16/tutorial-your-arduinos-inbuilt-eeprom/
#include <EEPROM.h>

void setup() {

   analogReference(INTERNAL); // for Uno (1,1V)
 // analogReference(INTERNAL1V1); // for Mega (1,1V)

// just first time... after must put commnent (//)
// EEPROM.write(69,25);    
// EEPROM.write(70,28);    
  
tmin = 10*EEPROM.read(69);
tmax = 10*EEPROM.read(70);

  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
  
  pinMode(dP, OUTPUT);
  
  pinMode(BUT1, INPUT);
  pinMode(BUT2, INPUT);
  pinMode(BUT3, INPUT);

  pinMode(RECE, OUTPUT);
  pinMode(CALD, OUTPUT);
//  pinMode(BINE, OUTPUT);

  digitalWrite(BUT1, HIGH); // pull-ups on
  digitalWrite(BUT2, HIGH);
  digitalWrite(BUT3, HIGH);

  digitalWrite(RECE, LOW);
  digitalWrite(CALD, LOW);
//  digitalWrite(BINE, LOW);
  
 Serial.begin(9600);
 Serial.println("test for niq_ro");

k = 0.891;  // factor corectie citire la multiplexare
delay(100);
t = analogRead(A0);
t0 = t * 110 / 1023;  // pentru referinta interna de 1,1V;
t2 = t0;
}

void loop() {

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

  Serial.print("tmin = ");
  Serial.println(tmin);
  Serial.print("tmax = ");
  Serial.println(tmax);

if (digitalRead(BUT3) == LOW) 
{ meniu = meniu + 1;
delay(250);
}
if (meniu == 3) meniu = 0;

if (millis() < 2000) citire();
//citire();
//  t = analogRead(LMPIN);
//  t0 = t *500 / 1023;
leduri();

  Serial.print(t);
  Serial.print("/1023 --> ");
  Serial.print("t = ");
  Serial.println(t0);



//displayNumber(t0*10);
    
if (meniu == 0) {
int numara = 0;
  while (meniu == 0) {
numara = numara + 1;
  if (numara == 1000)
    {
//  float t = analogRead(LMPIN);
//  float t0 = t *500 / 1023;
citire();
t0 = (t0 + t2) /2;
leduri();
 // delay(5);
  numara = 0;
  Serial.print("t = ");
  Serial.println(t0);
//  displayNumber(t0*10); // this is number to diplay
    }
  displayNumber(t0*10); // this is number to diplay
   t2 = t0;
if (digitalRead(BUT3) == LOW) 
{ meniu = 1;
delay(250);
}
//Serial.print("t = ");
//Serial.println(t0);
//delay(5);delay(5);  
}



if (meniu == 1) {
   while (meniu == 1) {
     minim(tmin/10); // this is number to diplay
   if (digitalRead(BUT1) == LOW)
  { tmin = tmin - 10; 
  delay(250);
  }
   if (digitalRead(BUT2) == LOW)
  { tmin = tmin + 10;
  delay(250);
  }
  if (digitalRead(BUT3) == LOW) 
  { meniu = 2;
  delay(250);
  }
//  delay(15);
     }
  EEPROM.write(69,tmin/10);   
  Serial.print("tmin = ");
  Serial.println(tmin/10);
   delay (100);
}

if (meniu == 2) {
  if (tmax <= tmin) tmax = tmin + 10;
   while (meniu ==2) {
     maxim(tmax/10); // this is number to diplay
   if (digitalRead(BUT1) == LOW) 
   { tmax = tmax - 10;
   delay(250);
  }
   if (digitalRead(BUT2) == LOW) 
   { tmax = tmax + 10;
   delay(250);
  }
   if (digitalRead(BUT3) == LOW) 
   { meniu = 0;
   delay(250);
  }
//   delay(15);
if (tmax < tmin) tmax = tmin + 10;
    }
  EEPROM.write(70,tmax/10);    
  Serial.print("tmax = ");
  Serial.println(tmax/10);

    delay(100);
}
}
//Given a number, we display 10:22
//After running through the 4 numbers, the display is left turned off

//Display brightness
//Each digit is on for a certain amount of microseconds
//Then it is off until we have reached a total of 20ms for the function call
//Let's assume each digit is on for 1000us
//Each digit is on for 1ms, there are 4 digits, so the display is off for 16ms.
//That's a ratio of 1ms to 16ms or 6.25% on time (PWM).
//Let's define a variable called brightness that varies from:
//5000 blindingly bright (15.7mA current draw per digit)
//2000 shockingly bright (11.4mA current draw per digit)
//1000 pretty bright (5.9mA)
//500 normal (3mA)
//200 dim but readable (1.4mA)
//50 dim but readable (0.56mA)
//5 dim but readable (0.31mA)
//1 dim but readable in dark (0.28mA)


void minim(int toDisplay) {
#define DISPLAY_BRIGHTNESS  750

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
   case 1:
      digitalWrite(digit1, DIGIT_ON);
      lightNumber(14); // display degree symbol
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 3:
      digitalWrite(digit3, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
    }
     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
  }
} 

void maxim(int toDisplay) {
#define DISPLAY_BRIGHTNESS  750

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      lightNumber(15); // display degree symbol
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;

    case 2:
      digitalWrite(digit2, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 3:
      digitalWrite(digit3, DIGIT_ON);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
   case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      delayMicroseconds(DISPLAY_BRIGHTNESS); 
      break;
    }
     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
}
} 

void displayNumber(int toDisplay) {
#define DISPLAY_BRIGHTNESS  500

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW
 for(int digit = 4 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      digitalWrite(13, HIGH);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      digitalWrite(13, LOW);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      digitalWrite(13, HIGH);
      lightNumber(toDisplay % 10);
      toDisplay /= 10;
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      lightNumber(12); // display C letter
      digitalWrite(13, HIGH);
      break;
    }
 //   lightNumber(toDisplay % 10);
 //   toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 

     //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);

}
} 


//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

#define SEGMENT_ON  LOW
#define SEGMENT_OFF HIGH

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  // all segment are ON
  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;
  
  // degree symbol made by niq_ro
  case 11:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  // C letter made by niq_ro
  case 12:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;
  
  // H letter made by niq_ro
  case 13:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    digitalWrite(13, SEGMENT_OFF);
    break;

  case 14:  // lower line (d segment)
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;

case 15:  // upper line (a segment)
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    digitalWrite(13, SEGMENT_OFF);
    break;  
  }
}


void leduri() {
 digitalWrite(CALD, LOW);
 digitalWrite(RECE, LOW);
// digitalWrite(BINE, LOW);

if ( t0*10 < tmin ) 
{  digitalWrite(RECE, HIGH);
   digitalWrite(CALD, LOW);
//   digitalWrite(BINE, LOW);
}
if ( t0*10 > tmax ) 
{  digitalWrite(CALD, HIGH);
   digitalWrite(RECE, LOW);
//   digitalWrite(BINE, LOW);
}
}

float citire() {
 // delay(250);
  t = analogRead(A0);
  t0 = t * k* 110 / 1023;  // pentru referinta interna de 1,1V;
  return t0;
}
   Am facut inca in filmulet, numit termostat cu 2 praguri de temperatura (6) in care am descris ce am scris mai sus:

7 comentarii:

  1. nu ar trebui sa avem rezistente conectate la fiecare LED?

    RăspundețiȘtergere
    Răspunsuri
    1. ba da, electronica car eo stiu eu impune asa ceva, mai ales ca am lucrat foarte mult cu integrate CMOS (MMC-uri), dar multiplexarea asta cu folosirea PWM-ului merge f. bine... vedeti link in sketch de unde am sursa...

      Ștergere
  2. M-ar interesa cum se poate inlocui afisajul din montajul : Termostat cu 2 praguri de temperatura pe afisaj LED cu un Lcd 1602 prevazut cu I2c precum si cum se schimba sketch-ul.
    Ar fi foarte util daca a-ti putea face un tutorial despre cum se schimba afisajele la diferite montaje si unde si cum se intervine in sketch-uri.
    Multumesc anticipat.

    RăspundețiȘtergere
  3. in mare schimbarea e simpla, se schimba comenzile de la afisajul asta cu unele pentru celalalt afisaj... stau foarte prost cu timpul liber si postez mult mai rar si putin... se adapteaza usor termostatele de la http://nicuflorica.blogspot.ro/2015/08/termostat-cu-control-umiditate-2.html si http://nicuflorica.blogspot.ro/2015/08/termostat-cu-control-umiditate.html
    incearca sa adaptezi pe ala la afisajul cu i2c, ca nu e mare scofala... e vorba doar de definirea intiala...

    RăspundețiȘtergere
  4. Salut ledul de pe a4 sti aprins iar celde pe a5 nu se aprinde indiferent ce setare fac.am schimbat si atmea 328 si la fel

    RăspundețiȘtergere
    Răspunsuri
    1. el "crede" ca temperatura e mai scazuta decat cea setata...
      #define RECE A4 // output for lower temperature
      #define CALD A5 // output for upper temperature
      si
      if ( t0*10 < tmin )
      { digitalWrite(RECE, HIGH);
      digitalWrite(CALD, LOW);
      // digitalWrite(BINE, LOW);
      }
      if ( t0*10 > tmax )
      { digitalWrite(CALD, HIGH);
      digitalWrite(RECE, LOW);
      // digitalWrite(BINE, LOW);
      }

      dar citeste temperatura? in setari ati reglat pragul?
      nu va pot ajuta daca nu dati informatii concrete, mai ales ca e un experiment de acum 3 ani jumate....

      Ștergere
  5. am uitat sa mentionez ca folosesc arduino genuino uno

    RăspundețiȘtergere