Pages: [1]   Go Down
Author Topic: Operating an LED with a potentiometer  (Read 571 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
  }
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 24, 2011, 05:58:21 am by AWOL » Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lots.
How about a range test.
If the pot is less than 512, then map one range, else map a different range.
Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I made it work with two if conditions
One "if" and an "else" should have done the trick.
Logged

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

Pages: [1]   Go Up
Jump to: