My code is supposed to have the lcd keypad shield, when left pressed, to turn the led off. But it doesn't work. the lcd keypad shield just flickers when i press left. here is the code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
/* Initialise l'écran LCD */
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("CmdOS");
delay(3000);
}
/** Fonction loop() */
void loop() {
int LED = 52;
pinMode(LED, OUTPUT);
int x;
x = analogRead(0);
if (x < 800) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Right = LED ON,");
lcd.setCursor(0, 1);
lcd.print("Left = LED Off.");
if (x < 60) {
lcd.clear();
lcd.print("LED On");
if (x < 800) {
digitalWrite(LED, HIGH);
} else if (x < 600) {
lcd.clear();
lcd.print("LED Off");
digitalWrite(LED, LOW);
}
};
}
};
/**
* Fonction de configuration de l'écran LCD pour la barre de progression.
* Utilise les caractéres personnalisés de 0 à 5 (6 et 7 restent disponibles).
*/
// You can have up to 10 menu items in the menuItems[] array below without having to change the base programming at all. Name them however you'd like. Beyond 10 items, you will have to add additional "cases" in the switch/case
// section of the operateMainMenu() function below. You will also have to add additional void functions (i.e. menuItem11, menuItem12, etc.) to the program.
Pressing ctl-t or Tools/AutoFormat does not change the behaviour of the sketch. It formats the code text ( does correct indentation ) to be better readable and understandable.
After doing so, it looks like this:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
/* Initialise l'écran LCD */
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("CmdOS");
delay(3000);
}
/** Fonction loop() */
void loop() {
int LED = 52;
pinMode(LED, OUTPUT);
int x;
x = analogRead(0);
if (x < 800) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Right = LED ON,");
lcd.setCursor(0, 1);
lcd.print("Left = LED Off.");
if (x < 60) {
lcd.clear();
lcd.print("LED On");
if (x < 800) {
digitalWrite(LED, HIGH);
} else if (x < 600) {
lcd.clear();
lcd.print("LED Off");
digitalWrite(LED, LOW);
}
};
};
};
/**
* Fonction de configuration de l'écran LCD pour la barre de progression.
* Utilise les caractéres personnalisés de 0 à 5 (6 et 7 restent disponibles).
*/
// You can have up to 10 menu items in the menuItems[] array below without having to change the base programming at all. Name them however you'd like. Beyond 10 items, you will have to add additional "cases" in the switch/case
// section of the operateMainMenu() function below. You will also have to add additional void functions (i.e. menuItem11, menuItem12, etc.) to the program.
The nested if(...)s with overlapping conditions looks confusing/confused. Especially with the inconsistent indenting.
For instance if x<60, then both x<800 and x<600 is true.
I imagine it flickers because some bit of the logic is conflicting and maybe you expect the screen change only when the button changes, not every time through the loop when the reading is unchanged from before.
The else if (x < 600) clause above will never be executed....because it can only reach here if x<60, and also x > 800. This whole clause will simplify to:
You have clear() in the loop. It may get executed multiple times per second...
That will cause glitches...
Better not clear the screen but simply overwrite.
Maybe add a timer (or delay(500)) to do the writing twice a second or so... lcd screens are not the fastest screens....
What is going on is you copy/pasted a bad sketch that you do not understand. You must learn to program before you copy/paste more bad code. You should learn to the point where you do not need help. THAT is what is going on.
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
/* Initialise l'écran LCD */
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(4, 0);
}
/** Fonction loop() */
void loop() {
int LED = 52;
pinMode(LED, OUTPUT);
int x;
x = analogRead(0);
lcd.print("CmdOS");
delay(3000);
if (x < 800) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Right = LED ON,");
lcd.setCursor(0, 1);
lcd.print("Left = LED Off.");
if (x < 60) {
lcd.print("LED On");
if (x < 800) {
digitalWrite(LED, HIGH);
} else if (x < 600) {
lcd.print("LED Off");
digitalWrite(LED, LOW);
}
};
}
};
/**
* Fonction de configuration de l'écran LCD pour la barre de progression.
* Utilise les caractéres personnalisés de 0 à 5 (6 et 7 restent disponibles).
*/
// You can have up to 10 menu items in the menuItems[] array below without having to change the base programming at all. Name them however you'd like. Beyond 10 items, you will have to add additional "cases" in the switch/case
// section of the operateMainMenu() function below. You will also have to add additional void functions (i.e. menuItem11, menuItem12, etc.) to the program.
oh wait i see the problem let me paste the code again
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
/* Initialise l'écran LCD */
lcd.begin(16, 2);
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("CmdOS");
delay(3000);
}
/** Fonction loop() */
void loop() {
int LED = 52;
pinMode(LED, OUTPUT);
int x;
x = analogRead(0);
if (x < 800) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Right = LED ON,");
lcd.setCursor(0, 1);
lcd.print("Left = LED Off.");
if (x < 60) {
if (x < 60) {
lcd.clear();
lcd.print("LED On");
digitalWrite(LED, HIGH);
};
} else if (x < 600) {
lcd.print("LED Off");
digitalWrite(LED, LOW);
}
};
}
/**
* Fonction de configuration de l'écran LCD pour la barre de progression.
* Utilise les caractéres personnalisés de 0 à 5 (6 et 7 restent disponibles).
*/
// You can have up to 10 menu items in the menuItems[] array below without having to change the base programming at all. Name them however you'd like. Beyond 10 items, you will have to add additional "cases" in the switch/case
// section of the operateMainMenu() function below. You will also have to add additional void functions (i.e. menuItem11, menuItem12, etc.) to the program.