Pages: [1]   Go Down
Author Topic: Quick Help for checking a simple code - not working at moment  (Read 91 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Fluid Mechanics PhD Student in University of Cambridge, UK
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Scotland
Offline Offline
Edison Member
*
Karma: 26
Posts: 1343
Have you had your Arduino fix today?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Low is not the same as LOW & you need a long currentMillis = 0; in your declarations also
« Last Edit: June 03, 2014, 01:25:07 pm by spycatcher2k » Logged

Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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.
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: