0

I have 2 buttons and 2 voids (blink_slow) (blink_fast). i need to be able to press button 1 and blink-slow void will run, then press button 2 and blink fast will run. none of my code works, any idea how i can do this?

const int btn1 1;
const int btn2 2;
const int led 13;
void setup(){
pinMode(btn1,INPUT_PULLUP);
pinMode(btn2,INPUT_PULLUP);
pinMode(led,OUTPUT);
digitalWrite(led,HIGH);
}
void loop(){
if(digitalRead(btn2 =LOW)){
blink_slow();
}
if(digitalRead(btn3 =LOW)){
blink_fast();
}
void blink_slow(){
digitalWrite(led,LOW);
delay(1000)
digitalWrite(led,HIGH);
delay(1000)
}
void blink_fast(){
digitalWrite(led,LOW);
delay(500)
digitalWrite(led,HIGH);
delay(500)
}
Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
asked Jan 22, 2016 at 20:18

1 Answer 1

3

Start by correcting the obvious:

const int btn1 = 2;
const int btn2 = 3;
const int led = 13;
void setup()
{
 pinMode(btn1, INPUT_PULLUP);
 pinMode(btn2, INPUT_PULLUP);
 pinMode(led, OUTPUT);
 digitalWrite(led, HIGH);
}
void loop()
{
 if (digitalRead(btn2) == LOW) {
 blink_slow();
 }
 else if (digitalRead(btn3) == LOW) {
 blink_fast();
 }
void blink_slow()
{
 digitalWrite(led, LOW);
 delay(1000)
 digitalWrite(led, HIGH);
 delay(1000)
}
void blink_fast()
{
 digitalWrite(led, LOW);
 delay(500)
 digitalWrite(led, HIGH);
 delay(500)
}

The mistakes where 1) const variable assignment, 2) using pin 1 (TX) is a potential risk if you later what to use Serial for debugging, etc, 3) condition expression, and a lot of formatting. Open your C/C++ book and check the syntax.

Now your turn to improve this!

answered Jan 22, 2016 at 20:26
2
  • He's not using Serial, so it's possible to use the TX pin? I'm not sure if it has internal pull-ups aswell (or other specifics), but in theory it's not wrong? But yes, I would advise to use Serial for debugging, since that almost everything you've got. Commented Jan 23, 2016 at 1:00
  • Yes, using pin 1 (TX) is possible when not using Serial but may give issues later on when doing do. A good practice is to avoid pin 0 and pin 1 until the sketch is debugged. Not a mistake, more a potential risk. Good catch, Paul! Commented Jan 23, 2016 at 9:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.