lcd pushbutton hookup

I have two servos hooked up to dgital pins 9 and 10. They have their own power supply, and a 25v 2200 uf capacitor.

pushbutton hooked up to digital pin 4, 10k pickup, feeding off the usb +5v.

An lcd (16,2) with back pack hooked to (A4 and A5) also feeding off usb.

All share a common ground.

My endgame is to get the lcd to display "Not Activated" when the button is not pressed, and "Activated" when pressed. The servos go through a loop after the button press and stop at the end of the loop (void myFunction). So I would like the lcd to display "Not Activated" at the end of the loop along with the servos.
Now i did ask this question in a different part of this forum under a different index, so I decided to go here under lcds...should have asked here first. shame on me.

here is my code:

#include <Wire.h>//must include with LCDI2C
#include <LiquidCrystal_I2C.h>
#include <Servo.h>  // servo library
//below is the address and pin set up for the I2C backpack
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

#define buttonPin  4//permenantly define the button to digital pin 4

int buttonVal = 0;//added this code to announce that the button
//would be considered of (no power)

unsigned long nowMillis=0;
unsigned long debounceTimer=0;

Servo servo1;  // servo control object
Servo servo2;

void setup()
{
  lcd.begin(16,2);//type of LCD
  lcd.backlight();//turn backlight on
  lcd.setCursor(0,1);//sets word to bottom line
  lcd.print("roboJACK V1.8.5");//never to be changed
  lcd.clear();
  
  pinMode(buttonPin,INPUT);
  digitalWrite(buttonPin, HIGH);
  
  servo1.attach(9);
  servo2.attach(10);
}

void loop()
{
  buttonVal = digitalRead(buttonPin);
  
  if (buttonVal == 0)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Not Activated");
    delay(250);
  }else{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("activated");
    delay(250);
  }
  
  nowMillis=millis();
  /*by adding the 'button = 0;' code, the digitalRead can now
  be set to '==' instead of '!=" because i clarified that the 
  button had no power going to it, before the arduino considered
  the button to be hot*/
  if ((digitalRead(buttonPin)==0) && ((nowMillis - debounceTimer) > 250))
    {
  int position;
  
  servo1.write(90);    // Tell servo to go to 90 degrees

  delay(1000);         // Pause to get it time to move

  servo2.write(90);    // Tell servo to go to 90 degrees

  delay(1000);
 
  servo2.write(0);    // Tell servo to go to 90 degrees

  delay(1000); 
         

  servo1.write(180);     // Tell servo to go to 0 degrees

  delay(1000);         // Pause to get it time to move
  
  servo2.write(90);    // Tell servo to go to 90 degrees

  delay(1000);
 
  servo2.write(0);    // Tell servo to go to 90 degrees

  delay(1000); 
  
   
    }
}
  void myFunction() {
  // put your code here
  //Will run once every time you press your button
}

So what is your question ?

You have HW and SW. You have not told us anything about your results/symptoms. If you are going to post
requesting help you need to at least demonstrate that you have made an effort to get it working and explain
why you are posting (ie: what is NOT working). Please state your question.

It is not clear to me how buttonVal == 0

Try printing buttonVal to LCD to see:

void loop()
{
  buttonVal = digitalRead(buttonPin);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(buttonVal);  
  delay(250);

  if (buttonVal == 0)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Not Activated");
    delay(250);
...

I guess I'm not making myself clear. What you need to do is to tell us what you have tried and what the result
is . "I'm not sure about ...." is not a question. You need to run your code, record your observations, and state
the problem. We are not going to state the problem because you still have not told us what the problem is.
If something is not working, you need to tell us what is not working. Just posting your code and expecting
us to proofread it for you is not how this works. The premise is that you are trying to learn and we are
trying to help. If you have a specific question about your code state it.
It would be nice to see some indication that you have at least Googled your question.
For example, have you looked at this ?

Here are a few comments about your LCD code:

  lcd.print("roboJACK V1.8.5");//never to be changed
  lcd.clear();

Shouldn't the comment '

never to be changed

' read '

never to be seen

'?

In the first part of loop() get rid of both lcd.clear statements and change the second message from

lcd.print("activated");

to

lcd.print("    Activated");

Don

Rasch, I basically want to know what code is needed to get the LCD to display what I asked. As of right now, the LCD only displays activated, no matter if the button is pushed or not. I tried if else, do..while, even switch case. Nothing.

Floresta, thanks, will try that!

Florester will be able to help you more than I can. My area of expertise is electronics, not programming
but I would suggest changing your if statement condition to (buttonVal == LOW), and adding a boolean
flag variable that you set when the above condition is met, and use the flag status to determine when
and what to display since a button press is only a brief moment and as soon as you take your finger off
it and your code is realtime, not latching:,

if (buttonVal == 0)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Not Activated");
    delay(250);
  }else{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("activated");
    delay(250);
  }

so if you pressed the button it should display "Not Activated"
for 250ms and then execute the "else" code and display "activated". If you create boolean flags you can
add Serial.print statements to print the flag status and also perform operations based on combinations
of conditions. Obviously this whole concept only works if you remember to include code to reset the flags
when appropriate. Other than the above I don't have any other suggestions.

I will say this though, you are using a digitalRead statement and according to the Arduino tutorial:

digitalRead(pin)
Parameters

