Go Down

Topic: Rolling through LED's with a potentiometer (Read 603 times) previous topic - next topic

emooney

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: [Select]

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

dxw00d

#1
Mar 13, 2012, 03:22 pm Last Edit: Mar 13, 2012, 03:25 pm by dxw00d Reason: 1
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

AWOL

How is your pot wired?
Have you looked at the "map" function?
"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.

emooney

Thanks dxw00d, forgot about that! My setup() now looks like this:
Code: [Select]

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?

PaulS

Code: [Select]
  Serial.begin(9600);
  pinMode(1, OUTPUT);

Take a close look at your board. What does it say next to pin 1?

AWOL

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

emooney

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


Go Up