Go Down

### Topic: Psuedo Random 4 LED twinkler (Read 10314 times)previous topic - next topic

#### SargentSeven

##### Dec 28, 2013, 12:12 amLast Edit: Dec 29, 2013, 12:15 am by SargentSeven Reason: 1
Hi there, First time posting a project up here.

I have designed and coded a circuit for the ATTiny85 that Randomly blinks at random durations 1 of 4 LEDs.
I am only using 2 of the 6 pins available on the chip by using the Tri state function of the GPIO pins.
(in plain English, I made the two pins do 3 different things, Sink, source, and Open (or HI-Z))

I programmed everything with the MIT method using an Uno as an ICSP.
The relevant part of this project is the code. I'm not sure if anyone has posted anything similar but I pretty much did this without much reference to anything else.

here is the code.
Code: [Select]
`/*Code by Nick Tracy 2013 May contain code parts from other programs, all credit goes to those who helped. Designed around the ATTiny85 @ 8 Mhz with internal oscillator, accuracy is not important here and is actually beneficial. This code uses a random number generator to simulate a twinkling effect on 4 different outputs. Used in a decoration for the holidays, can be used for just about anything really. it uses 4 LEDs connected to 2 pins (3 and 4 for ATTiny85) that are Tri-stated to provide a HIGH, LOW, and High Impedance output. the LEDs are connected in a sink/source set up on each pin. when the pin mode is changed to the third state the LEDs on that pin are off. *///Use Define so that not a lot of memory is wasted.#define LED1 3#define LED2 4//long for random number variableslong rand1;long rand2;long rand3;void setup() {                  pinMode(LED1, INPUT_PULLUP);  pinMode(LED2, INPUT_PULLUP);  //Generate Random numbers seed  randomSeed(12345612);  }void loop() {  rand1 = random(1,5); //Used for selecting which LED to fire.  rand2 = random(5,20); //Used to determine delay for On.  rand3 = random(50,300); //Used to determine delay for Off.  //Switch case for selecting the appropriate LED  //Fires one Random LED at a time each loop at a random duration On and Off.  switch (rand1){  case 1:    pinMode(LED1, OUTPUT); //set pin mode to output    pinMode(LED2, INPUT_PULLUP); //makes sure other output is off    digitalWrite(LED1, HIGH); //turn on led number 1 of 4    delay(rand2); //delay for On duration    pinMode(LED1, INPUT_PULLUP); //turn off the output    delay(rand3); //delay before next cycle    break;  case 2:    pinMode(LED1, OUTPUT); //set pin mode to output    pinMode(LED2, INPUT_PULLUP); //makes sure other output is off    digitalWrite(LED1, LOW); //turn on led number 2 of 4    delay(rand2); //delay for On duration    pinMode(LED1, INPUT_PULLUP); //turn off the output    delay(rand3); //delay before next cycle    break;  case 3:    pinMode(LED2, OUTPUT); //set pin mode to output    pinMode(LED1, INPUT_PULLUP); //makes sure other output is off    digitalWrite(LED2, HIGH); //turn on led number 3 of 4    delay(rand2); //delay for On duration    pinMode(LED2, INPUT_PULLUP); //turn off the output    delay(rand3); //delay before next cycle        break;  case 4:    pinMode(LED2, OUTPUT); //set pin mode to output    pinMode(LED1, INPUT_PULLUP); //makes sure other output is off    digitalWrite(LED2, LOW); //turn on led number 4 of 4    delay(rand2); //delay for On duration    pinMode(LED2, INPUT_PULLUP); //turn off the output    delay(rand3); //delay before next cycle        break;  default:    //If no LED is selected, turn off all LEDs for a random amount of time.    pinMode(LED1, INPUT_PULLUP);    pinMode(LED2, INPUT_PULLUP);    delay(rand2);  }}`

EDIT: The LEDs I used were White with a 3.2v drop and 20ma draw. but with different resistor values you can use any LED so long as you choose the correct current limiting resistor for your LED.

My only issue here is that two of the LEDs are dimly lit in the HI-Z state due to the internal pull up resistors. there is just enough current to light the LEDs I'm using. if anyone has a recommendation for that let me know.
Motivation comes in the form of knowing the wife is going to hurt me if I don't follow through with my projects after spending our entertainment budget on parts. XD

#1
##### Dec 28, 2013, 02:52 am
Quote
if anyone has a recommendation for that let me know.

Schematic?

#### SargentSeven

#2
##### Dec 28, 2013, 03:31 amLast Edit: Dec 28, 2013, 07:53 pm by SargentSeven Reason: 1

EDIT EDIT: Removed schematic due to error in wiring.
EDIT to the EDIT that I EDITED: New Fritzing diagrams with correct wiring. sorry

P.S. I'm refusing to use fritzing anymore, what a terrible program. going back to hand drawing.
Motivation comes in the form of knowing the wife is going to hurt me if I don't follow through with my projects after spending our entertainment budget on parts. XD

#### SargentSeven

#3
##### Dec 28, 2013, 03:33 am
This method could be used to expand the amount of leds. Just duplicate this to each pin and change the code accordingly by adding new case switch statements and changing the max number for the random function.
Motivation comes in the form of knowing the wife is going to hurt me if I don't follow through with my projects after spending our entertainment budget on parts. XD

#4
##### Dec 28, 2013, 04:04 am

Uh, how do you light any of the LEDs?  For example LED2 and LED4 have the cathodes connected to Vcc.  No matter what you do with pin 4 (physical pin 3) neither of those LEDs is going to conduct.

#### SargentSeven

#5
##### Dec 28, 2013, 04:08 amLast Edit: Dec 28, 2013, 04:38 am by SargentSeven Reason: 1

