I just got my first arduino (Uno R3) and what I did was put 3 leds in it and code a lot of stuff to mess with the leds. So what I have done is the following: I made a program that receives a code from the user in the computer and acording to that does something, like blinking, turning on and off, etc. But when I was making an option to make the leds blink I noticed I could only make them blink forever, because I could not enter any other option after telling the arduino to blink the leds, or I could make they blink for a short while, using the for command. Here is the code:
//GLOBAL VARIABLES
int ledRed=6;
int ledYellow=5;
int ledGreen=3;
int ledState;
int x; int y; int counter;
//SETUP FUNCTION
void setup() {
Serial.begin(9600);
pinMode(ledRed, OUTPUT);
pinMode(ledYellow, OUTPUT);
pinMode(ledGreen, OUTPUT);
}
//LOW ALL LEDS FUNCTION
void lowAll(){
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, LOW);
}
//LIGHT RUN FUNCTION
void lightRun(){
x=0;
while (x<5){
lowAll();
digitalWrite(ledRed, HIGH);
delay(200);
digitalWrite(ledRed, LOW);
digitalWrite(ledYellow, HIGH);
delay(200);
digitalWrite(ledYellow, LOW);
digitalWrite(ledGreen, HIGH);
delay(200);
lowAll();
x=x+1;
}
}
//HIGH ALL LEDS FUNCTION
void highAll(){
digitalWrite(ledRed, HIGH);
digitalWrite(ledYellow, HIGH);
digitalWrite(ledGreen, HIGH);
}
//INVERT ALL LEDS SIGNAL FUNCTION
void invertAll(){
digitalWrite(ledRed, !digitalRead(ledRed));
digitalWrite(ledYellow, !digitalRead(ledYellow));
digitalWrite(ledGreen, !digitalRead(ledGreen));
}
//BLINK ALL LEDS FUNCTION
void blinkAll(){
for (x=0;x<6;x++){
digitalWrite(ledRed, !digitalRead(ledRed));
digitalWrite(ledYellow, !digitalRead(ledYellow));
digitalWrite(ledGreen, !digitalRead(ledGreen));
delay(500);
}
}
//FADE ALL LEDS FUNCTION
void fadeAll(){
y=0; counter=0;
while (counter<=20){
if (counter<10){
analogWrite(ledRed, y);
analogWrite(ledYellow, y);
analogWrite(ledGreen, y);
y=y+1; counter=counter+1;
delay(80);
}
if (counter>=10){
analogWrite(ledRed, y);
analogWrite(ledYellow, y);
analogWrite(ledGreen, y);
y=y-1; counter=counter+1;
delay(80);
}
}
}
//LOOP (MAIN) FUNCTION
void loop() {
if (Serial.available()>0){
ledState=Serial.read();
if (ledState=='R'){ //HIGH OR LOW RED LED
digitalWrite(ledRed, !digitalRead(ledRed));
}
if (ledState=='Y'){ //HIGH OR LOW YELLOW LED
digitalWrite(ledYellow, !digitalRead(ledYellow));
}
if (ledState=='G'){ //HIGH OR LOW GREEN LED
digitalWrite(ledGreen, !digitalRead(ledGreen));
}
if (ledState=='L'){
lowAll(); //CALLS FUNCTION LOW ALL LEDS
}
if (ledState=='H'){
highAll(); //CALLS FUNCTION HIGH ALL LEDS
}
if (ledState=='I'){
invertAll(); //CALLS FUNCTION INVERT ALL LEDS
}
if (ledState=='B'){
blinkAll(); //CALLS FUNCTION BLINK ALL LEDS
}
if (ledState=='r'){ //BLINKS RED LED
for (x=0;x<6;x++){
digitalWrite(ledRed, !digitalRead(ledRed));
delay(500);
}
}
if (ledState=='y'){ //BLINKS YELLOW LED
for (x=0;x<6;x++){
digitalWrite(ledYellow, !digitalRead(ledYellow));
delay(500);
}
}
if (ledState=='g'){ //BLINKS GREEN LED
for (x=0;x<6;x++){
digitalWrite(ledGreen, !digitalRead(ledGreen));
delay(500);
}
}
if (ledState=='F'){
fadeAll(); //CALLS FUNCTION FADE ALL LEDS
}
if (ledState=='S'){
lightRun(); //CALLS FUNCTION LIGHT RUN
}
Serial.println(ledState);
}
}
So, I hope you understand when looking at the code, but without the for the leds blink forever but I can't put any other option, and with the for they only blink some times (as much as I code them to). Also in the fade function, the led goes slowly to what would be a digital HIGH and them slowly goes back to what would be a digital LOW, but the "HIGH" is half the total shining they get to when I simply use digitalWrite(led, HIGH); why is that?