I finally received my Arduino today, and have been trying to do a project. I want to control each led separately with two separate buttons on my remote. It works with led 1 and button 1, but not with led 2 and button 2. Can anyone help me please? Thank you. Here is my code:
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin2 = 12;
const int ledPin1 = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin1, LOW);
delay(1000);
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin2, LOW);
delay(1000);
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin2, LOW);
}
}}
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin2 = 12;
const int ledPin1 = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin1, LOW);
delay(1000);
digitalWrite(ledPin1, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
}
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin2, LOW);
delay(1000);
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin2, LOW);
}
}
Thank you so much! It works now! I read that a little bit, but I mean I'm a total noob at this. Basically I'd like to use this kind of code as a timer for a table hockey game I have. We usually play three minute periods, so I'd want to press the one button for that, and press another button for when a goal is scored. What advice would you have for changing the code since using delay isn't good? Thanks again!
GoBraves21:
Thank you so much! It works now! I read that a little bit, but I mean I'm a total noob at this. Basically I'd like to use this kind of code as a timer for a table hockey game I have. We usually play three minute periods, so I'd want to press the one button for that, and press another button for when a goal is scored. What advice would you have for changing the code since using delay isn't good? Thanks again!
Try to understand how it works. Then changing it to do what you want will be easy. Post any specific questions that we can answer.
For the buttons, the only specification you gave for how they operate, is "for that". You need to begin by developing concise, exact language for what is supposed to happen (and when).
For example, what is supposed to happen when a goal is scored?
Ok, thanks. I tried to grasp some of it, and I mean I get kind of what it is saying, but I'm still pretty lost on it. Basically I want to press a button, have a green light be off 180 seconds, and then turn on to signal the end of the period for 10 seconds then back off. Then I'd want to press that button to start the next period, unless it was the end of the game of course. For a goal, I'd want to press another button and have the light come on right then and stay on for 5 seconds, then back off, and do the same for whenever a goal is scored.
GoBraves21:
The major drawback with how I started the code I see now is that the second button does not switch on the LED if I have pressed the other button.
That major drawback, to put it in a more general way, is that you have used the delay() function. Nothing can happen while it's running.
Yeah, I figured out why using delay is bad. Thanks aarg, and let me see if I can get this right Larry. If the current milliseconds subtracted by Millis12 (which I assume is 0 because that is when I would push it right?) is equal to or greater than 180,000 milliseconds, then the light would turn on?
Ok, thanks! So, for the one LED that I would want off for 180 seconds and then on for 10 seconds and back off again, how exactly would I change that code you put up for me earlier without using delay?
I tried to get this code working without delay, but I haven't been able to get it to work like I want. Can anyone help me out? I know this isn't the actual time I would want to use for my game, but I'm trying to get both LEDs to work independently of one another. Here is my code:
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin2 = 12;
const int ledPin1 = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin1);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin1, HIGH);
if (millis() >= 5000UL
) digitalWrite(ledPin1, LOW)
// read the state of the pushbutton value:
;buttonState = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
digitalWrite(ledPin2, HIGH);
if (millis() >=5000UL
) digitalWrite(ledPin2, LOW);
} } }
const byte ledPin12 = 12;
const byte buttonPin2 = 2;
const unsigned long MyTime = 5000ul;
unsigned long Millis12;
boolean flag12 = false;
byte myCount = 0;
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin12, OUTPUT);
//LED off
digitalWrite(ledPin12, LOW);
// initialize the push button pin as an input:
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop()
{
// read the state of the push button value:
buttonState = digitalRead(buttonPin2);
//if button pressed start timing LED
if (flag12 == false && buttonState == HIGH)
{
//enable timing
flag12 = true;
Millis12 = millis();
//LED on
digitalWrite(ledPin12, HIGH);
}
//Time to toggle LED?
if(flag12 == true && millis() - Millis12 >= MyTime)
{
//get ready for next iteration
Millis12 += MyTime;
//toggle ledPin1
digitalWrite(ledPin12, !digitalRead(ledPin12));
if(myCount++ >= 1) //EDIT: was change from > to >=
{
//finished with this button press, get ready for the next
myCount = 0;
flag12 = false;
//LED off
digitalWrite(ledPin12, LOW);
}
}
} //END of loop()
Thanks a lot for all your help! Ok, so I get a little bit of it. Still not 100% sure on a lot of even after reading about a lot of the terms. The website is great for explanations of many of these terms. Without much programming knowledge, it just hasn't clicked it yet.
I copied and pasted the code so I could get them to work with both LEDs. Now I've mulled over trying to have one of the lights stay off for three minutes, then turn on for 10 seconds, then back off, and have the cycle repeat when I push the button to start the next period. Just to make things easier to test, MyTime2 I would actually want to be 180000ul for the 3 minute period length and MyTime3 I would actually want to be 10000ul for 10 seconds on at the end of the period. It still just hasn't totally clicked. The only difference in the LEDs with how my code is now is that LED13 turns on instantly for 2 seconds after I push my remote button instead of 5. Anymore help would be really appreciated! Thanks!
const byte ledPin12 = 12;
const byte buttonPin2 = 2;
const unsigned long MyTime = 5000ul;
const unsigned long MyTime2 = 2000ul;
const unsigned long MyTime3 = 10000ul;
const byte ledPin13 = 13;
const byte buttonPin3 = 3;
unsigned long Millis12;
unsigned long Millis13;
boolean flag12 = false;
boolean flag13 = false;
byte myCount = 0;
byte myCount2 = 0;
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin12, OUTPUT);
//LED off
digitalWrite(ledPin12, LOW);
// initialize the push button pin as an input:
pinMode(buttonPin2, INPUT_PULLUP);
{
// initialize the LED pin as an output:
pinMode(ledPin13, OUTPUT);
//LED off
digitalWrite(ledPin13, LOW);
// initialize the push button pin as an input:
pinMode(buttonPin3, INPUT_PULLUP);
}
}
void loop()
{
// read the state of the push button value:
buttonState = digitalRead(buttonPin2);
//if button pressed start timing LED
if (flag12 == false && buttonState == HIGH)
{
//enable timing
flag12 = true;
Millis12 = millis();
//LED on
digitalWrite(ledPin12, HIGH);
}
//Time to toggle LED?
if(flag12 == true && millis() - Millis12 >= MyTime)
{
//get ready for next iteration
Millis12 += MyTime;
//toggle ledPin1
digitalWrite(ledPin12, !digitalRead(ledPin12));
if(myCount++ >= 1) //EDIT: was change from > to >=
{
//finished with this button press, get ready for the next
myCount = 0;
flag12 = false;
//LED off
digitalWrite(ledPin12, LOW);
}
}
// read the state of the push button value:
buttonState = digitalRead(buttonPin3);
//if button pressed start timing LED
if (flag13 == false && buttonState == HIGH)
{
//enable timing
flag13 = true;
Millis13 = millis();
//LED on
digitalWrite(ledPin13, HIGH);
}
//Time to toggle LED?
if(flag13 == true && millis() - Millis13 >= MyTime2)
{
//get ready for next iteration
Millis13 += MyTime2;
//toggle ledPin1
digitalWrite(ledPin13, !digitalRead(ledPin13));
if(myCount++ >= 1) //EDIT: was change from > to >=
{
//finished with this button press, get ready for the next
myCount = 0;
flag13 = false;
//LED off
digitalWrite(ledPin13, LOW);
}
}
} //END of loop()
Ok, thanks for letting me know that. I edited that code with that info.
Here's what I'd like to do with each buttons totally separate of each other. ButtonPin2 goes only with ledPin12, ButtonPin3 goes only with ledPin13.
For Button2, Pin12, I want to press that button, have the light come on instantly for 5 seconds and then go back off, and do the same thing every time I push that button.
For Button3, Pin13, I'd like to press that button, have the light stay off for 180 seconds, turn on for 10 seconds, and then turn back off, and do the same thing every time I press that button.
Here is that slightly updated code by the way as well. Thanks again!
const byte ledPin12 = 12;
const byte buttonPin2 = 2;
const unsigned long MyTime = 5000ul;
const unsigned long MyTime2 = 2000ul;
const unsigned long MyTime3 = 10000ul;
const byte ledPin13 = 13;
const byte buttonPin3 = 3;
unsigned long Millis12;
unsigned long Millis13;
boolean flag12 = false;
boolean flag13 = false;
byte myCount12 = 0;
byte myCount13 = 0;
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
// initialize the LED pin as an output:
pinMode(ledPin12, OUTPUT);
//LED off
digitalWrite(ledPin12, LOW);
// initialize the push button pin as an input:
pinMode(buttonPin2, INPUT_PULLUP);
{
// initialize the LED pin as an output:
pinMode(ledPin13, OUTPUT);
//LED off
digitalWrite(ledPin13, LOW);
// initialize the push button pin as an input:
pinMode(buttonPin3, INPUT_PULLUP);
}
}
void loop()
{
// read the state of the push button value:
buttonState = digitalRead(buttonPin2);
//if button pressed start timing LED
if (flag12 == false && buttonState == HIGH)
{
//enable timing
flag12 = true;
Millis12 = millis();
//LED on
digitalWrite(ledPin12, HIGH);
}
//Time to toggle LED?
if(flag12 == true && millis() - Millis12 >= MyTime)
{
//get ready for next iteration
Millis12 += MyTime;
//toggle ledPin1
digitalWrite(ledPin12, !digitalRead(ledPin12));
if(myCount12++ >= 1) //EDIT: was change from > to >=
{
//finished with this button press, get ready for the next
myCount12 = 0;
flag12 = false;
//LED off
digitalWrite(ledPin12, LOW);
}
}
// read the state of the push button value:
buttonState = digitalRead(buttonPin3);
//if button pressed start timing LED
if (flag13 == false && buttonState == HIGH)
{
//enable timing
flag13 = true;
Millis13 = millis();
//LED on
digitalWrite(ledPin13, HIGH);
}
//Time to toggle LED?
if(flag13 == true && millis() - Millis13 >= MyTime2)
{
//get ready for next iteration
Millis13 += MyTime2;
//toggle ledPin1
digitalWrite(ledPin13, !digitalRead(ledPin13));
if(myCount13++ >= 1) //EDIT: was change from > to >=
{
//finished with this button press, get ready for the next
myCount13 = 0;
flag13 = false;
//LED off
digitalWrite(ledPin13, LOW);
}
}
} //END of loop()