Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

strange LED behaviour
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

strange LED behaviour

by Rcayot on Sun Feb 23, 2020 11:54 am

First of all, thanks for all the help with this NOOB.

So, I have overcome a lot, and have begun using the Arduino IDE, and have run several of the tutorials on this and the Arduino site.

I am trying to rebuild my traffic light model I built for the RPi-4 I have and wanted to learn a new way of programming the sequence not using 'delay' but using 'millis'. Finally after a long time with trial and error, I have it working sort of. the program goes through the loop, green on for 3s, yellow on for 2s, then red on for 5s, repeat.

I observe a few troubling things. First of all, I have two sets of LEDS, one for the north-south traffic, and one for the east-west traffic. I do not have the east west LEDS coded in yet, but the are hooked up to D3, D4, and D5 on my Metro M4 express. Here is the thing.

1. When I load the sketch, the LED connected to D4 is very dimly lit, and when loading is complete, it goes out.
2. When I run the sketch, the yellow LED lights when it is supposed to, but when the pin is set to LOW, it only dims, and the red LED lights as supposed to. when the sequence goes back to the beginning, the red and dim yellow LEDs both go off as expected.

Why is the yellow LED not turning completely off? What is causing the east west LED on D5 to light dimly while sketch is loading?

here is code:

Code: Select all | TOGGLE FULL SIZE
//  traffic lights
//  Turns on red green yellow like traffic lights.
//  modified 20 feb 2020
//  by Roger Ayotte
// updated 2/22 to use milis to determine interval to change lights.
const int NSRED = 0;
const int NSYEL = 1;
const int NSGRN = 2;
const int EWRED = 3;
const int EWYEL = 4;
const int EWGRN = 5;
int count = 0;

// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval3s = 3000;           // interval at which to change from green to yellow (milliseconds)
const long interval2s = 2000;           // interval at which to change red to green (milliseconds)

// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(NSRED, OUTPUT);
  pinMode(NSYEL, OUTPUT);
  pinMode(NSGRN, OUTPUT);
  pinMode(EWRED, OUTPUT);
  pinMode(EWYEL, OUTPUT);
  pinMode(EWGRN, OUTPUT);
  // Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {

// check to see if it's time to change green to yellow; that is, if the difference
// between the current time and last time you changed lights is bigger than
// the interval at which you want to change.  for two way traffic lights,
// the total time of green + yellow needs to equal red time.
 
  unsigned long currentMillis = millis();
  // Serial.println(count);
  // start traffic light sequence
  if (currentMillis - previousMillis < interval3s) {
  digitalWrite(NSGRN, HIGH);
  digitalWrite(NSRED, LOW);   
  digitalWrite(NSYEL, LOW);
  }
  //check elapsed time
  if (currentMillis - previousMillis >= interval3s) {
  digitalWrite(NSRED, LOW);
  digitalWrite(NSGRN, LOW);   
  digitalWrite(NSYEL, HIGH);
  }
  if (currentMillis - previousMillis >= (interval2s + interval3s)) {
  digitalWrite(NSGRN, LOW);
  digitalWrite(NSYEL, LOW);
  digitalWrite(NSRED, HIGH);
  } 
  if (currentMillis - previousMillis >= (2*(interval2s + interval3s))) {
  previousMillis = currentMillis;
  count = count + 1;
  }
}

Rcayot
 
Posts: 36
Joined: Sat Feb 08, 2020 6:48 pm

Re: strange LED behaviour

by adafruit_support_bill on Sun Feb 23, 2020 12:21 pm

Please post some photos showing how you have everything wired up.

adafruit_support_bill
 
Posts: 77845
Joined: Sat Feb 07, 2009 10:11 am

Re: strange LED behaviour

by Rcayot on Sun Feb 23, 2020 2:27 pm

here is picture of wiring.

Image

here is picture of green LED during loading sketch (for first time)


Image

a picture of yellow light
Image

and then when yellow goes dimmer, and red lights
Image

