Go Down

Topic: Operating an LED with a potentiometer (Read 779 times) previous topic - next topic

palmtree

This is prob super easy, but as a noob, I'm stuck!

Here's what I want to do:

I want to map a potentiometer to an LED so that when the potmeter is at 0, the LED is at 0. When the potmeter is at 127, I want the LED to be at 1023. When the potmeter is at 255, I want the LED to have gone back down to 0.



I have read the articles on remap and multimap on the playground, but couldn' make any sense of them.

Hoping for swift replies as this is for a university project with a dead line fast approaching

Thanks!


below is the sketch I've written for controlling the LED, only it doesn't go back down to 0..


const int PotPin1 = 0;
const int LEDPin1 = 3;
int Pot1Value = 0;
int LED1Value = 0;



void setup() {
 
}

void loop() {
  Pot1Value = analogRead(PotPin1);
  LED1Value = map(Pot1Value, 0, 1023, 0, 120);
  analogWrite(LEDPin1, LED1Value);
  }

AWOL

#1
May 24, 2011, 12:48 pm Last Edit: May 24, 2011, 12:58 pm by AWOL Reason: 1
Quote
I want the LED to be at 1023. When the potmeter is at 255, I want the LED to have gone back down to 0.

I think you have analogRead and analogWrite ranges the wrong way around.
Have a look at the reference.
http://arduino.cc/en/Reference/AnalogRead
http://arduino.cc/en/Reference/AnalogWrite

Quote
a dead line fast approaching

It's OK, the Rapture got called-off.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

palmtree

Quote
I think you have analogRead and analogWrite ranges the wrong way around.
Have a look at the reference.

Yeah, I know, I mixed up the ranges. Thanks.

But correcting that made no difference. I want to have it set up so the potmeter and LED correspond to each other like this:

potmeter:  0, 512, 1023
LED PWM:  0, 255, 0

Any ideas?

Thanks

AWOL

Lots.
How about a range test.
If the pot is less than 512, then map one range, else map a different range.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

palmtree

Thanks! I made it work with two if conditions:D

AWOL

Quote
I made it work with two if conditions

One "if" and an "else" should have done the trick.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up