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.
You go first: what have you tried so far?
So what have you written so far? Please post your progress with this project.
I'm not so sure how to do this exactly, so I haven't tried making the code yet.
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
TheXboxnerd:
I'm not so sure how to do this exactly, so I haven't tried making the code yet.
something like this:
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;
}
BulldogLowell:
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.
JimboZA:
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
I don't often use it, but it seemed à propos after the 2nd request