#include // Screen #include #include #include "pico/stdlib.h" // LEDs #include "hardware/pwm.h" #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define buttonPin1 26 #define buttonPin2 27 #define buttonPin3 28 #define buttonPin4 29 #define buttonPin5 3 #define buttonPin6 4 const uint redPin = D6; // Broche D6 const uint greenPin = D8; // Broche D8 const uint bluePin = D7; // Broche D7 int buttonState1 = 0; int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; int buttonState6 = 0; int RedState = 100; //The value foe each LED color int GreenState = 100; int BlueState = 100; int lightHourOn = 11; int lightHourOff = 12; int lightMinuteOn = 60; int lightMinuteOff = 0; int globalTimeOn = 0; int hours = 0; // To dispaly the time passed int minutes = 0; int seconds = 0; int timeBuffer = 0; unsigned long previousMillis = 0; bool selectL1 = true; //To know at what line we are bool selectL2 = false; bool selectL3 = false; bool selectL4 = false; bool engageL1 = false; //To know if the line is engage (we can change the values) bool engageL2 = false; bool engageL3 = false; bool engageL4 = false; bool pageRGB = true; bool pageCycle = false; void setup() { gpio_set_function(redPin, GPIO_FUNC_PWM); // PWM parts gpio_set_function(greenPin, GPIO_FUNC_PWM); gpio_set_function(bluePin, GPIO_FUNC_PWM); uint slice_num_red = pwm_gpio_to_slice_num(redPin); //Get the slices uint slice_num_green = pwm_gpio_to_slice_num(greenPin); uint slice_num_blue = pwm_gpio_to_slice_num(bluePin); pwm_set_wrap(slice_num_red, 255); // Configure each slices pwm_set_wrap(slice_num_green, 255); pwm_set_wrap(slice_num_blue, 255); pwm_set_enabled(slice_num_red, true); //Activate the PWM pwm_set_enabled(slice_num_green, true); pwm_set_enabled(slice_num_blue, true); pinMode(buttonPin1, INPUT); //Initialiset the buttons and the screen pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); pinMode(buttonPin6, INPUT); Serial.begin(115200); // initialize with the I2C addr 0x3C (for the 128x64) if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Show initial display buffer contents on the screen -- // the library initializes this with an Adafruit splash screen. display.display(); delay(1000); // Pause for 2 seconds // Clear the buffer display.clearDisplay(); } void printRGB(){ // The RGB screen display.print(F(" Vert = "));display.print(RedState);display.print(F("% ")); if(selectL1 == true){ display.println(F(" *")); } if(engageL1 == true){ display.println(F("**")); } if(selectL1 == false && engageL1 == false){ display.println(F(" ")); } display.print(F(" Bleu = "));display.print(GreenState);display.print(F("% ")); if(selectL2 == true){ display.println(F(" *")); } if(engageL2 == true){ display.println(F("**")); } if(selectL2 == false && engageL2 == false){ display.println(F(" ")); } display.print(F(" Rouge = "));display.print(BlueState);display.print(F("% ")); if(selectL3 == true){ display.println(F(" *")); } if(engageL3 == true){ display.println(F("**")); } if(selectL3 == false && engageL3 == false){ display.println(F(" ")); } display.print(F(" "));display.print(hours);display.print(F(":"));display.print(minutes);display.print(F(":"));display.print(seconds);display.print(F(" ")); if(selectL4 == true){ display.println(F(" *")); } else { display.println(F(" ")); } } void printLightCycle(){ //The light cycle screen display.print(F(" Alumme : ")); display.print(lightHourOn); display.print(F(" h ")); if(selectL1 == true){ display.println(F(" *")); } if(engageL1 == true){ display.println(F("**")); } if(selectL1 == false && engageL1 == false){ display.println(F(" ")); } display.print(F(" ")); display.print(lightMinuteOn); display.print(F(" min ")); if(selectL2 == true){ display.println(F(" *")); } if(engageL2 == true){ display.println(F("**")); } if(selectL2 == false && engageL2 == false){ display.println(F(" ")); } display.print(F(" Eteint : ")); display.print(lightHourOff); display.print(F(" h ")); if(selectL3 == true){ display.println(F(" *")); } if(engageL3 == true){ display.println(F("**")); } if(selectL3 == false && engageL3 == false){ display.println(F(" ")); } display.print(F(" ")); display.print(lightMinuteOff); display.print(F(" min ")); if(selectL4 == true){ display.println(F(" *")); } if(engageL4 == true){ display.println(F("**")); } if(selectL4 == false && engageL4 == false){ display.println(F(" ")); } } void haut(){ Serial.println("Haut"); if(selectL2 == true){ selectL2 = false; selectL1 = true; } else if(selectL3 == true){ selectL3 = false; selectL2 = true; } else if(selectL4 == true){ selectL4 = false; selectL3 = true; } } void bas(){ Serial.println("Bas"); if(selectL3 == true){ selectL3 = false; selectL4 = true; } else if(selectL2 == true){ selectL2 = false; selectL3 = true; } else if(selectL1 == true){ selectL1 = false; selectL2 = true; } } void gauche(){ Serial.println("Gauche"); if(pageRGB == true){ if(engageL1 == true && RedState > 0) RedState --; if(engageL2 == true && GreenState > 0) GreenState --; if(engageL3 == true && BlueState > 0) BlueState --; } if(pageCycle == true){ if(engageL1 == true && lightHourOn > 0){ lightHourOn --; lightHourOff ++; } if(engageL2 == true && lightMinuteOn > 0){ lightMinuteOn --; lightMinuteOff = 60 - lightMinuteOn % 60; } if(engageL3 == true && lightHourOff > 0){ lightHourOn ++; lightHourOff --; } if(engageL4 == true && lightMinuteOff > 0){ lightMinuteOn ++; lightMinuteOff = 60 - lightMinuteOn % 60; if(lightMinuteOn == 60) //Exeption (60 an 0, not 60 and 60) lightMinuteOff = 0; } } } void droite(){ Serial.println("Droite"); if(pageRGB == true){ if(engageL1 == true && RedState < 100) RedState ++; if(engageL2 == true && GreenState < 100) GreenState ++; if(engageL3 == true && BlueState < 100) BlueState ++; } if(pageCycle == true){ if(engageL1 == true && lightHourOn < 23){ lightHourOn ++; lightHourOff --; } if(engageL2 == true && lightMinuteOn < 60){ lightMinuteOn ++; lightMinuteOff = 60 - lightMinuteOn % 60; if(lightMinuteOn == 60) //Exeption (60 an 0, not 60 and 60) lightMinuteOff = 0; } if(engageL3 == true && lightHourOff < 23){ lightHourOn --; lightHourOff ++; } if(engageL4 == true && lightMinuteOff < 60){ lightMinuteOn --; lightMinuteOff = 60 - lightMinuteOn % 60; } } } void okEntrer(){ Serial.println("2 Ok"); if(engageL1 == true || engageL2 == true || engageL3 == true || engageL4 == true){ // If already engage, disengage (like a validation) if(engageL1 == true){ engageL1 = false; selectL1 = true; } if(engageL2 == true){ engageL2 = false; selectL2 = true; } if(engageL3 == true){ engageL3 = false; selectL3 = true; } if(engageL4 == true){ engageL4 = false; selectL4 = true; } } else{ if(selectL1 == true){ //Engege the line if not already engage engageL1 = true; selectL1 = false; } if(selectL2 == true){ engageL2 = true; selectL2 = false; } if(selectL3 == true){ engageL3 = true; selectL3 = false; } if(selectL4 == true && pageRGB == true){ pageRGB = false; pageCycle = true; selectL1 = true; selectL2 = false; selectL3 = false; selectL4 = false; } if(selectL4 == true && pageCycle == true){ engageL4 = true; selectL4 = false; } } } void annulerRetour(){ Serial.println("1 Annuler/retour"); if(pageCycle == true && (selectL1 == true || selectL2 == true || selectL3 == true || selectL4 == true)){ pageRGB = true; pageCycle = false; selectL1 = true; selectL2 = false; selectL3 = false; selectL4 = false; } if(engageL1 == true){ engageL1 = false; selectL1 = true; } if(engageL2 == true){ engageL2 = false; selectL2 = true; } if(engageL3 == true){ engageL3 = false; selectL3 = true; } if(engageL4 == true){ engageL4 = false; selectL4 = true; } } void setColor(int red, int green, int blue) { //Apply the PWM pwm_set_gpio_level(redPin, red); pwm_set_gpio_level(greenPin, green); pwm_set_gpio_level(bluePin, blue); } void passOneSecond(long currentMillis){ unsigned long totalSeconds = currentMillis / 1000; // Convertir en secondes seconds = totalSeconds % 60; // Obtenir les secondes unsigned int totalMinutes = totalSeconds / 60; // Convertir en minutes minutes = totalMinutes % 60; // Obtenir les minutes unsigned int totalHours = totalMinutes / 60; // Convertir en heures hours = totalHours % 24; // Obtenir les heures } void loop() { display.setTextSize(1); // Normal 1:1 pixel scale display.fillScreen(SSD1306_BLACK); display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(0, 0); // Start at top-left corner if(pageRGB == true) //print the needed page printRGB(); if(pageCycle == true) printLightCycle(); // Show the display buffer on the screen display.display(); buttonState1 = digitalRead(buttonPin1); //read if the button in engage buttonState2 = digitalRead(buttonPin2); //read if the button in engage buttonState3 = digitalRead(buttonPin3); //read if the button in engage buttonState4 = digitalRead(buttonPin4); //read if the button in engage buttonState5 = digitalRead(buttonPin5); //read if the button in engage buttonState6 = digitalRead(buttonPin6); //read if the button in engage if(buttonState1 == 1) // If "up" button is pressed haut(); if(buttonState2 == 1) //If possible the value change gauche(); if(buttonState3 == 1) // If "down" button is pressed bas(); if(buttonState4 == 1) //If possible the value change droite(); if(buttonState5 == 1){ //Disengage the line annulerRetour(); //saveVariables(); delay(200); } if(buttonState6 == 1){ //Engage the line okEntrer(); //saveVariables(); delay(200); } globalTimeOn = (lightHourOn * 60) + lightMinuteOn; // Calculate if we are in the dark or the light phase if(((hours * 60) + minutes) >= globalTimeOn) // Dark phase setColor(0,0,0); else setColor(250*(static_cast(RedState)/100), 250*(static_cast(GreenState)/100), 250*(static_cast(BlueState)/100)); // Light phase delay(100); unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= 1000) { previousMillis = currentMillis; passOneSecond(currentMillis); } }