Go Down

Topic: Two LEDs controlled with potentiometer. One to blink fast/ other slow (Read 166 times) previous topic - next topic

blindstrom

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
  }

PaulRB

+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.

UKHeliBob

Code: [Select]

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

Check the order in which you have these 2 lines
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

blindstrom

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.

Paul__B

Of course there is.  :smiley-roll:

Look/ search for a topic called "Blink without delay".  "delay()" is not useful in "real world" code.

UKHeliBob

Quote
Is there any way to run them simultaneously?
See Using millis() for timing.  A beginners guide,   Several things at the same time and look at the BlinkWithoutDelay example in the IDE.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up