Go Down

Topic: Buttons help (Read 1 time) previous topic - next topic

TheXboxnerd

I need help with my project, so if a button is pressed, then say hi in Serial Monitor, and if the button is pushed again, then say bye. 

JimboZA

You go first: what have you tried so far?
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

Grumpy_Mike

So what have you written so far? Please post your progress with this project.

TheXboxnerd

I'm not so sure how to do this exactly, so I haven't tried making the code yet.

JimboZA

Ok well have a look at this example: http://arduino.cc/en/Tutorial/ButtonStateChange .

But if you're really new, you might want to start at the top of the example page: http://arduino.cc/en/Tutorial/HomePage
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

BulldogLowell


I'm not so sure how to do this exactly, so I haven't tried making the code yet.


something like this:

Code: [Select]
int ledPin = 13;
int buttonPin = 2;  //buttonPin
int lastPressed;
int state;

void setup()
{               
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); 
}

void loop()
{
  int pressed = digitalRead(buttonPin);
  if (pressed == LOW)
  {
    if (lastPressed == HIGH)
    {
      state = !state;
      Serial.println(state? "hello" : "goodbye");
      digitalWrite(13, state? HIGH : LOW);
    }
  }
  lastPressed = pressed;
}

JimboZA



      Serial.println(state? "hello" : "goodbye");
      digitalWrite(13, state? HIGH : LOW);



I doubt if throwing the ternary operator at a beginner, without a single word of explanation, nor even saying it's called the ternary operator so he can look it up, has furthered his understanding one tiny bit.
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

BulldogLowell


I doubt if throwing the ternary operator at a beginner, without a single word of explanation, nor even saying it's called the ternary operator so he can look it up, has furthered his understanding one tiny bit.


right, that was sort of my point here... heck I'll throw out a proposed solution for the problem, but the OP may have to do some work to understand.

it is the "cryptic dungeon-master" approach :smiley-red:

I don't often use it, but it seemed à propos after the 2nd request

Go Up