I'm currently working on a little project and have ran into some trouble.
Right now i got an LCD screen and a pushbutton connected to my arduino board.
What i want is the text on the LCD screen to blink first time i press down the button, and stop blinking when i press it down the second time.
Right now it partly works. I can get the LCD screen to blink, but it's very random on which buttonpress it stops blinking on.
I'm new to arduino, so don't know how every command works exactly yet.
below is my code:
// include the library code:
#include <SerialLCD.h>
#include <SoftwareSerial.h> //this is a must
const int buttonPin = 2;
int buttonState = 0;
int buttonPresses = 0; // how many times the button has been pressed
int lastPressCount = 0;
// initialize the library
SerialLCD slcd(4,3);//this is a must, assign soft serial pins
void setup() {
pinMode(buttonPin, INPUT);
// set up
slcd.begin();
// Print a message to the LCD.
slcd.print("TEST");
Serial.begin(9600);
}
void loop() {
if (digitalRead(buttonPin) == HIGH) // check if button was pressed
{
buttonPresses++; // increment buttonPresses count
delay(100); // debounce switch
}
if (buttonPresses == 1) // rollover every fourth press
if (lastPressCount != buttonPresses) // only do output if the count has changed
{
Serial.print ("Button press count = "); // out to serial
Serial.println(buttonPresses, DEC);
// Turn off the display:
slcd.noDisplay();
delay(200);
// Turn on the display:
slcd.display();
delay(200);
}
if (buttonPresses == 2)
{
slcd.display();
}
}
Ty for the help. I rewrote the code a bit, while waiting for an answer, but it shouldn't be much difference.
I did what you told me and i got me a bit closer to working, but not quite yet.
Right now it blinks on every second press, but only when the button is pressed, not for the entire time until i press the button again.
#include <SerialLCD.h>
#include <SoftwareSerial.h>
const int buttonPin = 2;
const int buttonPin1 = 7;
int count = 0;
int count1 = 0;
int lastButtonState = 0;
int buttonState = 0;
int buttonState1 = 0;
SerialLCD slcd(4,3);
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
slcd.begin();
slcd.print("TEST");
Serial.begin(9600);
}
void loop(){
buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
Serial.print ("Button press count = ");
Serial.println(count, DEC);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
count++;
}
lastButtonState = buttonState;
if (count %2 == 0){
slcd.noDisplay();
delay(200);
slcd.display();
delay(200);
}
else {
slcd.display();
}
}
}