ciao ragazzi ho collegato un lcd affinchè mi visualizzasse i dati letti da un contatore di un progetto precedente seguendo passo passo lo schema nella sezione learning...
inizialmente tutto funziona ma giunto al valore di 3 lo schermo inizia a dare i numeri interrompendo il conteggio...
visualizzando in contemporanea la sequenza sul monitor seriale ho anche notato che quando il contatore scende mi salta dei numeriù8da 2 passa a 4)
allego lo sketch;
#include <LiquidCrystal.h>
const int buttonPin = 7; // pulsante UP
const int buttonPin1= 6; // pulsante DOWN
const int buttonPin0=4;
const int led1= 9;
const int led2 = 10;
const int led3= 11;
const int led4= 12;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 0; // stato di UP
int buttonState1 = 0; //stato di DOWN
int buttonState0 = 0;
int lastButtonState = LOW; // previous state of the button
int lastButtonState1= LOW;
int lastButtonstate0=LOW;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin0,INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop() {
//conteggio da 1 a 4 ciclico
buttonState = digitalRead(buttonPin);
buttonState1= digitalRead(buttonPin1);
buttonState0= digitalRead(buttonPin0);
delay(20); // debounce
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++ ;
if (buttonPushCounter >4) buttonPushCounter =1 ;
Serial.println(buttonPushCounter);
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(buttonPushCounter);
LedOn(buttonPushCounter);
}
}
lastButtonState = buttonState;
//conteggio da 4 a 1 ciclico
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
buttonPushCounter-- ;
if (buttonPushCounter<1) buttonPushCounter=4;
Serial.println(buttonPushCounter);
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(buttonPushCounter);
LedOn(buttonPushCounter);
}
}
lastButtonState1 = buttonState1;
if(buttonState0 != lastButtonstate0){
if(buttonState0==HIGH) {
buttonPushCounter = 1;
LedOn(buttonPushCounter);
}
lastButtonstate0=buttonState0;
}}
void LedOn (int numberled)
{
// spegne led
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
// accende il led numero ...
switch(numberled) {
case 1:
digitalWrite(led1, HIGH);
break;
case 2:
digitalWrite(led2, HIGH);
break;
case 3:
digitalWrite(led3, HIGH);
break;
case 4:
digitalWrite(led4, HIGH);
break;
default:;
}
}
eliminando i comandi dell'lcd va tutto alla perfezione..