Bonjour à vous deux. En me documentant sur vos conseils la modification de temporisation s'enregistre bien en EEPROM.
Voici le code que je essayer d'améliorer mais qui mise à part quelques légers défauts, fonctionne, et cela grâce à vous.
Merci beaucoup.
/*------------MENUS--PRINCIPAUX---------------------------*/
String menuItems[] = {"TempoRetour", "Type", "Menu 3", "Menu 4", "Menu 5"};
/*-----------VARIABLES----------------------*/
int readKey;
int tempora;
int commande_monter = 0;
int commande_retour = 1;
int securite = 2;
int contact_moteur = 3;
int menuPage = 0;
int maxMenuPages = round(((sizeof(menuItems) / sizeof(String)) / 2) + .5);
int cursorPosition = 0;
/*---------------NOUVEAUX--CARACTERES-------------------------*/
byte downArrow[8] = {
0b00100,
0b00100,
0b00100,
0b00100,
0b00100,
0b10101,
0b01110,
0b00100
};
byte upArrow[8] = {
0b00100,
0b01110,
0b10101,
0b00100,
0b00100,
0b00100,
0b00100,
0b00100
};
byte menuCursor[8] = {
B01000,
B00100,
B00010,
B00001,
B00010,
B00100,
B01000,
B00000
};
enum {
BUTTON_NONE,
BUTTON_UP,
BUTTON_DOWN,
BUTTON_LEFT,
BUTTON_RIGHT,
BUTTON_SELECT
};
#include <Wire.h>
#include <EEPROM.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
Serial.begin(9600);
EEPROM.get(10,tempora);
lcd.createChar(0, menuCursor);
lcd.createChar(1, upArrow);
lcd.createChar(2, downArrow);
pinMode(commande_monter, INPUT);
pinMode(commande_retour, INPUT);
pinMode(securite, INPUT);
pinMode(contact_moteur, OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.write("Digital Dock");
delay (1000);
lcd.setCursor(0, 1);
lcd.write("v1.10");
delay(5000);
lcd.clear();
}
void loop()
{
while ((digitalRead(securite) == HIGH)&&(digitalRead(commande_monter) == HIGH))
{
commande_monter_levre();
}
while ((digitalRead(commande_retour) == HIGH)&&(digitalRead(securite) == HIGH))
{
commande_retour_auto();
}
if (digitalRead(securite) == LOW)
{
affichage_securites();
}
if ((digitalRead(securite) == HIGH)&&(digitalRead(commande_monter) == LOW)&&(digitalRead(commande_retour) == LOW))
{
commande_relache();
}
switch (getPressedButton()) {
case BUTTON_NONE:
lcd.print(F(" "));
break;
case BUTTON_UP:
lcd.print(F("UP "));
break;
case BUTTON_DOWN:
lcd.print(F("DOWN "));
break;
case BUTTON_LEFT:
lcd.print(F("LEFT "));
break;
case BUTTON_RIGHT:
lcd.print(F("RIGHT "));
break;
case BUTTON_SELECT:
lcd.print(F("SELECT"));
menus();
break;
}
delay(500);
}
void menus() {
mainMenuDraw();
drawCursor();
operateMainMenu();
}
/*---------LECTURE--DES--BOUTONS--DU--KEYPAD------------*/
byte getPressedButton() {
int value = analogRead(A0);
if (value < 50)
return BUTTON_RIGHT;
else if (value < 250)
return BUTTON_UP;
else if (value < 410)
return BUTTON_DOWN;
else if (value < 550)
return BUTTON_LEFT;
else if (value < 850)
return BUTTON_SELECT;
else
return BUTTON_NONE;
}
void mainMenuDraw() {
Serial.print(menuPage);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print(menuItems[menuPage]);
lcd.setCursor(1, 1);
lcd.print(menuItems[menuPage + 1]);
if (menuPage == 0) {
lcd.setCursor(15, 1);
lcd.write(byte(2));
} else if (menuPage > 0 and menuPage < maxMenuPages) {
lcd.setCursor(15, 1);
lcd.write(byte(2));
lcd.setCursor(15, 0);
lcd.write(byte(1));
} else if (menuPage == maxMenuPages) {
lcd.setCursor(15, 0);
lcd.write(byte(1));
}
}
void drawCursor() {
for (int x = 0; x < 2; x++) {
lcd.setCursor(0, x);
lcd.print(" ");
}
if (menuPage % 2 == 0) {
if (cursorPosition % 2 == 0) { // If the menu page is even and the cursor position is even that means the cursor should be on line 1
lcd.setCursor(0, 0);
lcd.write(byte(0));
}
if (cursorPosition % 2 != 0) { // If the menu page is even and the cursor position is odd that means the cursor should be on line 2
lcd.setCursor(0, 1);
lcd.write(byte(0));
}
}
if (menuPage % 2 != 0) {
if (cursorPosition % 2 == 0) { // If the menu page is odd and the cursor position is even that means the cursor should be on line 2
lcd.setCursor(0, 1);
lcd.write(byte(0));
}
if (cursorPosition % 2 != 0) { // If the menu page is odd and the cursor position is odd that means the cursor should be on line 1
lcd.setCursor(0, 0);
lcd.write(byte(0));
}
}
}
void operateMainMenu() {
int activeButton = 0;
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 0: // When button returns as 0 there is no action taken
break;
case 1: // This case will execute if the "forward" button is pressed
button = 0;
switch (cursorPosition) { // The case that is selected here is dependent on which menu page you are on and where the cursor is.
case 0:
menuItem1();
break;
case 1:
menuItem2();
break;
case 2:
menuItem3();
break;
case 3:
menuItem4();
break;
case 4:
menuItem5();
break;
}
activeButton = 1;
mainMenuDraw();
drawCursor();
break;
case 2:
button = 0;
if (menuPage == 0) {
cursorPosition = cursorPosition - 1;
cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
}
if (menuPage % 2 == 0 and cursorPosition % 2 == 0) {
menuPage = menuPage - 1;
menuPage = constrain(menuPage, 0, maxMenuPages);
}
if (menuPage % 2 != 0 and cursorPosition % 2 != 0) {
menuPage = menuPage - 1;
menuPage = constrain(menuPage, 0, maxMenuPages);
}
cursorPosition = cursorPosition - 1;
cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
mainMenuDraw();
drawCursor();
activeButton = 1;
break;
case 3:
button = 0;
if (menuPage % 2 == 0 and cursorPosition % 2 != 0) {
menuPage = menuPage + 1;
menuPage = constrain(menuPage, 0, maxMenuPages);
}
if (menuPage % 2 != 0 and cursorPosition % 2 == 0) {
menuPage = menuPage + 1;
menuPage = constrain(menuPage, 0, maxMenuPages);
}
cursorPosition = cursorPosition + 1;
cursorPosition = constrain(cursorPosition, 0, ((sizeof(menuItems) / sizeof(String)) - 1));
mainMenuDraw();
drawCursor();
activeButton = 1;
break;
}
}
}
// This function is called whenever a button press is evaluated. The LCD shield works by observing a voltage drop across the buttons all hooked up to A0.
int evaluateButton(int x) {
int result = 0;
if (x < 50) {
result = 1; // right
} else if (x < 250) {
result = 2; // up
} else if (x < 410) {
result = 3; // down
} else if (x < 550) {
result = 4; // left
}
return result;
}
void drawInstructions() {
lcd.setCursor(0, 1); // Set cursor to the bottom line
lcd.print("Utilisez ");
lcd.write(byte(1));
lcd.print("/");
lcd.write(byte(2));
}
void menuItem1() { // Function executes when you select the 1st item from main menu
int activeButton = 0;
lcd.clear();
lcd.setCursor(0, 1);
drawInstructions();
lcd.setCursor(0, 0);
lcd.print("TEMPO: ");
lcd.setCursor(8,0);
lcd.print(tempora);
lcd.setCursor(13, 0);
lcd.print(" ms");
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 2:
button = 0;
tempora = tempora + 1000;
tempora = constrain(tempora,1000,30000);
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(tempora);
lcd.setCursor(13,0);
lcd.print(" ms");
break;
case 3:
button = 0;
tempora = tempora - 1000;
tempora = constrain(tempora,1000,30000);
lcd.setCursor(8,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(tempora);
lcd.setCursor(13,0);
lcd.print(" ms");
break;
case 4: // This case will execute if the "back" button is pressed
button = 0;
EEPROM.put(10,tempora);
lcd.clear();
lcd.setCursor(2,0);
lcd.print(" REGLAGE ");
lcd.setCursor(2,1);
lcd.print(" VALIDE ");
delay(1500);
activeButton = 1;
break;
}
}
}
void menuItem2() { // Function executes when you select the 2nd item from main menu
int activeButton = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Test 2");
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 4: // This case will execute if the "back" button is pressed
button = 0;
activeButton = 1;
break;
}
}
}
void menuItem3() { // Function executes when you select the 3rd item from main menu
int activeButton = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Test 3");
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 4: // This case will execute if the "back" button is pressed
button = 0;
activeButton = 1;
break;
}
}
}
void menuItem4() { // Function executes when you select the 4th item from main menu
int activeButton = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Test 4");
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 4: // This case will execute if the "back" button is pressed
button = 0;
activeButton = 1;
break;
}
}
}
void menuItem5() { // Function executes when you select the 5th item from main menu
int activeButton = 0;
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("Test 5");
while (activeButton == 0) {
int button;
readKey = analogRead(0);
if (readKey < 790) {
delay(100);
readKey = analogRead(0);
}
button = evaluateButton(readKey);
switch (button) {
case 4: // This case will execute if the "back" button is pressed
button = 0;
activeButton = 1;
break;
}
}
}
/*---------COMMANDES------------*/
void commande_monter_levre()
{
lcd.setCursor(0, 0);
lcd.print("MONTER ");
lcd.setCursor(0, 1);
lcd.print(" ");
digitalWrite(contact_moteur,HIGH);
}
void commande_retour_auto()
{
lcd.setCursor(0, 0);
lcd.print("RETOUR ");
lcd.setCursor(0, 1);
lcd.print("AUTOMATIQUE ");
digitalWrite(contact_moteur,HIGH);
delay (tempora);
}
void commande_relache()
{
lcd.clear();
digitalWrite(contact_moteur,LOW);
}
void affichage_securites()
{
lcd.setCursor(0, 0);
lcd.print("SECURITES ");
lcd.setCursor(0, 1);
lcd.print("P1 ");
digitalWrite(contact_moteur,LOW);
}