Timer with leds and potentiometers

Hi,how to control two leds with four potentiometer(one led two potentiometers). Potentiometers are used for delay to led LOW and the other one to led HIGH. First i tried with two loops but that can't work... Then I find the blinking without delay,tried but not fully understand... If you wan't more information how I want to work ask. Here is the code with delay ...

int potPin = 1;    // select the input pin for the potentiometer
int pot1Pin = 2;// select the input pin for the potentiometer
int pot2Pin = 3;    // select the input pin for the potentiometer
int pot3Pin = 4;// select the input pin for the potentiometer
int ledPin = 2;   // select the pin for the LED
int ledPin1 = 3;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor
int val1 = 0;       // variable to store the value coming from the sensor
int val2 = 0;       // variable to store the value coming from the sensor
int val3 = 0;       // variable to store the value coming from the sensor
unsigned long time;
void setup() {
 pinMode(ledPin, OUTPUT);  // declare the ledPin as an output
 pinMode(ledPin1, OUTPUT);  // declare the ledPin as an OUTPUT
}

void loop() {
 unsigned long var = analogRead(potPin)*60UL;    // read the value from the sensor
 unsigned long var1 = analogRead(pot1Pin)*60UL;   // read the value from the sensor
 unsigned long var2 = analogRead(potPin)*60UL;    // read the value from the sensor
 unsigned long var3 = analogRead(pot1Pin)*60UL;   // read the value from the sensor
 digitalWrite(ledPin, LOW );  // turn the ledPin on
 delay(var);                  // stop the program for some time
 digitalWrite(ledPin, HIGH);   // turn the ledPin off
 delay(var1);                  // stop the program for some time
 digitalWrite(ledPin1, LOW );  // turn the ledPin on
 delay(var2);                  // stop the program for some time
 digitalWrite(ledPin1, HIGH);   // turn the ledPin off
 delay(var3);                  // stop the program for some time
}

It is working but I want without delay. Sorry for my bad English,thank you ! :)

You mentioned something about seeing the demo "blink without delay()". Learning that demo and incorporating its elements into your code will fix your sketch. Here's why:

When you call "delay()", you halt the WHOLE code. That means, all of the LEDs hold their state for varx milliseconds. This will cause a great deal of error in your program.

I suggest you take another look at the blink without delay demo and post any questions you have about the demo here. Then, you will have enough intuition to fix your sketch.

I will modify this code :

// Which pins are connected to which LED
const byte greenLED = 2;
const byte redLED = 3;
const byte pot1Pin = 1;
const byte pot2Pin= 2;
const byte pot3Pin= 3;
const byte pot4Pin= 4;

// Time periods of blinks in milliseconds (1000 to a second).
const unsigned long greenLEDinterval = 2000;
const unsigned long redLEDinterval = 5000;

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

void setup () 
 {
 pinMode (greenLED, OUTPUT);
 pinMode (redLED, OUTPUT);
 greenLEDtimer = millis ();
 redLEDtimer = millis ();
 }  // end of setup

void toggleGreenLED ()
 {
  if (digitalRead (greenLED) == LOW)
     digitalWrite (greenLED, HIGH);
  else
     digitalWrite (greenLED, LOW);

 // remember when we toggled it
 greenLEDtimer = millis ();  
 }  // end of toggleGreenLED

void toggleRedLED ()
 {
  if (digitalRead (redLED) == LOW)
     digitalWrite (redLED, HIGH);
  else
     digitalWrite (redLED, LOW);

 // remember when we toggled it
 redLEDtimer = millis ();  
 }  // end of toggleRedLED

void loop ()
 {

 // Handling the blink of one LED.
 if ( (millis () - greenLEDtimer) >= greenLEDinterval)
    toggleGreenLED ();

 // The other LED is controlled the same way. Repeat for more LEDs
 if ( (millis () - redLEDtimer) >= redLEDinterval) 
   toggleRedLED ();

/* Other code that needs to execute goes here.
  It will be called many thousand times per second because the above code
  does not wait for the LED blink interval to finish. */

}  // end of loop

But I have a question... How can I " const unsigned long greenLEDinterval = 2000; "change to read from potentiometer not to write the time ? I have tried "const unsigned long greenLEDinterval = analogRead(pot1Pin); " but not working... Please help.

Don't use 'const'

.

