Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Canvas_SEEEDTOUCH.h>
- #include <Display.h>
- #include <Button.h>
- // Esta librería es un helper para repetir algo cada cierto tiempo (non-blocking)
- // La consigues en el Libraries Manager de Arduino
- #include <neotimer.h>
- Canvas_SEEEDTOUCH canvas = Canvas_SEEEDTOUCH(TFT_LANDSCAPE,WHITE);
- Display stopWatch = Display(320,140,WHITE,BLACK,BLUE);
- Display minDisplay = Display(80,50,WHITE,BLACK,ILI9341_LIGHTGREY);
- Display secDisplay = Display(80,50,WHITE,BLACK,ILI9341_LIGHTGREY);
- Display milDisplay = Display(80,50,WHITE,BLACK,ILI9341_LIGHTGREY);
- Button button = Button(320,100,GREEN,WHITE,WHITE);
- Neotimer refreshTimer = Neotimer(10);
- long startTime;
- long elapsedTime;
- int state = 0; // 0 - resseted, 1 - started, 2 - stopped
- int Min = 0;
- int Seg = 0;
- int Mil = 0;
- int prevMin = 0;
- int prevSeg = 0;
- int prevMil = 0;
- // Global buffer to be used for each element in the time
- String numDisplay;
- char numBuf[3]; // Añade un caracter para el EOS (End of String = 0)
- void setup() {
- startTime = millis();
- Serial.begin(9600);
- canvas.init();
- // Display Configurations
- stopWatch.setText("Puzzle Timer");
- stopWatch.fontSize = 4;
- minDisplay.setText("00");
- minDisplay.fontSize = 4;
- secDisplay.setText("00");
- secDisplay.fontSize = 4;
- milDisplay.setText("00");
- milDisplay.fontSize = 4;
- // Button Configuration
- button.setText("START");
- button.fontSize = 4;
- button.setEventHandler(&buttonEventHandler);
- // Adding and positioning in Canvas
- canvas.add(&stopWatch,0,0);
- canvas.add(&button,0,140);
- canvas.add(&minDisplay,30,45,false);
- canvas.add(&secDisplay,120,45,false);
- canvas.add(&milDisplay,210,45,false);
- }
- void loop() {
- canvas.scan();
- if(state == 1){ //RUN State
- elapsedTime = millis() - startTime;
- Min = (int)((elapsedTime / 1000) / 60);
- Seg = (int)((elapsedTime / 1000) % 60);
- Mil = (int)((elapsedTime % 1000) / 10);
- // Cada 10 milisegundos actualiza el valor en el display
- if(refreshTimer.repeat()){
- // Podemos usar un Display para cada parte y así podemos actualizar solo la que cambia...
- // Sólo actualizamos cuando cambie el valor así evitamos
- // ciclos de ejecución redibujando cuando el valor no ha cambiado.
- if(Min != prevMin){
- writeDisplay(Min,&minDisplay);
- prevMin = Min;
- }
- if(Seg != prevSeg){
- writeDisplay(Seg,&secDisplay);
- prevSeg = Seg;
- }
- if(Mil != prevMil){
- writeDisplay(Mil,&milDisplay);
- prevMil = Mil;
- }
- }
- }
- }
- // Routine to enter number in Display with padded 0 if neccessary
- void writeDisplay(int number, Display* dsp){
- // Pad with a leading zero if number is less than 10
- if(number < 10){
- numDisplay = "0";
- numDisplay += String(number);
- }else{
- numDisplay = String(number);
- }
- numDisplay.toCharArray(numBuf,3);
- dsp->setText(numBuf);
- dsp->update();
- }
- // Button Event Handler
- void buttonEventHandler(Button* btn){
- if(state == 0){ // Start pressed
- startTime = millis();
- stopWatch.hide();
- minDisplay.show();
- secDisplay.show();
- milDisplay.show();
- btn->setText("STOP");
- btn->setColors(RED,WHITE,GRAY1);
- state = 1;
- }else if(state == 1){ // Stop pressed
- btn->setText("RESET");
- btn->setColors(YELLOW,WHITE,GRAY1);
- state = 2;
- }else if(state == 2){ // Reset pressed
- minDisplay.setText("00");
- secDisplay.setText("00");
- milDisplay.setText("00");
- stopWatch.show();
- btn->setText("START");
- btn->setColors(GREEN,WHITE,GRAY1);
- state = 0;
- }
- btn->show();
- }
Add Comment
Please, Sign In to add comment