Basic Project: Blinking and Fading at the same time

Hello guys!
I am Giovanny, and i’m new in Arduino, but not in electronics. But anyway during last days i am really enjoying it (Arduino). Now i am in a challenge, during the last three days and i really can’t finish without help.
The circuit is very simple. I want to wire two LEDs in my breadboard. One must blink, another fade. Until here all okay, and easy. Its speed should be controlled by a potentiometer (I used A10K). And the principal circuit function: Both should oscillate in the same speed. I know that one are blinking and another fading, but don’t matter, it should be in the same rhythm. I already tried a lot of things, codes and different ways to wire, and no one gave me a good result, so i will give the most simple information possible because don’t want to turn the things confused :wink: . Here is a little wiring schematic that i did for show how i am wiring it. Even if i use different potentiometers, one interferes in the another. If i put a pot for fade speed and another for blink speed, the fade pot changes the blink too, and the same occurs inversely, you got it? I wired a pot in two different analog inputs for try to get two separated signals for try to solve this problems using math functions and applying the results in only one analog input, but i really don’t know how to do it in Arduino. And anyway the ports is not working totally separately. There are some interference.
Its my wiring diagram:

It’s my code:

int PinBlk = 5; 
int PinFad = 9; 
int BlkWai = 0;
const int BlkSpd = 0;
const int FadSpd = 1;
int FadWai = 0;
int FadShine = 0; 
int fadeAmount = 5; 

void setup() {
  pinMode(PinFad, OUTPUT);
  pinMode(PinBlk, OUTPUT);
}

void loop() {
FunPots();
FunBlk();
FunFad();
}

int FunPots(){ 
    BlkWai = analogRead(BlkSpd); 
    FadWai = analogRead(FadSpd); 
}

int FunBlk(){
  digitalWrite(PinBlk, HIGH);
  delay(BlkWai); 
  digitalWrite(PinBlk, LOW);
  delay(BlkWai); 
}

int FunFad(){ 
  analogWrite(PinFad, FadShine); 
  FadShine = FadShine + fadeAmount; 
  if (FadShine <= 0 || FadShine >= 255) { 
    fadeAmount = -fadeAmount;
  }
  delay(FadWai); 

}

Thank you all for helping! Good day!

Can you do one or the other without using delay()?

-jim lee

Better still, do both without using delay()

See Several things at the same time

Hey guys, thank you a lot for replying. I really did not know that is possible blink without delay. Haha i will see it, and let you know if works for me!

@UKHeliBob One step at a time weed hopper.

-jim lee

Hey guys, and how can i change the "const long interval = 1000;" using a pot even it being before the loop function?

GiovannyS10: Hey guys, and how can i change the "const long interval = 1000;" using a pot even it being before the loop function?

You can't because it's a const. Take const out and you can assign values to interval anywhere in the code.

Like i thought... So its really a problem, i should control it using a pot. :(

There is not any other way to control the speed of the two different leds using the same pot in a independent way?

As I said, remove the const keyword.

sterretje But the "unsigned long previousMillis = 0;const long interval = 1000;" should be declared in the scope, and the potentiometer's analogRead in the loop... It will not work. Because if i assing a value to interval in loop it just not work. I am sorry if i really asking an easy thing. I am beginner in Arduino :(

if i assing a value to interval in loop it just not work

Show us what you tried.

Once you get past the pot issue, consider the possibility of using PWMfor both LEDs. A blink is simply jumping between two brightness values, while fading is a ramp between values over time.

This sounds harder, it will help in the long term... If you use the 'both' PWM idea, it can make the code shorter & simpler - because you use the same methods to control both LEDs, just with a different transition speed.

UKHeliBob: Show us what you tried.

Hi, i just tried a lot of things. So many that i can't count. The only one that worked better was using this code below:

int FunBlink(){ 
 int pot_Read;
 int delay_in_ms;
 int value = LOW;
 long lastTime = 0;
 int update_value;

 while(1)
 {
   delay(1); 
   
   pot_Read=analogRead(0);
   delay_in_ms=map(pot_Read,0,1023,100,2000); //Our pot will select from 200ms to 2000ms

   if (millis() > lastTime + long(delay_in_ms)) 
   {
     if (value == LOW)
       value = HIGH;
     else
     value = LOW;
     digitalWrite(PinSqr, value);
     lastTime=millis();
   }
 }
}

This one works very well, but when i use it my fade function not work. The LED continues off. I tried to change the function order... Nothing happened. I tried to use this code without use delay too. Continued working, but without the fade...

lastchancename: Once you get past the pot issue, consider the possibility of using PWMfor both LEDs. A blink is simply jumping between two brightness values, while fading is a ramp between values over time.

This sounds harder, it will help in the long term... If you use the 'both' PWM idea, it can make the code shorter & simpler - because you use the same methods to control both LEDs, just with a different transition speed.

It seems interesting. I only want the both LEDs changing at the same speed. The both will not be on in every time, you only will can choose if it will blink or fade. It can be made by pressing a button, or in another form after the AtMega, in breadboard. I only just want both in the same speed now. Anyway i just enjoyed your idea, but like i said, i am a beginner. This is my first project, and i really don't know how to do what you are saying works.