Go Down

Topic: Using Potentiometer to Control LED Blink Speed (Read 4346 times) previous topic - next topic

Jeremy1998

Apr 19, 2010, 02:21 am Last Edit: Apr 19, 2010, 02:36 am by Jeremy1998 Reason: 1
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: [Select]
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....

Jeremy1998

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?

Parker Smith

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.

WRonX

#3
Apr 21, 2010, 02:28 pm Last Edit: Apr 21, 2010, 02:30 pm by WRonX Reason: 1
Try reading the actual value of potentiometer in a loop. Something like this:

Code: [Select]

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 :)

pekkaa

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

Code: [Select]

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


WRonX

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


WRonX

Can you explain, when and where it freezes?


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy