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)
}
1 Answer 1
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.aaa– aaa2016年01月23日 01:00:42 +00:00Commented 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!Mikael Patel– Mikael Patel2016年01月23日 09:11:22 +00:00Commented Jan 23, 2016 at 9:11