thanks for any help.

Roger

Rcayot
 
Posts: 36
Joined: Sat Feb 08, 2020 6:48 pm

Re: strange LED behaviour

by adafruit_support_bill on Sun Feb 23, 2020 3:02 pm

According to your code, you are only writing to pins 0, 1 & 2. But your photos show activity on the LEDs connected to pins 4 and 5. Are you sure it is running the same code as posted?

Also, what is the value of the resistors used? Unlike the Atmega processors in the Arduino UNO, the pins on the M4 can only source or sink up to 7mA.

adafruit_support_bill
 
Posts: 77845
Joined: Sat Feb 07, 2009 10:11 am

Re: strange LED behaviour

by Rcayot on Sun Feb 23, 2020 4:39 pm

After I sent the code I tried changing the LEDs in the code from 0,1,2 to 3,4,5 to see if the effect was pin related. It wasn't. The dim yellow still persists.

I am using 1k resistors.

Thanks.

Roger

Rcayot
 
Posts: 36
Joined: Sat Feb 08, 2020 6:48 pm

Re: strange LED behaviour

by adafruit_support_bill on Sun Feb 23, 2020 5:02 pm

This code turns the green LED ON and the others OFF if less than 3 seconds has elapsed:
Code: Select all | TOGGLE FULL SIZE
  if (currentMillis - previousMillis < interval3s) {
  digitalWrite(NSGRN, HIGH);
  digitalWrite(NSRED, LOW);   
  digitalWrite(NSYEL, LOW);
  }

This code turns the yellow LED ON and the others OFF if greater than 3 seconds has elapsed.
Code: Select all | TOGGLE FULL SIZE
  if (currentMillis - previousMillis >= interval3s) {
  digitalWrite(NSRED, LOW);
  digitalWrite(NSGRN, LOW);   
  digitalWrite(NSYEL, HIGH);
  }

This code turns the red LED ON and the others OFF if greater than 5 seconds has elapsed.
Code: Select all | TOGGLE FULL SIZE
  if (currentMillis - previousMillis >= (interval2s + interval3s)) {
  digitalWrite(NSGRN, LOW);
  digitalWrite(NSYEL, LOW);
  digitalWrite(NSRED, HIGH);
  }


Note that 5 seconds is also greater than 3 seconds. So those second two 'if' statements will both be true after 5 seconds has elapsed. So on each pass through the loop it will be rapidly alternating between the red and yellow LEDs - so they will both light up somewhat dimmed.

I am using 1k resistors.

That should keep current to a safe level.

adafruit_support_bill
 
Posts: 77845
Joined: Sat Feb 07, 2009 10:11 am

Re: strange LED behaviour

by Rcayot on Sun Feb 23, 2020 6:46 pm

Okay. That makes sense. I can make the condition such that it will not include both loops as true.

Roger

Rcayot
 
Posts: 36
Joined: Sat Feb 08, 2020 6:48 pm

Re: strange LED behaviour

by kcl1s on Sun Feb 23, 2020 7:20 pm

Rcayot wrote:Okay. That makes sense. I can make the condition such that it will not include both loops as true.

Roger

Roger,
One way of doing this is with the if/else if statements https://www.arduino.cc/reference/en/language/structure/control-structure/else/ You would want to start with your largest interval at the top then work down the list.

if >reset time
reset
else if >red time
light red
else if >yellow time
light yellow
else light green

Only one statement would get executed each time.

Fellow hobbyist
Keith

kcl1s
 
Posts: 1511
Joined: Tue Aug 30, 2016 12:06 pm

Re: strange LED behaviour

by Rcayot on Tue Feb 25, 2020 12:07 pm

Keith,

Thanks, great suggestion! Right now things are working, but they are cycling through each loop quite a bit, doing it the other way around will help.

See my other question posted today 2/25

Roger

Rcayot
 
Posts: 36
Joined: Sat Feb 08, 2020 6:48 pm

Please be positive and constructive with your questions and comments.