jrullan

Reynold Stop-Watch

Oct 26th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Canvas_SEEEDTOUCH.h>
  2. #include <Display.h>
  3. #include <Button.h>
  4. // Esta librería es un helper para repetir algo cada cierto tiempo (non-blocking)
  5. // La consigues en el Libraries Manager de Arduino
  6. #include <neotimer.h>
  7.  
  8. Canvas_SEEEDTOUCH canvas = Canvas_SEEEDTOUCH(TFT_LANDSCAPE,WHITE);
  9. Display stopWatch = Display(320,140,WHITE,BLACK,BLUE);
  10. Display minDisplay = Display(80,50,WHITE,BLACK,ILI9341_LIGHTGREY);
  11. Display secDisplay = Display(80,50,WHITE,BLACK,ILI9341_LIGHTGREY);
  12. Display milDisplay = Display(80,50,WHITE,BLACK,ILI9341_LIGHTGREY);
  13. Button button = Button(320,100,GREEN,WHITE,WHITE);
  14.  
  15. Neotimer refreshTimer = Neotimer(10);
  16.  
  17. long startTime;
  18. long elapsedTime;
  19. int state = 0; // 0 - resseted, 1 - started, 2 - stopped
  20.  
  21. int Min = 0;
  22. int Seg = 0;
  23. int Mil = 0;
  24. int prevMin = 0;
  25. int prevSeg = 0;
  26. int prevMil = 0;
  27.  
  28. // Global buffer to be used for each element in the time
  29. String numDisplay;
  30. char numBuf[3]; // Añade un caracter para el EOS (End of String = 0)
  31.  
  32. void setup() {
  33.   startTime = millis();
  34.   Serial.begin(9600);
  35.   canvas.init();
  36.  
  37.   // Display Configurations
  38.   stopWatch.setText("Puzzle Timer");
  39.   stopWatch.fontSize = 4;
  40.   minDisplay.setText("00");
  41.   minDisplay.fontSize = 4;  
  42.   secDisplay.setText("00");
  43.   secDisplay.fontSize = 4;
  44.   milDisplay.setText("00");
  45.   milDisplay.fontSize = 4;
  46.  
  47.   // Button Configuration
  48.   button.setText("START");
  49.   button.fontSize = 4;
  50.   button.setEventHandler(&buttonEventHandler);
  51.  
  52.   // Adding and positioning in Canvas
  53.   canvas.add(&stopWatch,0,0);
  54.   canvas.add(&button,0,140);
  55.   canvas.add(&minDisplay,30,45,false);
  56.   canvas.add(&secDisplay,120,45,false);
  57.   canvas.add(&milDisplay,210,45,false);
  58. }
  59.  
  60. void loop() {
  61.     canvas.scan();
  62.      
  63.     if(state == 1){ //RUN State
  64.         elapsedTime = millis() - startTime;
  65.    
  66.         Min = (int)((elapsedTime / 1000) / 60);
  67.         Seg = (int)((elapsedTime  / 1000) % 60);
  68.         Mil = (int)((elapsedTime % 1000) / 10);
  69.        
  70.         // Cada 10 milisegundos actualiza el valor en el display
  71.         if(refreshTimer.repeat()){
  72.             // Podemos usar un Display para cada parte y así podemos actualizar solo la que cambia...
  73.             // Sólo actualizamos cuando cambie el valor así evitamos
  74.             // ciclos de ejecución redibujando cuando el valor no ha cambiado.
  75.             if(Min != prevMin){
  76.               writeDisplay(Min,&minDisplay);
  77.               prevMin = Min;
  78.             }
  79.             if(Seg != prevSeg){
  80.               writeDisplay(Seg,&secDisplay);
  81.               prevSeg = Seg;
  82.             }
  83.             if(Mil != prevMil){
  84.               writeDisplay(Mil,&milDisplay);
  85.               prevMil = Mil;
  86.             }
  87.         }
  88.        
  89.     }
  90.    
  91. }
  92.  
  93.  
  94. // Routine to enter number in Display with padded 0 if neccessary
  95. void writeDisplay(int number, Display* dsp){
  96.     // Pad with a leading zero if number is less than 10
  97.     if(number < 10){
  98.         numDisplay = "0";
  99.         numDisplay += String(number);
  100.     }else{
  101.         numDisplay = String(number);
  102.     }
  103.     numDisplay.toCharArray(numBuf,3);
  104.     dsp->setText(numBuf);
  105.     dsp->update();
  106. }
  107.  
  108. // Button Event Handler
  109. void buttonEventHandler(Button* btn){
  110.     if(state == 0){ // Start pressed
  111.         startTime = millis();
  112.         stopWatch.hide();
  113.         minDisplay.show();
  114.         secDisplay.show();
  115.         milDisplay.show();
  116.         btn->setText("STOP");
  117.         btn->setColors(RED,WHITE,GRAY1);
  118.         state = 1;
  119.     }else if(state == 1){ // Stop pressed
  120.         btn->setText("RESET");
  121.         btn->setColors(YELLOW,WHITE,GRAY1);
  122.         state = 2;
  123.     }else if(state == 2){ // Reset pressed
  124.         minDisplay.setText("00");
  125.         secDisplay.setText("00");
  126.         milDisplay.setText("00");
  127.         stopWatch.show();
  128.         btn->setText("START");
  129.         btn->setColors(GREEN,WHITE,GRAY1);
  130.         state = 0;
  131.     }
  132.     btn->show();  
  133. }
Add Comment
Please, Sign In to add comment