I am working on a project and I can't seem to program my Arduino to do the correct thing. I have tried to code it myself, but the Arduino does not do what I would like it to. I have two buttons connected to Digital pins 1 and 2, and three LED's connected to pins 13, 12, and 11. I want one of the LED's to blink while I am pressing a button. I would want the other LED to blink while the other button is pressed. While both buttons are pressed at the same time, I would like the third LED to turn on, but only while both buttons are being pressed. I have tried coding this myself, but the code doesn't do what I want it to. If someone could please upload the code for this it would be great. Thanks.
Why don't you post the code you wrote and we'll help you find the mistakes.
As @Delta_G said, post your code.
Don't use pin 1. Pins 0 and 1 are used by HardwareSerial for communicating with the PC.
...R
Ok, thanks, I will post the code.
This is my original code, I also had another sketch, but it didn't save.
const int ledPin= 13;
const int led1Pin= 12;
const int led2Pin= 11;
const int buttonPin= 2;
const int button1Pin= 3;
int buttonState= 0;
int button1State= 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(button1Pin, INPUT);
pinMode(ledPin,OUTPUT);
pinMode(led1Pin,OUTPUT);
pinMode(led2Pin,OUTPUT);
}
void loop()
{
buttonState= digitalRead(buttonPin);
button1State= digitalRead(button1Pin);
{
if ((buttonState==HIGH) && (button1State==HIGH))
(led2Pin,HIGH);
if ((buttonState==HIGH) && (button1State==LOW))
(ledPin, HIGH);
delay(175);
(ledPin,LOW);
delay(175);
if ((buttonState==LOW) && (button1State==HIGH))
(led1Pin,HIGH);
delay(175);
(led1Pin,LOW);
delay(175);
}
}
you've got quite a few mistakes. Afro future reference, use put
[.code]
[./code]
(delete the periods)
before and after your code to make it easier to read.
the first thing is that your if statements are formatted incorrectly.
it should be
if(buttonState == HIGH && button1State == HIGH){
some code
}
also, in order to drive the LED pins high, you need to use digitalWrite, not what you have.
Im not sure of your setup, but just in case, most LEDs do not have built in resistors, so you should add one to avoid damaging the LED or Board...
Thank you, I fixed the code using your advice, but nothing happens when I press the buttons. I would also like the LED's to blink only while I am pressing the buttons.
This is the code that I have now.
const int ledPin= 13;
const int led1Pin= 12;
const int led2Pin= 11;
const int buttonPin= 2;
const int button1Pin= 3;
int buttonState= 0;
int button1State= 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(button1Pin, INPUT);
pinMode(ledPin,OUTPUT);
pinMode(led1Pin,OUTPUT);
pinMode(led2Pin,OUTPUT);
}
void loop()
{
buttonState= digitalRead(buttonPin);
button1State= digitalRead(button1Pin);
{
if(buttonState==HIGH && button1State==HIGH)
digitalWrite(led2Pin,HIGH);
if(buttonState==HIGH && button1State==LOW)
digitalWrite(ledPin, HIGH);
delay(175);
digitalWrite(ledPin,LOW);
delay(175);
if (buttonState==LOW && button1State==HIGH)
digitalWrite(led1Pin,HIGH);
delay(175);
digitalWrite(led1Pin,LOW);
delay(175);
}
}
This is the code that I have now.
Is there a question?
I have three LED's on pins 13,12, and 11. I have two buttons on pins 2 and 3. I would like one LED to blink while one button is being pressed, and the other LED to blink while the other button is being pressed. When both buttons are pressed, I would like the third LED to stay while the button is being pressed.
Only one of the LED's blinks when I press the button. When I press the other button, the other LED does not blink.
Each of your if statements appears to be missing braces. Add an opening brace after the if and a closing one after the next four lines.
Now the first and third LED's work perfectly fine, but the second one stays on, and when both buttons are pressed, it turns off. I would like it to stay off, and only turn on while both buttons are being pressed at the same time. Here is my code.
const int ledPin= 13;
const int led1Pin= 12;
const int led2Pin= 11;
const int buttonPin= 2;
const int button1Pin= 3;
int buttonState= 0;
int button1State= 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(button1Pin, INPUT);
pinMode(ledPin,OUTPUT);
pinMode(led1Pin,OUTPUT);
pinMode(led2Pin,OUTPUT);
}
void loop()
{
buttonState= digitalRead(buttonPin);
button1State= digitalRead(button1Pin);
{
if(buttonState==LOW && button1State==LOW)
digitalWrite(ledPin,LOW);
digitalWrite(led1Pin,LOW);
digitalWrite(led2Pin,LOW);
}
{if(buttonState==HIGH && button1State==HIGH)
digitalWrite(led1Pin,HIGH);}
{ if(buttonState==HIGH && button1State==LOW)
digitalWrite(ledPin, HIGH);
delay(175);
digitalWrite(ledPin,LOW);
delay(175);}
{if (buttonState==LOW && button1State==HIGH)
digitalWrite(led2Pin,HIGH);
delay(175);
digitalWrite(led2Pin,LOW);
delay(175);}
}
{
if(buttonState==LOW && button1State==LOW)
digitalWrite(ledPin,LOW);
digitalWrite(led1Pin,LOW);
digitalWrite(led2Pin,LOW);
}
There's nothing syntactically wrong with the braces, but it may not be what you want - didi you mean
if(buttonState==LOW && button1State==LOW)
{
digitalWrite(ledPin,LOW);
digitalWrite(led1Pin,LOW);
digitalWrite(led2Pin,LOW);
}
?
The same problem is still occurring.
So, show us what the code looks like now.
It works. Thank you for your help.Help with Arduino code for a project - Programming Questions - Arduino Forum