Combining Arduino sketches. Blink and fade.

Thread Starter

jgreene44

Joined Dec 8, 2016
108
Ok Thanks in advance for being here.

What im trying to do is about like the title says. Simply combine the two. I realize its not that simple. I have managed to get the
two codes too work together but the fade command has to wait on the blink command so I know thats not what i want. Would be really appreciated if someone would spend a minute and post an example of the two codes working together in a way that they have there own timing. this is what i came up with. is this the best i can expect to do? im not sure. guessing not. Also for some reason. pin 10 is only reading .15v where as pin 9 is showing 2.5v. The led does blink though.
C:
int bled = 10;
int led = 9;        
int brightness = 0;  
int fadeAmount = 5;  


void setup()

{ pinMode(led, OUTPUT);
  pinMode(bled, OUTPUT); }


void loop()

{ analogWrite(led, brightness);
 
  brightness = brightness + fadeAmount;
 
  if (brightness <= 0 || brightness >= 255)
    {fadeAmount = -fadeAmount;}
    delay(60);  
   
  if (brightness <= 0)
    analogWrite(bled, HIGH);  
 
  if (brightness >= 255)
    analogWrite(bled, LOW);}
 
Last edited by a moderator:

RayB

Joined Apr 3, 2011
31
but the fade command has to wait on the blink command so I know thats not what i want.
Most microcontrollers can only manage a single thread at a time ... so the code is worked downwards from the top to the bottom with the logic in each line being active at the moment. So, with blink 'n fade, you must be a little more creative. You can use one of the free timers in the AVR uC to control the fading through PWM, such that the fading is not tied to the existing sketch but hardware. Then with your sketch, you could turn on/off the LED power source. The easiest way to accomplish this (never tried but sounds reasonable), is to have the LED and current limiting resistor between two separate output pins... The first pin will be PWM to manage the fade and the second pin will be simply ON or OFF to mange the blink. Remember to use a resistor value that will keep the max current below what two I/O pins can manage (2x the single pin current.)

As stated, I have not tried the approach above and I'm off in ESP land these days so my workstation is not setup to do AVR experiments at the moment, but I have managed to fade a tri-color LED with a tiny85, so I think the trick will work.
T85 project: https://www.hackster.io/rayburne/hot-yet-8d097d
 

mcgyvr

Joined Oct 15, 2009
5,394
jgreene44 can you explain in words exactly what you would like to happen..
You have 2 LEDs (hopefully with a resistor on each..)
Now do you want one to be fading up and down while the other is blinking? Is there any specific timing you want (blink rate/duration?)
 

Thread Starter

jgreene44

Joined Dec 8, 2016
108
this is mainly an exercise. so timing is not so crucial as the knowledge to do so. must be done with and thru a sketch rayb.thanks already.
 

Thread Starter

jgreene44

Joined Dec 8, 2016
108
Just an update if anyone is still interested. I found the answer in the arduino example file "blink without delay". Fits perfect.
 
Top