Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: blindstrom on May 16, 2019, 07:35 am

Title: Two LEDs controlled with potentiometer. One to blink fast/ other slow
Post by: blindstrom on May 16, 2019, 07:35 am
Hey i am a noob. Trying to get one led to blink fast whilst a second led to blink slow and when i turn the potentiometer the fast one will slow and the slow one will increase in speed.
Have Arduino Uno so i can't use the Scheduler.
Here is my code thus far. No errors but not doing what i would like it to.

Code: [Select]

int sensorPin = 0;    // The potentiometer is connected to
                      // analog pin 0
                     
int ledPin = 13;      // The LED is connected to digital pin 13
int ledPin2 = 12;      // The LED is connected to digital pin 12
int sensorValue;

void setup()
{
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() // this function runs repeatedly after setup() finishes
{
lights1();
lights2(); 
}

void lights1(){
  sensorValue = analogRead(sensorPin);   
  digitalWrite(ledPin, HIGH);     // Turn the LED on
  delay(sensorValue);             // Pause for sensorValue
  digitalWrite(ledPin, LOW);      // Turn the LED off
  delay(sensorValue);             // Pause for sensorValue
  }

void lights2(){
  int newSensorValue;
  newSensorValue = map(sensorValue, 0, 1023, 1023, 0);
  sensorValue = analogRead(sensorPin);   
  digitalWrite(ledPin2, HIGH);     // Turn the LED on
  delay(newSensorValue);             // Pause for sensorValue
  digitalWrite(ledPin2, LOW);      // Turn the LED off
  delay(newSensorValue);             // Pause for sensorValue
  }
Title: Re: Two LEDs controlled with potentiometer. One to blink fast/ other slow
Post by: PaulRB on May 16, 2019, 07:39 am
+1 Karma for using code tags on your first post. But you still made a noob mistake:
Quote
not doing what i would like it to
Think about how much that will help us to understand your problem.
Title: Re: Two LEDs controlled with potentiometer. One to blink fast/ other slow
Post by: UKHeliBob on May 16, 2019, 09:18 am
Code: [Select]

  newSensorValue = map(sensorValue, 0, 1023, 1023, 0);
  sensorValue = analogRead(sensorPin);   

Check the order in which you have these 2 lines
Title: Re: Two LEDs controlled with potentiometer. One to blink fast/ other slow
Post by: blindstrom on May 17, 2019, 12:14 pm
Thanks Heaps. That has kinda helped.

That's got it kind of right. The issue is both loops are running one after another instead of simultaneously. I want one to constantly blink fast and the other to constantly blink slow, the when i turn the potentiometer the fast one slows down whilst the slow one speeds up. Is there any way to run them simultaneously?

Thanks again.
Title: Re: Two LEDs controlled with potentiometer. One to blink fast/ other slow
Post by: Paul__B on May 17, 2019, 01:36 pm
Of course there is.  :smiley-roll:

Look/ search for a topic called "Blink without delay".  "delay()" is not useful in "real world" code.
Title: Re: Two LEDs controlled with potentiometer. One to blink fast/ other slow
Post by: UKHeliBob on May 17, 2019, 02:29 pm
Quote
Is there any way to run them simultaneously?
See Using millis() for timing.  A beginners guide (http://forum.arduino.cc/index.php?topic=503368.0),   Several things at the same time (http://forum.arduino.cc/index.php?topic=223286.0) and look at the BlinkWithoutDelay example in the IDE.