Need guidance on small code

Hello

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();
}
  }

When you have trouble with switches, it is useless to show us the code without telling us how the switch is wired.

  if (digitalRead(buttonPin) == HIGH)  // check if button was pressed
  {
    buttonPresses++;                  // increment buttonPresses count
    delay(100);                       // debounce switch
  }

Wrong. You want to increment the counter when the switch BECOMES pressed, not IS pressed. Look at the state change detection example.

100 milliseconds is FAR too long for debouncing.

  if (lastPressCount != buttonPresses)              // only do output if the count has changed

Look at the modulo operator (%), instead. Do something when the count is even (%2 == 0) and something else when the count is odd.

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();
    }
  }
}
const int buttonPin = 2;
const int buttonPin1 = 7;

int buttonState = 0;
int buttonState1 = 0;

Is that the way you count? If you are going to number one of the variables, number ALL of them. Or use arrays.

Put every { on a new line. Use Tools + Auto Format. You'll see instantly why the LCD doesn't blink all the time. At least I did.

Changing the position of { didn't change anything :frowning: It still only blinks when the button is pushed.

Changing the position of { didn't change anything

Of course not. But it DID make it easier to see the problem.

Post your modified code, and I'll point out the problem.

#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();
    }
  }
}
void loop(){
    if (buttonState == HIGH) {

I said "every". I meant EVERY! I didn't mean "whichever ones you felt like".

  if (buttonState != lastButtonState) 
  {
    if (buttonState == HIGH) {
      count++;
    }
    lastButtonState = buttonState;
    if (count %2 == 0)
    {
      slcd.noDisplay();
      delay(200);
      slcd.display();
      delay(200);
    }
    else 
    {
      slcd.display();
    }
  }

Do you really want to blink the LCD only when the "button" state has changed? I certainly wouldn't.

Ahh that make alot of sense. Stupid mistake i didn't spot.

Well ty alot for the help.