Pages: [1]   Go Down
Author Topic: Rolling through LED's with a potentiometer  (Read 559 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I've got 5 LED's and a potentiometer. As I roll the pot, I want an LED to come on if the pot's value is within a certain range. All of my grounds are going to a common ground, LED's are in D1-D6, pot is in A0. I'm not getting any compile errors. The current behavior is on boot, LED 1 comes on and when I move the pot, LED 1 stays on and none of the others come on.
Any ideas?

Code:
void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  if(sensorValue < 205) {
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);     
  } else if(sensorValue > 205 && sensorValue < 410) {
    digitalWrite(1, LOW);
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW); 
  } else if(sensorValue > 409 && sensorValue < 615) {
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW); 
  } else if(sensorValue > 614 && sensorValue < 819) {
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW); 
  } else {
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);     
  }
  delay(100);
  Serial.println(sensorValue);
}
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't seem to be setting the pinMode(). Digital pins default to inputs. What you are doing is toggling the internal pull-up resistors.

http://arduino.cc/en/Reference/PinMode
« Last Edit: March 13, 2012, 09:25:44 am by dxw00d » Logged

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

How is your pot wired?
Have you looked at the "map" function?
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: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks dxw00d, forgot about that! My setup() now looks like this:
Code:
void setup() {
  Serial.begin(9600);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
}
When I turn the pot, the other LED's turn on, however, my first LED is still constantly on. Not sure why because I'm setting it to LOW in the if statements.

AWOL - I have seen the map function. Is that required for this example to work?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.begin(9600);
  pinMode(1, OUTPUT);
Take a close look at your board. What does it say next to pin 1?
Logged

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

Quote
AWOL - I have seen the map function. Is that required for this example to work?
It isn't required, but it could make your sketch a lot shorter.
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: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS - I'm doing this on an Ardweeny so I didn't see that 'tx' label that's right there on my mega. Thank you!
@AWOL - cool, I looked at the map method and I'll see how I can utilize this to my advantage. Shorter in the fact that I could minimize my if statements? I'll try to find some examples. Thanks again for your help.
Logged

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

nevermind AWOL; just found this: http://arduino.cc/en/Reference/Map
Logged

Pages: [1]   Go Up
Jump to: