Author Topic: 4-way traffic light  (Read 3728 times)

jazzlad

  • Newbie
  • *
  • Posts: 3
4-way traffic light
« on: November 07, 2016, 03:36:02 pm »
Hi all,

I've been trying to get a 4-way traffic light working for my 4-year-old (scaled for his small cars running on a Digispark ATTINY85 board), but something isn't working right.  I thought it might be my board, but I have tried 2 boards now & both act the same way.  In a nutshell, it's 6 LEDs (12 technically, but N/S and E/W are the same) that cycle through a simple rotation.

Here's the code:
Code: [Select]
void setup() {
  pinMode(0, OUTPUT); // Green
  pinMode(1, OUTPUT); // Yellow
  pinMode(2, OUTPUT); // Red
  pinMode(3, OUTPUT); // Green
  pinMode(4, OUTPUT); // Yellow
  pinMode(5, OUTPUT); // Red
}
void loop() {
  digitalWrite(2, HIGH);  // Opposing Red On
  digitalWrite(3, HIGH);  // Green On
  delay(4000);            // 4 Seconds
  digitalWrite(3, LOW);   // Green Off
  digitalWrite(4, HIGH);  // Yellow On
  delay(1500);            // 1.5 Seconds
  digitalWrite(4, LOW);   // Yellow Off
  digitalWrite(5, HIGH);  // Red On (overlap)
  delay(1000);            // 1 Second
  digitalWrite(5, LOW);   // Red Off
  digitalWrite(2, LOW);   // Opposing Red Off

  digitalWrite(5, HIGH);  // Opposing Red On
  digitalWrite(0, HIGH);  // Green On
  delay(4000);            // 4 Seconds
  digitalWrite(0, LOW);   // Green Off
  digitalWrite(1, HIGH);  // Yellow On
  delay(1500);            // 1.5 Seconds
  digitalWrite(1, LOW);   // Yellow Off
  digitalWrite(2, HIGH);  // Red On (overlap)
  delay(1000);            // 1 Second
  digitalWrite(2, LOW);   // Red Off
  digitalWrite(5, LOW);   // Opposing Red Off
}

If I only connect one set of 3 (or 6) LEDs, it's fine, and the green on 3 is ok, but the yellow on 4 does nothing & when I connect the red on 5 it seems to crash.  I'm using appropriate resisters (I'd have to look it up, I bought 100 when I bought my LEDs & my memory thinks they are 200ohm, but my memory isn't always reliable ;-) ) & the LEDs work on 0,1,2 that don't work on 3,4,5 (so it's not the wiring). 

Ideas?

jazzlad

  • Newbie
  • *
  • Posts: 3
Re: 4-way traffic light
« Reply #1 on: November 07, 2016, 07:46:00 pm »
I've been reading & thought this might be pertinent, but I'm not certain what it means, nor how to overcome it:

Quote
https://digistump.com/wiki/digispark/tutorials/basics

Is my understanding that P3 is resisted at 1.5kΩ and P5 is 3v instead of 5v?  Does this render both unusable for LEDs?  Please forgive my ignorance, I am quite new to these boards (and Arduinos, for that matter).  I chose this board for its size and that it appeared to have the requisite 6 outputs, but I'll look into getting a larger board if I have to (I have a couple UNOs, but they are way too big for this toy).

Thanks in advance for any advice you can give.

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: 4-way traffic light
« Reply #2 on: November 07, 2016, 08:35:45 pm »
P3 does have a pull up of 1.5K to 5V and yes P5 is 3v. This shouldn't cause any problems except that P5 at 3V with the same resistor as used on the other LEDs will drop the current to the LED lower. 5V / 200 = 25mA whereas 3V / 200 = 15mA which would cause the LED on P5 to be dimmer than the others.  Your code looks fine. Don't see anything obviously wrong. I lifted it directly, breadboarded 6 LEDs and it appears to be working.

How are you powering the Digispark? Don't know if having it connected via USB to the PC will cause any problems since P3 and P4 are used for USB communication but I would try testing it powered from another source. I have mine powered from charger that accepts USB and a USB cable that is male to female so I can plug the Digispark into one end of that cable.

Another thing to consider if you are indeed going to power the LEDs in pairs of twos as you said (i.e. N/S E/W), the Digispark according to this reference...
http://digistump.com/wiki/digispark/quickref
says the GPIO output is 20mA max per pin.

Finally, double check your wiring if all else fails.
« Last Edit: November 07, 2016, 09:40:00 pm by exeng »

exeng

  • Sr. Member
  • ****
  • Posts: 454
Re: 4-way traffic light
« Reply #3 on: November 08, 2016, 01:02:24 pm »
Ralf, Yep, thought that, that could be a problem for jazzlad but assumed he was using a real Digispark. If not, that would explain the "seems to crash" behavior when Pin 5 is connected to a path to GND. Good catch.

jazzlad

  • Newbie
  • *
  • Posts: 3
Re: 4-way traffic light
« Reply #4 on: November 09, 2016, 03:58:02 pm »
Thanks for all the advice, guys!   :)

Hansepansje

  • Newbie
  • *
  • Posts: 1
Re: 4-way traffic light
« Reply #5 on: December 01, 2016, 02:59:49 pm »
I experienced exactly the same issue: with P0 to P4 all individually connected a led via a 1k resistor to GND works fine, but as soon as I connect P5 to a led which is via a resistor connected to GND the program stops and my laptop complains the USB device is no longer recognized. The P5 reset explains this behavior.
Next try could be to connect all leds via a resistor to +5 volt and then turn on the leds by writing LOW to the ports. Would that allow me to use all 6 ports without running into reset issues again?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: 4-way traffic light
« Reply #6 on: December 02, 2016, 12:47:15 am »
I believe it may be possible to use P5 as a I/O pin (or at least an input pin) without setting the RSTDSBL fuse, but the catch will probably be as soon as you do a digitalWrite(5,LOW) the Digispark will reset (since you haven't disabled the reset function of that pin)! :-O