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)
}
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!

  • 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. – Paul Jan 23 '16 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! – Mikael Patel Jan 23 '16 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.