Hey everybody! I just got an Arduino Uno R3 for Christmas and am SO EXCITED!
I've been fooling around with the beginner programs and decided to write my own. The program is in the attatchments, or should I embed the text in my post?
My problem is that my LED doesn't do anything. I'm sure the LED works, but I don't know if I'm calling the variables correctly or if I set the switch up properly. Please help a beginner with his first program!
Small programs like can be embedded in the message with code tags
/* Up here is where I will call int and define pin outs/ins, etc.
This program will blink a certain color of LED until I press the switch. When the switch is pressed, it will cycle to the next LED color and blink it.
*/
int color = 0;
int switchstate = 0;
int pin = color * 3;
void setup() {
// put your setup code here, to run once:
//declare outputs
pinMode(3, OUTPUT);
pinMode(6, OUTPUT);
pinMode(9, OUTPUT);
//declare inputs
pinMode (2, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (color == 4) {
color = 0;
}
switchstate = digitalRead(2);
if (switchstate == HIGH) {
color = color + 1;
digitalWrite(pin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a second
digitalWrite(pin, LOW);// turn the LED off by making the voltage LOW
delay(250); // wait for a second
}
//if the switch is pressed, increment color, then go to the blink function
//otherwise, keep blinking
else
digitalWrite(pin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(250); // wait for a second
digitalWrite(pin, LOW);// turn the LED off by making the voltage LOW
delay(250); // wait for a second
}
I don't see anything that changes pin from being equal to 0.
0,1 are the serial IO lines, you should try and keep those clear for serial comm's.
Edit: I changed where it said digitalWrite(pin, HIGH); to digitalWrite(color * 3, HIGH);
now I am getting this error:
avrdude: stk500_getsync(): not in sync: resp=0x00
Why?!?!?!
Edit2: I got it to work. Is there a way I could make it delay until the button is pressed?
Sure, you can read the button, and not let the program advance until you saw the button state you wanted.
Say you were looking for the pin state to read back as low:
while (digitalRead(pinX )== HIGH)
{
// do nothing
};
Once it reaches this it will stay there doing nothing but reading pinX over and over until its value came back as LOW, then it would proceed to the rest of the program.
Did you want it stopped that dead in its tracks?
Or did you want to just set a flag, go do some other stuff, and after the other stuff go back & check it again, only taking action when a check of the flag showed it had changed?
I just want the light turn on a certain color, then when the button is pressed and released, advance to the next color.
Then you need to know more that just whether the switch is pressed or not. You need to change what happens when a transition to pressed occurs.
To know when a transition occurs, you need to track the previous state of the switch.
int currState;
int prevState = LOW;
void loop()
{
currState = digitalRead(somePin);
if(currState != prevState)
{
// A transition occurred
}
prevState = currState;
}
Where the comment is, you can determine whether the transition was from pressed to released or from released to pressed, based on the value in currState. Generally, changes are made when the transition from released to pressed occurs. The transition from pressed to released is ignored.
Unless you are designing GUIs. Then, the transition from released to pressed is ignored, and the transition from pressed to released matters.