Advertisement
elektronek

Daniel Brandejs - Časovač - ukázka sprintf

Jan 26th, 2021
1,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2.  
  3. // Tvuj LCD
  4. #include <LiquidCrystal_I2C.h>
  5. LiquidCrystal_I2C lcd(0x27,16,2);
  6.  
  7. // Muj LCD
  8. // #include <LiquidCrystal_PCF8574.h>
  9. // LiquidCrystal_PCF8574 lcd(0x27);
  10.  
  11. #define startbutt 2
  12. #define minusbutt 0   // Nedavej to na piny 0 a 1 - tam je USART
  13. #define plusbutt 1    // a uz si ni cnevypises na Serial pripadne
  14. #define piezo 3
  15. #define redC 13
  16. #define blueC 12
  17. #define greenC 11
  18.  
  19.  
  20. int Sec = 0;      // Sekundy staci
  21. int zobrazit=1;   // rezim zobrazeni
  22. int stisknutoP=0; // priznak stisku PLUS
  23. int stisknutoM=0; // priznak stisku MINUS
  24. char buffer[20];  // Pomocny buffer pro displej - sprintf format
  25.  
  26. void setup()
  27. {
  28.   pinMode(startbutt, INPUT_PULLUP);
  29.   pinMode(minusbutt, INPUT_PULLUP);
  30.   pinMode(plusbutt,  INPUT_PULLUP);
  31.  
  32.   // Init Tveho LCD
  33.   lcd.init();
  34.   lcd.backlight();
  35.  
  36.   // Init meho LCD
  37.   // lcd.begin(16,2);
  38.   // lcd.setBacklight(255);
  39.  
  40.   lcd.setCursor(0,0);
  41.   lcd.print("digital exposure");
  42.   delay(1);
  43.   lcd.setCursor(0,1);
  44.   lcd.print("unit         1.0");
  45.   delay(10);
  46.   analogWrite(redC, 255);
  47.   tone(piezo,350,800);
  48.   delay(4500);
  49. }
  50.  
  51. void zobraz()
  52. {
  53.   zobrazit=0;
  54.   sprintf(buffer,"Time       %02d:%02d",Sec/60, Sec%60);
  55.   lcd.setCursor(0,1);
  56.   lcd.print(buffer);
  57. }
  58.  
  59.  
  60. void loop()
  61. {
  62.   if (zobrazit) zobraz();
  63.    
  64.   if (digitalRead(plusbutt)==0 && stisknutoP==0)
  65.   {
  66.     stisknutoP=1;
  67.     Sec++;
  68.     zobrazit=1;
  69.     delay(20);
  70.   }
  71.   if (digitalRead(plusbutt)) { delay(20); stisknutoP=0;}
  72.  
  73.   if (digitalRead(minusbutt)==0 && Sec > 0 && stisknutoM==0)
  74.   {
  75.     stisknutoM=1;
  76.     Sec--;
  77.     zobrazit=1;
  78.     delay(20);
  79.   }
  80.   if (digitalRead(minusbutt)) {delay(20); stisknutoM=0;}
  81.  
  82.   // a sem si doplnis funkce dalsiho tlacitka nebo co je potreba a pokud je potreba aktualizovat vypis
  83.   // na displej tak staci nastavit promennou zobrazit=1; a je to :-)
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement