Hi, I've just started using the Arduino and this is the first project I've done on my own so I'm having a lot of problems, but now I have one with the LCD screen that I can't fix:
as I run the code the screen seems to work but then it starts showing random characters.
Putting the code on an online simulator it seems to work so probably the problem is in the wiring or something but I don't know what it can be (I've read that there can be noise problems but I don't understand what I have to do).
What I've tried so far is to use a 100uF capacitor (I don't have 10) on the first two pins of the lcd with no results and also to put all the cables better in the breadboard.
Below I upload the code but simplified, given that the problems are already in the first part and perhaps solved there everything is resolved. I apologize if some parts of the code are not in English.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int rossoPin = 10;
int verdePin = 6;
int bluPin = 9;
int a=16; //intensità led
int timep=0; //timer precedente
unsigned long timer=0;
unsigned long tinizio;
unsigned long tora;
int tempo=240;
int err=0; //errori commessi
void setup(){
Serial.begin(9600);
lcd.begin(16,2);
lcd.setCursor(0,0);
lcd.print("Premere i 4");
lcd.setCursor(0,1);
lcd.print("pulsanti insieme");
pinMode(rossoPin, OUTPUT);
pinMode(verdePin, OUTPUT);
pinMode(bluPin, OUTPUT);
pinMode(8, INPUT);
pinMode(7, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
}
void loop(){
if(digitalRead(A1)==1&&digitalRead(A2)==1&&digitalRead(7)==1&&digitalRead(8)==1){
Serial.println("inizio ");
tinizio=millis();
lcd.clear();
delayt(3500);
sequenza();
sequenza();
sequenza();
delayt(1000);
Serial.println("FINE");
}
}
//esegue la fese delle sequenze con la verifica dell'errore o del successo
void sequenza(){
randomSeed(analogRead(0));
int sq1[6];
int i;
creasq(sq1);
Serial.print(" ");
writet();
// mostra sq1
int risposta=1;
do{
for (i=1;i<=sq1[0];i++){
colore(sq1[i]);
if (digitalRead(sq1[5])==1){
risposta=0;
lcd.setCursor(0,0);
lcd.print("CORRETTO!");
rgb(0,127,0);
delayt(1000);
lcd.setCursor(0,0);
lcd.print(" ");
break;
}
else if ((digitalRead(A1)==1||digitalRead(A2)==1||digitalRead(7)==1||digitalRead(8)==1)&&digitalRead(sq1[5])!=1){
lcd.setCursor(0,0);
lcd.print("ERRORE!");
err++;
rgb(127,0,0);
tempo=tempo-30;
delayt(1000);
lcd.setCursor(0,0);
rgb(0,0,0);
lcd.print(" ");
}
};
delayt(1000);
rgb(0,0,0);
if (digitalRead(sq1[5])==1){
risposta=0;
lcd.setCursor(0,0);
lcd.print("CORRETTO!");
rgb(0,127,0);
}
else if ((digitalRead(A1)==1||digitalRead(A2)==1||digitalRead(7)==1||digitalRead(8)==1)&&digitalRead(sq1[5])!=1){
lcd.setCursor(0,0);
lcd.print("ERRORE!");
err++;
rgb(127,0,0);
tempo=tempo-30;
}
delayt(1000);
lcd.setCursor(0,0);
lcd.print(" ");
rgb(0,0,0);
}while(risposta==1);
delayt(1000);
}
//crea sequenza colore con anche pulsante corretto
void creasq(int sq[]){
int i,n,l=random(2)+3;
sq[0]=l;
for(i=1; i<=sq[0];i++){
n= random(5);
sq[i]=n;
Serial.print(n);
}
Serial.print(" ");
writet();
if(l==3){
if(sq[1]!=3&&sq[2]!=3&&sq[3]!=3) //non c’è il colore rosso
sq[5]=A2;
else if (sq[3]==0) //se l'ultimo colore è il bianco
sq[5]=8;
else if (sq[1]==2) //se c’è il colore viola
sq[5]=A1;
else if (sq[2]==2)
sq[5]=A2;
else if (sq[3]==2)
sq[5]=7;
else //Altrimenti
sq[5]=7;
}
if(l==4){
int ros=0; //più di una volta il colore rosso
if(sq[1]==3)
ros=ros+1;
if(sq[2]==3)
ros=ros+1;
if(sq[3]==3)
ros=ros+1;
if(sq[4]==3)
ros=ros+1;
if(ros>1){
if(sq[4]==3)
sq[5]=8;
else if (sq[3]==3)
sq[5]=7;
else if (sq[2]==3)
sq[5]=A2;
}
else if (sq[4]==1&&sq[1]!=3&&sq[2]!=3&&sq[3]!=3) //se l'ultimo colore è il giallo e non c’è il colore rosso
sq[5]=A1;
else{
int bl=0; //se c'è esattamente una volta il blu
if(sq[1]==4)
bl=bl+1;
if(sq[2]==4)
bl=bl+1;
if(sq[3]==4)
bl=bl+1;
if(sq[4]==4)
bl=bl+1;
if(bl==1)
sq[5]=A2;
else{
int vi=0; //se c'è più di una volta il colore viola
if(sq[1]==2)
vi=vi+1;
if(sq[2]==2)
vi=vi+1;
if(sq[3]==2)
vi=vi+1;
if(sq[4]==2)
vi=vi+1;
if(vi>1)
sq[5]=8;
else //Altrimenti
sq[5]=A2;
}
}
}
Serial.print(sq[5]);
}
//colore per LED RGB
void colore(int col){
switch(col){
case 0:
rgb(a-14, a+24, a+16); //bianco
delayt(1000);
rgb(0, 0, 0);
delayt(200);
break;
case 1:
rgb(a-14, a+16, 0); //giallo
delayt(1000);
rgb(0, 0, 0);
delayt(200);
break;
case 2:
rgb(a-8, 0, a+16); //viola scuro
delayt(1000);
rgb(0, 0, 0);
delayt(200);
break;
case 3:
rgb(a, 0, 0); //rosso
delayt(1000);
rgb(0, 0, 0);
delayt(200);
break;
case 4:
rgb(0, 0, a); //blu
delayt(1000);
rgb(0, 0, 0);
delayt(200);
break;
}
}
//Procedura per settare il colore
void rgb(int rosso, int verde, int blu){
analogWrite(rossoPin, rosso);
analogWrite(verdePin, verde);
analogWrite(bluPin, blu);
}
//Delay ma con timer (delayt(0) cambia solo il timer)
void delayt(int d){
int i, x= d%800;
d=d-x;
for(i=0; i<d/800;i++){
delay(800);
writet();
}
delay(x);
writet();
}
//Scrive timer su lcd
void writet(){
int s,m;
tora=millis();
timer=(tempo-(tora-tinizio)/1000);
if (tempo-(tora-tinizio)/1000==0 || err>2){ //esplosione
writet();
rgb(255,0,0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("KA-BOOOM");
Serial.println("Esploso");
delay(1000000);
}
if(timer!=timep){
s=timer%60;
m=(timer-s)/60;
lcd.setCursor(12,1);
lcd.print(m);
lcd.setCursor(13,1);
lcd.print(":");
if (s<10){
lcd.setCursor(14,1);
lcd.print("0");
lcd.setCursor(15,1);
lcd.print(s);
}
else{
lcd.setCursor(14,1);
lcd.print(s);
}
}
timep=timer;
}
In this part of the code the LED simply shows a sequence of colors and according to it one of the four buttons must be pressed (on the Serial monitor one can see what must be pressed). you have to do it three times before the timer drops to 0 and without making more than 2 errors (plus each error takes 30 seconds off the timer)
(Obviously if the code can be improved in general I accept suggestions)
This is how the Arduino is wired: