Hello, I've been trying to make my own project and the code I made of a lot different codes. My problem is that after I press any of my buttons for few times the lcd doesn't show what it meant to show instead it shows some random characters.
The lcd is 2x40 and its programmed to display the real time from DS1302 module and to display in second line if any of them buttons are on or off. Everything works fine except that lcd. Could somebody look at my code and give me some quotes of what the problem might be. I understand the code is all messy because I've changed the whole circuit a bit and I didnt had time to fix the code up yet, but it works for what I need like it is now. I don't wanna change nothing in the code except the lcd problem after pressing "buttonPin or buttonPin4". Thanks so much for any help and very sorry for the way I express my self, English is my fourth language.
There's the code:
#include <LiquidCrystal.h>
#include <Wire.h>
#include "RTClib.h"
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
RTC_DS1307 RTC;
//buttons setup
const int ledPin= 13;
const int ledPin2= 9;
const int ledPin3 = 14;
const int ledPin4 = 6;
const int buttonPin = 1;
const int buttonPin2 = 10;
const int buttonPin3 =0;
const int buttonPin4 = 8;
const int buttonPin5 = 15;
const int buttonPin6 = 16;
const int buttonPin7 = 17;
// variables will change:
int state7 = HIGH;
int reading7;
int previous7 = LOW;
int state3 = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int sensorPin = A0;
int val = 0;
// select the input pin for the ldr
unsigned int sensorValue = 0; // variable to store the value coming from the ldr
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup() {
// DATE AND TIME MODULE CONFIG AND SERIAL COMMUNICATION////////////////////////////////////////////////////////////////////////////////////////
Serial.begin(9600);
Wire.begin();
RTC.begin();
pinMode(0,OUTPUT);
if (! RTC.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__)); }
// lcd setup
lcd.begin(40, 2);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
// initialize the pushbutton pins as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
pinMode(buttonPin7, INPUT);
}
void loop(){
////////////////////////////////////// DATE AND TIME SETUP/////////////////////////////////////////////////////////////////////////////////////
DateTime now = RTC.now();
lcd.setCursor(10, 0);
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
lcd.print(' ');
lcd.setCursor(0, 0);
if (now.hour()<10)
lcd.print('0');
lcd.print(now.hour(), DEC);
lcd.print(':');
if (now.minute()<10)
lcd.print('0');
lcd.print(now.minute(), DEC);
lcd.print(':');
if (now.second()<10)
lcd.print('0');
lcd.print(now.second(), DEC);
lcd.setCursor(22, 0);
int dayofweek = now.dayOfWeek();
switch(dayofweek){
case 1:
lcd.print("Poniedzialek");
break;
case 2:
lcd.print("Wtorek");
break;
case 3:
lcd.print("Sroda");
break;
case 4:
lcd.print("Czwartek");
break;
case 5:
lcd.print("Piatek");
break;
case 6:
lcd.print("Sobota");
break;
case 0:
lcd.print("Niedziela");
break;
delay(1000);
}
////////////////////////////////////// Button 1 switch and LCD display/////////////////////////////////////////////////////////////////////////////
buttonState = digitalRead(buttonPin);
lcd.setCursor(0, 1);
if (buttonState == LOW) {
lcd.print ("L1 ON ");
digitalWrite(ledPin, HIGH);}
if (buttonState == HIGH) {
lcd.print ("L1 OFF");
digitalWrite(ledPin, LOW);}
////////////////////////////////////// Button 2 switch and LCD display/////////////////////////////////////////////////////////////////////////////
buttonState = digitalRead(buttonPin2);
lcd.setCursor(8, 1);
if (buttonState == LOW) {
lcd.print ("L2 ON ");
digitalWrite(ledPin4, HIGH);}
if (buttonState == HIGH) {
lcd.print ("L2 OFF");
digitalWrite(ledPin4, LOW);}
//////////////////////////////////// Backlight (ledPin3) toggle //////////////////////////////////////////////////////////////////////////////////////////
reading = digitalRead(buttonPin3);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == LOW && previous == HIGH && millis() - time > debounce) {
if (state3 == LOW)
state3 = HIGH;
else
state3 = LOW;
time = millis();}
digitalWrite(ledPin3, state3);
previous = reading;
//////////////////
reading7 = digitalRead(buttonPin7);
// if we just pressed the button (i.e. the input went from LOW to HIGH),
// and we've waited long enough since the last press to ignore any noise...
if (reading7 == HIGH && previous7 == LOW && millis() - time > debounce) {
// ... invert the output
if (state7 == LOW)
state7 = HIGH;
else
state7 = LOW;
// ... and remember when the last button press was
time = millis();
}
digitalWrite(ledPin2, state7);
previous7 = reading7;
//////////////////////////////////////// Light Sensor ////////////////////////////////////////////////////////////////////////////////////////////////////
lcd.setCursor(35, 0);
sensorValue = analogRead(sensorPin);
if(sensorValue<220) lcd.print("Night");
else lcd.print ("Day:)");
}
Moderator edit: code tags. Yet again.