Help with loops

Hi,
My project is making an RGB LED turn red when you get an email on Gmail. So far, that works. Now, I want to make the RGB LED cycle through colors while "scanning" for an email. I don't know how to merge both of these so that when an email comes, it will stop cycling through colors and blink in pin 13 to light up the red led.

Here is the code for the email(it also uses python but this is what I need to run constantly):

int val = 0; //value that is stored from Serial monitor
byte led = 13; //pin the led is connected to

void setup(){
Serial.begin(9600); //begins Serial monitor
pinMode(led, OUTPUT); //set led as an output
}

void loop(){
 if(Serial.available()) //check to see if Serial data is available
   val = Serial.read() - '0'; //store the numerical value
 digitalWrite(led, val); //write the value to the led
}

and here is the RGB Cycle code:

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE

void setup()
{
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 pinMode(bluePin, OUTPUT);  
}

void loop()
{
 setColor(255, 0, 0);  // red
 delay(1000);
 setColor(0, 255, 0);  // green
 delay(1000);
 setColor(0, 0, 255);  // blue
 delay(1000);
 setColor(255, 255, 0);  // yellow
 delay(1000);  
 setColor(80, 0, 80);  // purple
 delay(1000);
 setColor(0, 255, 255);  // aqua
 delay(1000);
}

void setColor(int red, int green, int blue)
{
 #ifdef COMMON_ANODE
   red = 255 - red;
   green = 255 - green;
   blue = 255 - blue;
 #endif
 analogWrite(redPin, red);
 analogWrite(greenPin, green);
 analogWrite(bluePin, blue);  
}

I want both of these to run simultaneously. Any help would be appreciated.

You could put the code from the RGB cycle program in a function and call it if you don't get a '1' in your email checking loop() function. Try this first then deal with the fact that the RGB code has delay()s in it which will stop anything else happening whilst they execute. A different timing technique using millis() is needed to make the code appear to do two things at once. The same goes for blinking the LED when mail is received.

Look at the BlinkWithoutDelay example and Several things at the same time

Could you give me an example? I am really new to Arduino. Can you link me to a tutorial?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Hi,
My project is making an RGB LED turn red when you get an email on Gmail. So far, that works. Now, I want to make the RGB LED cycle through colors while "scanning" for an email. I don't know how to merge both of these so that when an email comes, it will stop cycling through colors and blink in pin 13 to light up the red led.

Here is the code for the email(it also uses python but this is what I need to run constantly):

int val = 0; //value that is stored from Serial monitor
byte led = 13; //pin the led is connected to

void setup(){
Serial.begin(9600); //begins Serial monitor
pinMode(led, OUTPUT); //set led as an output
}

void loop(){
 if(Serial.available()) //check to see if Serial data is available
   val = Serial.read() - '0'; //store the numerical value
 digitalWrite(led, val); //write the value to the led
}

and here is the RGB Cycle code:

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE

void setup()
{
 pinMode(redPin, OUTPUT);
 pinMode(greenPin, OUTPUT);
 pinMode(bluePin, OUTPUT);  
}

void loop()
{
 setColor(255, 0, 0);  // red
 delay(1000);
 setColor(0, 255, 0);  // green
 delay(1000);
 setColor(0, 0, 255);  // blue
 delay(1000);
 setColor(255, 255, 0);  // yellow
 delay(1000);  
 setColor(80, 0, 80);  // purple
 delay(1000);
 setColor(0, 255, 255);  // aqua
 delay(1000);
}

void setColor(int red, int green, int blue)
{
 #ifdef COMMON_ANODE
   red = 255 - red;
   green = 255 - green;
   blue = 255 - blue;
 #endif
 analogWrite(redPin, red);
 analogWrite(greenPin, green);
 analogWrite(bluePin, blue);  
}

I want both of these to run simultaneously. Any help would be appreciated.
I am really new to this so linking me to a tutorial or doing it for me would be helpful!

Your problem is all those delays in the color sequence. When you call delay() the processor can do nothing else until the delay is done. You have to re-write the sketch so that it does each step when it is time to do the step. See the BlinkWithoutDelay example for the simplest example of doing something when a time elapses rather than using delay().

Didn't you like the answer in Help with loops - Programming Questions - Arduino Forum

johnwasser:
See the BlinkWithoutDelay example for the simplest example of doing something when a time elapses rather than using delay().

Thanks, but I read up on it and have 0 idea of how to implement it into my code. Can I just replace delay with the word millis?

UKHeliBob:
Didn't you like the answer in Help with loops - Programming Questions - Arduino Forum

Sorry, but I didn't understand your answer on that post.

@adamzakhan, stop cross-posting. Threads merged.

@adamzakhan, stop cross-posting. Other thread removed.

adamzakhan:
Sorry, but I didn't understand your answer on that post.

That is no reason to start a new thread.