Can I use int ? How can I " unsigned long greenLEDinterval = 2000; "change to read from potentiometer not to write the time ? I have tried "unsigned long greenLEDinterval = analogRead(pot1Pin); " but not working...

don't put that in the variables declaration. you need to read that either in the setup() if you want to read that only once for the duration of the sketch or in the loop if you want to change it while the program is running.

you can only define constants stuff at compilation time.

Can I use int ?

You could, but the results would not be satisfactory.

How can I " unsigned long greenLEDinterval = 2000; "change to read from potentiometer not to write the time ?

unsigned long greenLEDinterval = 2000;

void loop()
{
   greenLEDInterval = analogRead(someAnalogPin);

I have tried "unsigned long greenLEDinterval = analogRead(pot1Pin); " but not working...

"But not working" is just too lame to bother responding to. The code was placed somewhere in a sketch. The code did something. You expected it to do something else.

Well, which of those things did you tell us? NONE of them. So, all we can offer is some phony sympathy.

and please edit all your posts above to use the code tags...

J-M-L: and please edit all your posts above to use the code tags...

Ok. I was successfully to control each led one potentiometer but i want to control one led with two potentiometers... Here is the code :

int greenLED = 2;
int redLED = 3;
int potPin = 1;
int pot1Pin= 2;
int pot2Pin = 3;
int pot3Pin= 4;
int val = 0;       // variable to store the value coming from the sensor
int val1 = 0;       // variable to store the value coming from the sensor
int val2 = 0;       // variable to store the value coming from the sensor
int val3 = 0;       // variable to store the value coming from the sensor

// Time periods of blinks in milliseconds (1000 to a second).
unsigned long greenONLEDinterval = analogRead(potPin);
unsigned long redONLEDinterval = analogRead(pot3Pin);

// Variable holding the timer value so far. One for each "Timer"
unsigned long greenLEDtimer;
unsigned long redLEDtimer;

void setup () 
  {
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  greenLEDtimer = millis ();
  redLEDtimer = millis ();
  }  // end of setup

void toggleGreenLED ()
  {
   if (digitalRead (greenLED) == LOW)
      digitalWrite (greenLED, HIGH);
   else
      digitalWrite (greenLED, LOW);

  // remember when we toggled it
  greenLEDtimer = millis ();  
  }  // end of toggleGreenLED

void toggleRedLED ()
  {
   if (digitalRead (redLED) == LOW)
      digitalWrite (redLED, HIGH);
   else
      digitalWrite (redLED, LOW);

  // remember when we toggled it
  redLEDtimer = millis ();  
  }  // end of toggleRedLED

void loop ()
  {
unsigned long greenLEDinterval = analogRead(pot1Pin);
unsigned long redLEDinterval = analogRead(pot3Pin);
  // Handling the blink of one LED.
  if ( (millis () - greenLEDtimer) >= greenLEDinterval)
     toggleGreenLED ();

  // The other LED is controlled the same way. Repeat for more LEDs
  if ( (millis () - redLEDtimer) >= redLEDinterval) 
    toggleRedLED ();


}

but i want to control one led with two potentiometers…

You REALLY need to define some requirements. What are the values read from the two potentiometers to be used for?

thanks for making the effort to add the code tags! much better that way

I was successfully to control each led one potentiometer but i want to control one led with two potentiometers…

so what do you want the 2 potentiometers to do? that’s what you need to code :wink:

PaulS: You REALLY need to define some requirements. What are the values read from the two potentiometers to be used for?

I want that one control while the LED off and the other onewhile the LED is on...

so green and red will blink the same way - right? or do you need 4 potentiometers (which is what you declared)

I want that one control while the LED off and the other onewhile the LED is on...

So, one sets the ON interval and one sets the OFF interval. That means that you need two interval variables, and you need to compare now minus then to the correct interval before calling toggleGreenLED().

PaulS: So, one sets the ON interval and one sets the OFF interval. That means that you need two interval variables, and you need to compare now minus then to the correct interval before calling toggleGreenLED().

Paul you're right,but not understand what to do :D

but not understand what to do

You need two variables. Have you defined them? What are they called? How are valued?

You currently use the same value for the on time and the off time. Do you understand where you are doing that?

Try, instead of writing code, writing out, in excruciating detail, how YOU would use the two intervals and the current time to turn the LED off if it is on, and on if it is off.

Once you KNOW what you want to do, writing the code is trivial.

I quickly hacked your code to do this - it’s untested but you’ll get the idea

of course there is an issue in the code because pin 2 is used for 2 things (same for pin 3)… but that’s in your code above…

const byte greenLED = [color=red]2[/color];
const byte pot1Pin = [color=red]2[/color];

so you need to define your PINs for each component in the right way.

// control the green LED
const byte greenLED = 2;
const byte pot0Pin = 1;
const byte pot1Pin = 2; // THIS NEEDS TO BE CHANGED AS THIS IS ALSO THE PIN FOR greenLED
unsigned long greenONLEDinterval;  // Time periods of ON blinks in milliseconds(1000 to a second).
unsigned long greenOFFLEDinterval; // Time periods of OFF blinks in milliseconds(1000 to a second).
unsigned long greenLEDtimer;// Variable holding the timer value at the time of state change


// control the red LED
const byte redLED = 3;
const byte pot2Pin = 3;// THIS NEEDS TO BE CHANGED AS THIS IS ALSO THE PIN FOR redLED
const byte pot3Pin = 4;
unsigned long redONLEDinterval;  // Time periods of ON blinks in milliseconds(1000 to a second).
unsigned long redOFFLEDinterval; // Time periods of OFF blinks in milliseconds(1000 to a second).
unsigned long redLEDtimer;// Variable holding the timer value at the time of state change

// ---------------------------------------------------------------
// setup()   - initialize everything
// ---------------------------------------------------------------
void setup()
{
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  greenLEDtimer = millis();
  redLEDtimer = millis();
}  // end of setup

// ---------------------------------------------------------------
// toggleGreenLED() - handle the blinking of the green led
// ---------------------------------------------------------------

void toggleGreenLED()
{
  greenONLEDinterval = analogRead(pot0Pin);
  greenOFFLEDinterval = analogRead(pot1Pin);

  if (digitalRead(greenLED) == LOW) {
    // check if we need to turn it on
    if ((millis() - greenLEDtimer) >= greenOFFLEDinterval) {
      digitalWrite(greenLED, HIGH);
      greenLEDtimer = millis();
    }
  } else {
    // check if we need to turn if off
    if ((millis() - greenLEDtimer) >= greenONLEDinterval) {
      digitalWrite(greenLED, LOW);
      greenLEDtimer = millis();
    }
  }
}  // end of toggleGreenLED

// ---------------------------------------------------------------
// toggleRedLED() - handle the blinking of the red led
// ---------------------------------------------------------------

void toggleRedLED()
{
  redONLEDinterval = analogRead(pot2Pin);
  redOFFLEDinterval = analogRead(pot3Pin);

  if (digitalRead(redLED) == LOW) {
    // check if we need to turn it on
    if ((millis() - redLEDtimer) >= redOFFLEDinterval) {
      digitalWrite(redLED, HIGH);
      greenLEDtimer = millis();
    }
  } else {
    // check if we need to turn if off
    if ((millis() - redLEDtimer) >= redONLEDinterval) {
      digitalWrite(redLED, LOW);
      greenLEDtimer = millis();
    }
  }
}  // end of toggleGreenLED

// ---------------------------------------------------------------
// loop() - keep going
// ---------------------------------------------------------------

void loop()
{
  toggleGreenLED();
  toggleRedLED();
}

of course there is an issue in the code because pin 2 is used for 2 things (same for pin 3)... but that's in your code above...

const byte greenLED = 2; const byte pot1Pin = 2;

One pin 2 refers to analog pin 2. The other refers to digital pin 2. They are NOT the same pin. They are not even on the same side of the board.

PaulS: One pin 2 refers to analog pin 2. The other refers to digital pin 2. They are NOT the same pin. They are not even on the same side of the board.

:) LOL - you blew my cunning plan... that was actually for the OP to dig into this. I think I wanted him to state that A2 and A3 (+ A0 and A1) would have been more suited for code readability and make sure that he does not just adopt code without reading and understanding it.

I think I wanted him to state that A2 and A3 (+ A0 and A1) would have been more suited for code readability

But, they are not. A0, A1, etc. are aliases defined for use when using the analog pins as digital pins. I do not like seeing people use A0 in an analogRead() statement. The potentiometer is connected to analog pin 0, not digital pin 14.