pin: the number of the digital pin you want to read (int)
Returns

HIGH or LOW

It doesn't say anything about returning "0" or "1" so maybe that's relevant. If you're
checking for "0" I don't think that will work if it returns HIGH or LOW. Florester would know though...

Florester would know though...

Sorry, but I am barely literate in 'C'. If he want's to retry it in assembly language I could probably help.

Don

pushbutton hooked up to digital pin 4, 10k pickup, feeding off the usb +5v.

I have to assume that when you say "10k pickup" , what you mean is "10k PULLup". , since there is no such thing as a
"10k pickup" in electronics.

So that means the DEFAULT (not activated) state for the button is HIGH since it is PULLed up.
When you press the button, (if the button is wired correctly to ground on the other side) the state goes to LOW.
However your code is the opposite.

 if (buttonVal == 0) // (ie LOW)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Not Activated");
    delay(250);
  }else{
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("activated"); // (ie: HIGH)
    delay(250);
  }

So how exactly is your button wired ?

I've got a K-10 pickup in my garage.

Don

It is not clear to me how buttonVal == 0

Rasch, I basically want to know what code is needed to get the LCD to display what I asked. As of right now, the LCD only displays activated, no matter if the button is pushed or not. I tried if else, do..while, even switch case. Nothing.

My endgame is to get the lcd to display "Not Activated" when the button is not pressed, and "Activated" when pressed. The servos go through a loop after the button press and stop at the end of the loop (void myFunction). So I would like the lcd to display "Not Activated" at the end of the loop along with the servos.

Attached is the corrected program , as far as I can tell, since you are not responding to questions.
As I pointed out. The if statement logic was backwards so I changed that to (buttonVal==HIGH).
Also, there were numerous uses of "LCD.clear" which were unnecessary.
I didn't really care for the choice of "Not Activated "/" Activated " so I changed it to something
that reflects the STATUS more clearly (I think) . (ie: " RESET "/ " ACTIVE ").

If you want it to display "Not Activated" when the servos stop all you have to do is put that code
right after the servo sequence code.

 lcd.begin(16,2);//type of LCD
  lcd.backlight();//turn backlight on
  lcd.setCursor(0,1);//sets word to bottom line
  lcd.print("roboJACK V1.8.5");//never to be changed
  lcd.clear();

This made no sense to me whatsoever. It prints something to the LCD with NO delay and follows it with an "lcd.clear();"
statement , so unless you can see something that only lasts 20uS there is really no point in including it.

 pinMode(buttonPin,INPUT);
  digitalWrite(buttonPin, HIGH);

If you don't mind me asking, why are you defining this pin as an INPUT and then writing to it as if it were an OUTPUT ?
(which is not since you clearly just defined it as an INPUT).

In any case , the attached program does everything you asked , but maybe not the way you asked for .
(I just didn't care for the term "Not Activated" , I don't really know why...

LCD_button_issue.ino (2.56 KB)

First of all, thanks for the replies guys. I have been busy with work, school, and family, so I am sorry for not responding sooner.

I did mean 10k pull-up, I was tired at the time, sorry.

As far as not understanding the code I used, understand, most of the code used is copy and paste, or a reference from a book. I am very, very, new to programming, so I don't fully understand what everything I use as code actually means. I am a visual learner, so I probably used the code, saw that it worked for a different application, and thought it would work here.

Rasch, I programmed the button as input and out as mistake. I copied a code from forum, and pieced a code from a book at a different time. My fault!

Thanks for the help!

To run my code just copy and paste your Lcd Constructor over mine because my
lcd didn't work with your constructor.

This made no sense to me whatsoever. It prints something to the LCD with NO delay and follows it with an "lcd.clear();"
statement , so unless you can see something that only lasts 20uS there is really no point in including it.

That's what I was getting at in the first part of reply #5. "Shouldn't the comment 'never to be changed' read 'never to be seen'?"

If you don't mind me asking, why are you defining this pin as an INPUT and then writing to it as if it were an OUTPUT ?
(which is not since you clearly just defined it as an INPUT).

This is why comments are so important. How about "// Enable the internal pull-up resistors"

Don

Attached is corrected program with your lcd constructor so it will run on your lcd.
I never got an answer to "How exactly is your button wired ?'
So the attached program has a micro pushbutton with a 1k PULL-DOWN to GND
on the left side and a 10k PULL-UP resistor on the RIGHT side of the button.
Button is a momentary so it is NORMALLY OFF .
Consequently D4 is NORMALLY HIGH.
Button press connects D4 THROUGH 1k resistor to GND, pulling D4 LOW.
and so on.
I tried to upload a short video but couldn't , probably due to size limitations.

LCD_button_issue_Bstank06.ino (2.73 KB)

floresta,
Oh, I get it now...

The button is wired to digital 4 pin with the pullup attached to +5v. and then of course it is grounded.
As far as the 1k pulldown, is that a specific type of resistor? Or is any 1K resistor a "pulldown". I ask because I have plenty of 1k resistors, I just dont know if they are pull down.
thank you very much!

"Pulldown means you have chosen to connect the 1k resistor from some pin to GND .
If your circuit required a 1k " pullup" instead the same exact resistor could be used.
Pullup means tied to Vcc(+5V) on one end , Pulldown means tied to GND on one end.
Have you tried the program I uploaded ?