I am an electronics tech but I am brand new to arduino and processor language. I am starting with a three color light tree project and I need help starting and stopping the program. I am intending to light three LEDs in sequence and I need to vary the time each is on independently. I have accomplished this simply enough in loop fashion but now I want to trigger the sequence one time only using a normally open button. I am connected to pin 2 for an input using a 10k pullup resistor. I have tried a number of language angles and can't seem to get results. Any help would be greatly appreciated. Here is what I have so far:
To further explain... My project is a drag racing starting tree. I would like to hit the button to trigger the first light and have it hold lit for 5 sec. Then light 1 goes off and #2 lights for only 1/4 sec and goes off as the third lights for 10 sec. Then everything stops until the button is fired again.
Any help would be great! I am enjoying this but am running out of time.
Thanks.
groundfungus:
Which pin for input? Shouldn't use pin 1 unless you really need to. It is hardware serial TX.
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
The above belongs in setup() unless you change pinMode of a pin in loop for some reason. No reason to set pinMode every time through loop().
Next thing would be to look at the blink without delay example (in the IDE, File, Examples, Digital) to get rid of the delays.
He did say he was using pin 2 as his input. Thanks for adding setting the pin high, I missed that! I agree with what you suggested he move to setup but it's really not an issue for this sketch, right? Same with using millis vs delays. Since he's only sequencing lights I don't see the delays as hurting performance. Of course if the next progression is to add trip lights to see if someone left early then the delays would cause a problem.
I agree with what you suggested he move to setup but it's really not an issue for this sketch, right?
No issue at all. Just isn't necessary in this context.
Delay is fine if you don't want to do anything else but wait. BWOD is the way to do lots of stuff while you wait. Maybe not for this sketch, but the next one?
One more suggestion:
const byte inputPin = 2;
// instead of
int inpuPin = 2;
Would be better because inputPin will be stored in flash (program) memory instead of SRAM. Again not a big deal for this sketch but later in life, when SRAM becomes more valuable, things like that can make a difference.
Thank you for all the help. I think I see where my thinking was off track. I think I can pull this off. I am enjoying this. These are amazing tools with a ton of potential and a lot of uses.
Been working on this off and on through the day, I implemented the above suggestions and modified them a number of ways but have had no luck recognizing any call from the #2 input. I have removed the resistor and tried pulling pin @ high as well. many times I end up with simple language errors. Sometime I can work them out and other times it loads correct but the lights keep cycling away merrily in loop fashion regardless.
I seem to have the digital HIGH and delay and LOW working flawlessly. So well in fact the I can't get the program to stop! Any suggestions on how to write in a normally open PB start and then have the program stop after it runs it's course one time and then waits for another input to start again?
I am grateful for every ones willingness to help like this... good people out there! Is a great learning tool... and humbling too!
This set up will load without error but cycles in loop like the others. Have tried a number of others I have tried to learn from the book as well. I am currently connected to the 5v pin and the #2 pin with a normally open switch. Is this correct?
here is what I am thinking (I could be way off). I think my problem is in the "if" line. Am I correct to assume that the sketch will require the input to put a call in to start because of the "if" command? Then if I can get this to function properly it will solve the start and stop problem together. The pro0gram will run it's course and then wait for another "if" command to start again.
It means,in this case, that you left off a { right after loop().The last code that you posted is an incomplete repost of your original code. Do you want help with your new code?
You should be connecting pin 2 to ground. Set pin 2 high in setup (use the internal pullup resistor) and then when you close the switch it will go low. The if Statement is looking for a low on pin 2 to start the cycle. I'm assuming it's looping continuously because it isn't set high in setup. If you post your complete code I'll replicate your setup in the morning and try it out.
Here is a sketch that does what you want and uses millis instead of delays. I wrote the code so that I could test it with a relay representing the lights. The relays are activated with a low signal so you will need to reverse the highs and lows for the 3 lights and assign the lights and push button to the pins you are using. Other than that you are all set. Let me know if you have any questions.
const int lightOne = 7;
const int lightTwo = 6;
const int lightThree = 5;
const int startButton = 12;
unsigned long previousMillis;
unsigned long timeDelay = 2000;
unsigned long printMillis = 0;
unsigned long currentMillis = 0;
int CycleStage = 0;
int runState = 0;
void setup() {
Serial.begin(9600);
digitalWrite (lightOne, HIGH);
digitalWrite (lightTwo, HIGH);
digitalWrite (lightThree, HIGH);
pinMode (lightOne, OUTPUT);
pinMode (lightTwo, OUTPUT);
pinMode (lightThree, OUTPUT);
pinMode (startButton, INPUT_PULLUP);
}
void loop() {
currentMillis = millis();//update every loop
if (digitalRead (startButton) == 0){
runState = 1;}
if (runState == 1){
if (currentMillis - previousMillis > timeDelay) {
CycleStage++;
previousMillis = currentMillis;//reset timer
}
switch (CycleStage) {
case 0:
break;
case 1:
timeDelay = 500;//update timeDelay for next step
break;
case 2://
digitalWrite (lightOne, LOW);
//delay (?);
timeDelay = 5000;//update timeDelay for next step
break;
case 3:
digitalWrite (lightOne, HIGH);
digitalWrite (lightTwo, LOW);
// delay (?);
timeDelay = 250;//update timeDelay for next step
break;
case 4:
digitalWrite (lightTwo, HIGH);
digitalWrite (lightThree, LOW);
// delay (?);
timeDelay = 10000;//update timeDelay for next step
break;
case 5:
digitalWrite (lightThree, HIGH);
// delay (?);
timeDelay = 100;//update timeDelay for next step
runState = 0;
CycleStage = 0;
break;
}
}
}
Sorry guys, I was out of town for the weekend and was not able to get back to this until tonight. Thank you for the advice. I will try the new code and let ya know how it works out.