Pages: [1]   Go Down
Author Topic: Using Potentiometer to Control LED Blink Speed  (Read 3339 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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....
« Last Edit: April 18, 2010, 07:36:40 pm by Jeremy1998 » Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Siepraw, PL
Offline Offline
Newbie
*
Karma: 0
Posts: 45
No time, stroking Cat...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 smiley
« Last Edit: April 21, 2010, 07:30:40 am by WRonX » Logged

Finland
Offline Offline
Sr. Member
****
Karma: 1
Posts: 270
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 ...
}  
Logged

Siepraw, PL
Offline Offline
Newbie
*
Karma: 0
Posts: 45
No time, stroking Cat...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Stupid me! But yes, it should work and it takes less space smiley
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it works, but it freezes ALOT
Logged

Siepraw, PL
Offline Offline
Newbie
*
Karma: 0
Posts: 45
No time, stroking Cat...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you explain, when and where it freezes?
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 733
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just when I turn it quick.
Logged

Pages: [1]   Go Up
Jump to: