Using Potentiometer to Control LED Blink Speed

First off, I must thank sciguy for helping me on the code.

This is a cimple bit of code that allows a pot to control an led's flash speed. Here is the code for it:

int potpin = 0;  // analog pin used to connect the potentiometer
int ledpin = 13;
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
pinMode(ledpin, OUTPUT); 
} 
 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 500, 5);     // scale it to use it with the servo (value between 0 and 180) 
  digitalWrite(ledpin, HIGH);
  delay(val);                           // waits for the servo to get there 
  digitalWrite(ledpin, LOW);
  delay(val);
}

And if you couldnt tell, the code is a modified copy of the knob servo code. That is the reason for the random describing side notes... i was to lazy to finish or delete them.

There is also a making/using vid coming soon....

The only issue I had with the code is that it has to finish the on / off cycle before the speed change is applied. Any help on finxing it?

You could throw in another val = analogRead(potpin) and val = map(val, 0,1023, 500, 5); right after it sets the LED pin high, that way the delay is now based on the new value. I guess you could add it in again after the LED pin is set low if you wanted even more precision.

Try reading the actual value of potentiometer in a loop. Something like this:

int potpin = 0;  
int ledpin = 13;
int val;    
int lastChange; // the time of changing pin state

void setup()
{
  pinMode(ledpin, OUTPUT);
  val = map(analogRead(potpin), 0, 1023, 500, 5);  // actual value
}

void loop()
{
  digitalWrite(ledpin, HIGH);
  lastChange = millis();  // ledpin state changed NOW
  while(lastChange + val <= millis())  // loop while there's time for it :)
  {
    val = map(analogRead(potpin), 0, 1023, 500, 5);  // read the value every time
  }

  // the same here:
  digitalWrite(ledpin, LOW);
  lastChange = millis();
  while(lastChange + val <= millis())
  {
    val = map(analogRead(potpin), 0, 1023, 500, 5);  
  }
}

I didn't check it, I don't have the access to IDE right now, but maybe it will work :slight_smile:

This is a slightly simplified version of WRonX's example. I haven't actually tested it.

int potpin = 0;  
int ledpin = 13;
int val;    
int lastChange; // the time of changing pin state
int state = HIGH;

void setup()
{
  pinMode(ledpin, OUTPUT);
  lastChange = millis();  // ledpin state changed NOW
  digitalWrite(ledpin, state);
}

void loop()
{
  val = map(analogRead(potpin), 0, 1023, 500, 5);  // read the value every time
  if (lastChange + val <= millis()) 
  {
       state = !state; // invert led's state
       lastChange = millis();  // ledpin state changed NOW
       digitalWrite(ledpin, state);
  }
  // you can add other functionality here ...
}

Stupid me! But yes, it should work and it takes less space :slight_smile:

it works, but it freezes ALOT

Can you explain, when and where it freezes?

Just when I turn it quick.