Uh, how do you light any of the LEDs?  For example LED2 and LED4 have the cathodes connected to Vcc.  No matter what you do with pin 4 (physical pin 3) neither of those LEDs is going to conduct.

Sorry I'm new to fritzing and wasn't paying attention to that, I was more concerned with getting the traces to look nice and kinda threw that up in a hurry.

on my breadboard they are hooked up correctly.
Motivation comes in the form of knowing the wife is going to hurt me if I don't follow through with my projects after spending our entertainment budget on parts. XD

#### fungus

#6
##### Dec 28, 2013, 07:31 pm
How about a schematic that fits on the screen?
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

#### SargentSeven

#7
##### Dec 28, 2013, 07:44 pm

How about a schematic that fits on the screen?

You can easily view the image by right clicking it and clicking "view image" it will open the full image in a new window. I won't change the resolution of the image because it may lower the quality, not my fault the arduino forum can't properly handle image attachments like every other forum software out there.
Motivation comes in the form of knowing the wife is going to hurt me if I don't follow through with my projects after spending our entertainment budget on parts. XD

#### SargentSeven

#8
##### Dec 28, 2013, 07:51 pm
and why is there so much interest in the schematic? there isn't anything there besides 2 pins and 4 leds with resistors. the main focus of this project was the code.

its as simple as connecting it like this

| ATtiny85 |
Physical pin 2---------(a)LED1(c)-------100ohm--------Common -
Physical pin 2---------(c)LED2(a)-------100ohm--------Positive 5v
Physical pin 3---------(a)LED3(c)-------100ohm--------Common -
Physical pin 3---------(c)LED4(a)-------100ohm--------Positive 5v
4 to ground
8 to Vcc 5v
very simple.
Motivation comes in the form of knowing the wife is going to hurt me if I don't follow through with my projects after spending our entertainment budget on parts. XD

#9
##### Dec 28, 2013, 10:28 pm
and why is there so much interest in the schematic?

Two reasons...

1. I assumed you are wiring the LEDs the way you are in fact wiring them.  If the forward voltage was close to 2.5V it could explain the symptom.  In your first post you failed to mention the fact that you are using white / forward voltage = 3.2V LEDs.  By asking for the schematic I can validate my assumption and confirm the forward voltage is high enough for the circuit to work.

2. Anyone new to Arduinos / electronics who wants to build a similar device is going to have trouble without a schematic.  In addition, they would be rather disappointed if they had used red LEDs instead of white.  By providing details you are helping future adventurers.

#10
##### Dec 28, 2013, 10:31 pm

Now, back to the dim LEDs...

My only issue here is that two of the LEDs are dimly lit in the HI-Z state due to the internal pull up resistors. there is just enough current to light the LEDs I'm using. if anyone has a recommendation for that let me know.

Change INPUT_PULLUP to INPUT.  All lines like this...

[font=Courier New]  pinMode(LED2, INPUT_PULLUP); //makes sure other output is off[/font]

...become this...

[font=Courier New]  pinMode(LED2, INPUT); //makes sure other output is off[/font]

#### SargentSeven

#11
##### Dec 28, 2013, 11:57 pmLast Edit: Dec 29, 2013, 12:08 am by SargentSeven Reason: 1

Change INPUT_PULLUP to INPUT.  All lines like this...

[font=Courier New]  pinMode(LED2, INPUT_PULLUP); //makes sure other output is off[/font]

...become this...

[font=Courier New]  pinMode(LED2, INPUT); //makes sure other output is off[/font]

The reason I used the internal pullup resistors is because i had read that with any CMOS chip, if you leave an input pin floating in this circuit, you could damage the chip. is this not true for the AVR chips?

here is my source for this information, in the comment section someone brought up this point about using internal pullups to prevent burning up the pins. http://www.neufeld.newton.ks.us/electronics/?p=151
Motivation comes in the form of knowing the wife is going to hurt me if I don't follow through with my projects after spending our entertainment budget on parts. XD

#### SargentSeven

#12
##### Dec 29, 2013, 12:04 am

1. I assumed you are wiring the LEDs the way you are in fact wiring them.  If the forward voltage was close to 2.5V it could explain the symptom.  In your first post you failed to mention the fact that you are using white / forward voltage = 3.2V LEDs.  By asking for the schematic I can validate my assumption and confirm the forward voltage is high enough for the circuit to work.

I tried this with other LEDs besides red and i can confirm that it will work with most leds, for the red all i had to do was change the resistor to 150ohm and it worked the same.

2. Anyone new to Arduinos / electronics who wants to build a similar device is going to have trouble without a schematic.  In addition, they would be rather disappointed if they had used red LEDs instead of white.  By providing details you are helping future adventurers.

I apologize, i kind of assumed that LEDs and current limiting resistors were pretty entry level knowledge. if someone is trying to get into this project and doesn't know at least that much then they should be starting on something simpler IMO.
Motivation comes in the form of knowing the wife is going to hurt me if I don't follow through with my projects after spending our entertainment budget on parts. XD

#13
##### Dec 29, 2013, 12:14 amLast Edit: Dec 29, 2013, 12:18 am by Coding Badly Reason: 1

This appears to be a good description of the problem...
http://www.ti.com/lit/an/scba004c/scba004c.pdf
I'm too lazy to read it thoroughly but from a cursory glance I gather the problem has to do with overheating caused by an input floating in the linear region of the transistor or from excessive switching.  Neither of those is a problem for an AVR processor.  Switching does burn energy but nowhere near enough to cause overheating problems.

In other words, floating inputs are most certainly not a problem for AVR processors.  That's how they are initialized after a reset or power-up.