#include #include #include #include #include #include //only for debugging [RTC_DS1307 myRTC;] > [if (!myRTC.begin())] #include int a = 0; //Variable modifiable through the button to make the program change function int horas; //Variable to be displayed on OLED of the hours int minutos; //Variable to display on OLED of the minutes int segundos; //Variable to be displayed on OLED of seconds int segundostotal; //Total time int segundosrestantes; //Remaining time int porcentaje; //Percentage of completed time to total time int visual; //Variable that stores the pixel number between 0 and 'NUM_pixels' corresponding to 'percentage' int sethoras = 0; //Variable to store the 'set hours' value int setminutos = 0; //Variable to store the 'set minutes' value int setsegundos = 0; //Variable to store the value 'set seconds' const int PIN_LedDebug = 10; //Led on the board for debugging const int PIN_Potenciometro = 4; //Potentiometer as selector const int PIN_BotonInicio = 11; //Start button const int PIN_Neopixeles = 1; //NeoPixel Strip RGBW 144L/m 1m SMD5050 SK6812 const int NUM_Pixeles = 144; //Number of pixels of the strip int boton = 0; //Button status (startup shutdown) int potenciometroRAW; //Variable that stores the raw analog reading of the potentiometer int potenciometro; //Variable that stores the average of ten potentiometer measurements (to clean electrical output) int potenciometro100; //Variable storing the potentiometer position in percent //Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 myOLED(128, 64, &Wire, -1); //Reset pin # (or -1 if sharing Arduino reset pin) //Declaration for an DS1307 RTC connected to I2C (SDA, SCL pins) RTC_DS1307 myRTC; //Declaration for Neopixels Adafruit_NeoPixel pixels(NUM_Pixeles, PIN_Neopixeles, NEO_GRB + NEO_KHZ800); //---------------------------------------------------------------------------------------------------------------------// void setup() { Serial.begin(115200); pixels.begin(); //Initialize NeoPixel strip object (REQUIRED) pixels.setBrightness(250); //Set BRIGHTNESS (max = 255) if (!myOLED.begin(SSD1306_SWITCHCAPVCC, 0x3C)) //I2C OLED address 0x3C for 128x64 Serial.println(F("SSD1306 allocation failed")); if (!myRTC.begin()) Serial.println("Couldn't find RTC"); //myRTC.adjust(DateTime(F(__DATE__), F(__TIME__))); //Uncomment only to reprogram the RTC time. //pinMode(PIN_Neopixeles, OUTPUT); //It is not necessary to declare it pinMode(PIN_BotonInicio, INPUT); pinMode(PIN_LedDebug, OUTPUT); //pinMode(PIN_Potenciometro, INPUT); //It is not necessary to declare it } //--------------------------------------------------------------------------------------------------------------------// void loop() { //WELCOME FUNCTION while (a == 0) { //Commands for display of welcome text myOLED.clearDisplay(); myOLED.setTextColor(WHITE); myOLED.setTextSize(2); myOLED.setCursor(40,0); myOLED.println("TIME"); myOLED.setCursor(46,16); myOLED.println("THE"); myOLED.setCursor(40,34); myOLED.println("TIME"); myOLED.display(); //Commands to jump to the next stage boton = digitalRead(PIN_BotonInicio); if (boton == HIGH) { delay(300); a = 1; } //Commands to turn off all LEDs pixels.clear(); pixels.show(); //Commands to make a visual welcome animation //pixels.clear(); //Set all pixel colors to 'off' //for(int i=0; i 0)) { //If the potentiometer is in range, we decrease the minutes by one unit. sethoras--; delay(200); } if ((potenciometro100 < 30) && (potenciometro100 >= 15) && (setminutos > 0)) { //If the potentiometer is in range, we decrease the minutes by one unit setminutos--; delay(200); } //if ((potenciometro100 <= 20) && (setminutos = 0) && (sethoras > 0)) { //Special case // setminutos = 59; // sethoras--; //} // Make the minutes jump to 0, I don't know why (before it was as an if inside the previous one) if ((potenciometro100 <= 45) && (potenciometro100 >= 30) && (setsegundos > 0)) { //If the potentiometer is in range, we decrease the seconds by one unit setsegundos--; delay(200); } //if ((potenciometro100 < 45) && (potenciometro100 > 20) && (setsegundos = 0) && (setminutos > 0)) { //Special case // setsegundos = 59; // setminutos--; //} //Makes the seconds jump to 0, I don't know why (before it was as an if inside the previous one) //Range between 45 and 55 without action. if ((potenciometro100 >= 55) && (potenciometro100 <= 70)) //If the potentiometer is in range, we increase the seconds by one unit setsegundos++; if (setsegundos == 60) { setsegundos = 0; setminutos++; delay(200); } if ((potenciometro100 > 70) && (potenciometro100 <= 85)) //If the potentiometer is in range, we increase the minutes by one unit setminutos++; if (setminutos == 60) { setminutos = 0; sethoras++; delay(200); } if (potenciometro100 > 85) { //If the potentiometer is in range, we decrease the minutes by one unit sethoras++; delay(200); } //Commands to display the HH:MM:SS we are selecting for the timer myOLED.clearDisplay(); myOLED.setTextColor(WHITE); myOLED.setTextSize(2); myOLED.setCursor(0,0); myOLED.print("SET TIMER:"); myOLED.setCursor(0,16); myOLED.print("HH:MM:SS"); myOLED.setCursor(0,34); print2digits(sethoras); myOLED.print(":"); print2digits(setminutos); myOLED.print(":"); print2digits(setsegundos); myOLED.display(); //DEBUGGING (to check if it is reading the potentiometer, the button and if the counter is incremented accordingly) //Serial.write("Potenciometro(%):"); //Serial.println(position); //Serial.write("Estado del boton:"); //Serial.println(a); //Serial.write("Variable setsegundos(s):"); //Serial.println(setsegundos); //delay(1000); } //FUNCTION TO REPRESENT THE REMAINING TIME while (a == 2) { segundostotal = setsegundos + (setminutos * 60) + (sethoras * 60 * 60); //Convert the selected time to seconds segundosrestantes = segundostotal; //Gives as initial value to seconds remaining the total value of the time while (segundosrestantes > 0) { porcentaje = map(segundosrestantes, segundostotal, 0, 0, 100); //Convert to range from 0 to 100 (%) the percentage of time completed visual = map(segundosrestantes, segundostotal, 0, 0, NUM_Pixeles); //Convert to range from 0 to 'NUM_Pixels' the percentage of time completed delay (1000); //Discounting in 1-second periods segundosrestantes--; horas = ((segundosrestantes / 60)/ 60); //Convert remaining seconds to hours minutos = (segundosrestantes / 60) % 60; //Convert remaining seconds to minutes segundos = segundosrestantes % 60; //Convert remaining seconds to 60-second periods //Display the remaining HH:MM:SS of the timer myOLED.clearDisplay(); myOLED.setTextColor(WHITE); myOLED.setTextSize(2); myOLED.setCursor(0,0); myOLED.print("TIME LEFT:"); myOLED.setCursor(0,16); myOLED.print("HH:MM:SS"); myOLED.setCursor(0,34); print2digits(horas); myOLED.print(":"); print2digits(minutos); myOLED.print(":"); print2digits(segundos); myOLED.setTextSize(1); myOLED.setCursor(0,52); myOLED.print("COMPLETADO:"); myOLED.print(porcentaje); myOLED.print("%"); myOLED.display(); //Commands to turn on the leds according to the percentage completed pixels.setPixelColor(visual, pixels.Color(255, 255, 255, 255)); //It lights the LEDs corresponding to the percentage of time completed. pixels.show(); } while (segundosrestantes == 0) { myOLED.clearDisplay(); myOLED.setTextColor(WHITE); myOLED.setTextSize(2); myOLED.setCursor(40,0); myOLED.println("TIME"); myOLED.setCursor(51,16); myOLED.println("IS"); myOLED.setCursor(40,34); myOLED.println("OVER"); myOLED.display(); for(int i=0; i<144; i++) { pixels.setPixelColor(i, pixels.Color(255, 255, 255, 255)); pixels.show(); delay(50); } for(int i=0; i<144; i++) { pixels.setPixelColor(i, pixels.Color(0, 0, 0, 0)); pixels.show(); delay(50); } //boton = digitalRead(PIN_BotonInicio); //if (boton == HIGH) { // delay(300); // a = 0; //} //Does not work because it does not exit the while (this while does not evaluate 'a') } } } //Function to write two digits in the time counter even if the time to be represented is only one digit void print2digits(int number) { if (number >= 0 && number < 10) { myOLED.write('0'); } myOLED.print(number); }