Go Down

Topic: Quick Help for checking a simple code - not working at moment (Read 142 times) previous topic - next topic

Jeff27

Hi Guys,

I am learning the magic world of Arduino at moment.... and I was watching the video tutorial on Youtube:

                        https://www.youtube.com/channel/UCmBMoU5ZDb44zwp47g3AInA

This is about how to synchronize two LED lights blinking at different time intervals. I tried to copy the same code shown in the video and run it in Arduino 1.05-r2, but the code did not work by saying:  'currentMillis' was not declared in this scope ('currentMillis' is a function I defined in the code). So I copied my code here for you pro guys to review it, maybe you can tell me why it's not working :

// constants to set pin numbers
const int greenLedPin = 11;
const int redLedPin = 12;
//variables
int greenLedState = HIGH;
int redLedState = HIGH;
long previousMillisRed = 0;
long previousMillisGreen = 0;
//time changing intervals of the 2 LED
long greenLedInterval = 900;
long redLedInterval = 200;

void setup(){
  currentMillis = millis();                    // this seems to be a problem when running, but in the video it was the same....
  pinMode(greenLedPin, OUTPUT);
  pinMode(redLedPin, OUTPUT);
}

void loop()
{
currentMillis = millis();
manageRed();
manageGreen();
}

// define the two functions for the 2 LED respectively
void manageRed(){
if(currentMillis - previousMillisRed > redLedInterval){
   previousMillisRed = currentMillis;
   redLedState = (redLedState == HIGH) ? Low : HIGH;
   digitalWrite(redLedPin, redLedState);
}
}

void manageGreen(){
if(currentMillis - previousMillisRed > greenLedInterval){
   previousMillisRed = currentMillis;
   greenLedState = (greenLedState == HIGH) ? Low : HIGH;
   digitalWrite(greenLedPin, greenLedState);
}
}

Could someone help me test it as well ....

Thank you very much.

Kind Regards,
Jeff

spycatcher2k

#1
Jun 03, 2014, 08:17 pm Last Edit: Jun 03, 2014, 08:25 pm by spycatcher2k Reason: 1
Low is not the same as LOW & you need a long currentMillis = 0; in your declarations also
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

robtillaart

please use code tags  - # button above smileys next time.

Code can contain "words" that are interpreted as markup for the forum resulting in unreadable code.

redid your code, search the 7 differences and explain them (== homework;)
Code: [Select]

// constants to set pin numbers
const int greenLedPin = 11;
const int redLedPin = 12;

//variables
int greenLedState = HIGH;
int redLedState = HIGH;

unsigned long previousMillisRed = 0;
unsigned long previousMillisGreen = 0;
//time changing intervals of the 2 LED
unsigned long greenLedInterval = 900;
unsigned long redLedInterval = 200;

unsigned long currentMillis = 0;

void setup()
{
 currentMillis = millis();                    // this seems to be a problem when running, but in the video it was the same....
 pinMode(greenLedPin, OUTPUT);
 pinMode(redLedPin, OUTPUT);
}

void loop()
{
 currentMillis = millis();
 manageRed();
 manageGreen();
}

// define the two functions for the 2 LED respectively
void manageRed()
{
 if(currentMillis - previousMillisRed > redLedInterval)
 {
   previousMillisRed = currentMillis;
   redLedState = (redLedState == HIGH) ? LOW : HIGH;
   digitalWrite(redLedPin, redLedState);
 }
}

void manageGreen()
{
 if(currentMillis - previousMillisGreen > greenLedInterval)
 {
   previousMillisGreen = currentMillis;
   greenLedState = (greenLedState == HIGH) ? LOW : HIGH;
   digitalWrite(greenLedPin, greenLedState);
 }
}

be sure to have a resistor in series with the LED.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy