Trouble converting button clicks to int to LCD message

Hi. I am trying to make something that pretty much shows my emotion through an LCD. Below in my code, greenButton is my button. What is supposed to happen is that first, on the LCD, all you see is "I am feeling:" oin row "0" of the LCD. Then when I click the button, a(starting at 0) increments by one using the ++ operator, but what ends up happening is that the whole row two is parts of many of the many different feelings. By the way, some of them occur a lot more often than others. Excited and bored appear a lot, and sometimes even, excited will appear many times in a row. The one thing that remains the same every time except the "I am feeling:" is the end o the line which it the beginning part of the "disappointed" feeling, normally contain the strong "dissapoin" Any help or advice would be great. Thank you!!!

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int greenButton = 8;
int a = 0;
void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("I am feeling:");
  pinMode(greenButton,INPUT_PULLUP);
}
void feeling() {
  if (digitalRead(greenButton) == HIGH) {
    a++;
    if (a <= 8) {
      if (a == 1) {
        lcd.print("happy");
      }
      if (a == 2) {
        lcd.print("bored");
      }
      if (a == 3) {
        lcd.print("sad");
      }
      if (a == 4) {
        lcd.print("angry");
      }
      if (a == 5) {
        lcd.print("afraid");
      }
      if (a == 6) {
        lcd.print("amazed");
      }
      if (a == 7) {
        lcd.print("excited");
      }
      if (a == 7) {
        lcd.print("dissapointed");
      }
    } else {
      a = 0;
    }
  }
}
void loop() {
  lcd.setCursor(0,1);
  feeling();
}

Ethan :slight_smile: :slight_smile: :slight_smile:

Read Gammon Forum : Electronics : Microprocessors : Switches tutorial.

You need to detect transitions (from not pressed to pressed) rather than just "it is pressed right now".

if (digitalRead(greenButton) == HIGH)

So you want this to happen when the button is NOT pressed? That's what you have. If you want this to happen when the button IS pressed then you want LOW instead of HIGH.

Try clearing the LCD before printing the emotion.

void loop() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("I am feeling:");
  lcd.setCursor(0,1);
  feeling();
}

Ok. Thank you everyone. I will try all of that. So I would need a transistor? What type? Thank you.
Ethan :slight_smile: :slight_smile: :slight_smile:

EQOxx123:
Ok. Thank you everyone. I will try all of that. So I would need a transistor? What type? Thank you.
Ethan :slight_smile: :slight_smile: :slight_smile:

The man said, "transition".

Oh.. my bad.. Thank you @aarg

aarg:
The man said, "transition".

I am a little familiar with the FiniteStateMachine Library. Is that what I would need to incorporate and how would I do that? Thx. Also, in the meantime, the code below is what I have got to. Now, under "I am feeling:"on the LCD it says only one word at a time, but still goes out of order, can be a little repetitive, but the main thing is that now, unlike before, all of the pixels are shaking a lot and looks like a reflector going up and down. Here is my code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int greenButton = 8;
int a = 0;
void setup() {
  lcd.begin(16, 2);
  pinMode(greenButton,INPUT_PULLUP);
}
int feeling() {
  while (digitalRead(greenButton) == HIGH) {
    a++;
    if (a <= 8) {
      if (a == 1) {
        return 1;
      }
      if (a == 2) {
        return 2;
      }
      if (a == 3) {
        return 3;
      }
      if (a == 4) {
        return 4;
      }
      if (a == 5) {
        return 5;
      }
      if (a == 6) {
        return 6;
      }
      if (a == 7) {
        return 7;
      }
      if (a == 8) {
        return 8;
      }
    } else {
      a = 0;
      return 0;
    }
  }
}
void loop() {
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("I am feeling:");
  lcd.setCursor(0,1);
  feeling();
  if (a == 0) {
    lcd.print("sad");
  } if (a == 1) {
    lcd.print("happy");
  } if (a == 2) {
    lcd.print("scared");
  } if (a == 3) {
    lcd.print("nervous");
  } if (a == 4) {
    lcd.print("upset");
  } if (a == 5) {
    lcd.print("dissapointed");
  } if (a == 6) {
    lcd.print("proud");
  } if (a == 7) {
    lcd.print("surprised");
  } if (a == 8) {
    lcd.print("ashamed");
  }
}

I don't see any transition detection there. From the page I linked above:

  // see if switch is open or closed
  byte switchState = digitalRead (switchPin);
  
  // has it changed since last time?
  if (switchState != oldSwitchState)
    {
    oldSwitchState =  switchState;  // remember for next time 
    if (switchState == LOW)
       {
       Serial.println ("Switch closed.");
       }  // end if switchState is LOW
    else
       {
       Serial.println ("Switch opened.");
       }  // end if switchState is HIGH
    }  // end of state change

The test for "has it changed since last time?" is the transition test.


However your code is not detecting transitions:

  while (digitalRead(greenButton) == HIGH) {

... the pixels are shaking a lot ...

That will happen if you redraw them 1000 times a second.