Go Down

### Topic: Analogue Sensor to Cycle Through LEDs (Read 644 times)previous topic - next topic

#### 311x311

##### Feb 10, 2013, 12:27 am
Hi,
I am trying to figure out a way to cycle through a number of LEDs one by one in sequence with analogRead value.

Basically, when there is a value above the threshold, one LED lights up.  This LED stays on until the next value is read.
When the next value is read, the currently lit LED turns off and then the next LED lights up.
This is repeated until the last LED is lit.
When another value is read at this point, the first LED should light up.  Then the whole cycle starts again.

Code: [Select]
`const int sensorPin = A0;const int threshold = 600;int sensorRead=0;   int state=0;int pin[] = {  3,5,6,9,10,11};void setup() {  for (int i = 0; i<6; i++){    pinMode(pin[i], OUTPUT);   }  //Serial.begin(9600);}void loop() {  for(int counter=0; counter<6; counter++){    sensorRead = analogRead(sensorPin);     // Serial.println(sensorRead);    if (sensorRead >= threshold) {      if (state == HIGH){        state = LOW;      }      else{        state = HIGH;      }      digitalWrite(pin[counter], state);     }  }}`

#1
##### Feb 10, 2013, 12:49 am
Here's one way that's pretty simple
Code: [Select]
`void loop(){sensorRead = analogRead(sensorPin);  // read the analog pindigitalWrite(ledPin, LOW);  // turn off prior LEDledPin =  map(sensorRead, 0, 1023, 2, 19);  // map analog reading to the new pin. 0,1 left for Serial comm'sdigitalWrite(ledPin, HIGH); // turn on next LED}`
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### 311x311

#2
##### Feb 10, 2013, 01:26 am
Thank you!  This is nice but isn't quite what I am looking for.
As you wrote, this maps analog reading over the pins between 2 and 19.
I am looking for a way to light up an LED one after the other, when and only when the analog value is above or equal to the threshold.

In other words,(in case my explanation was not clear)
When a value is above or equal to the threshold, LED#1 lights up.
This LED#1 stays on, while the sensor pin waits for another incoming signal.
When a signal comes in and meets the condition, LED#1 turns off and then LED#2 turns on.
When the last LED is lit up, the next LED which will light up will return to LED#1.
This cycles continues.

#3
##### Feb 10, 2013, 02:05 am
Ah, then you need to read the sensor, update the ledPin #, and set a flag that indicates the threshold is waiting to go down again.
When the reading drops, clear that flag, when the reading goes up again, turn off the ledPin and turn on the next one, and set the waiting flag again.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

#### 311x311

#4
##### Feb 10, 2013, 02:29 am
That is exactly where I am getting stuck...

#5
##### Feb 10, 2013, 02:47 am
Something like this then
Code: [Select]
`waitingForHigh = 1;void loop(){sensorRead = analogRead(sensorPin);  // read the analog pinif (sensorRead >=highThreshValue && waitingForHigh == 0){waitingForHigh = 1;  // ensures no change until level has gone lowdigitalWrite(ledPin, LOW);  // turn off prior LEDledPin = ledPin +1;if (ledPin ==20){ledPin = 2;}  // wrap back to beginningdigitalWrite(ledPin, HIGH); // turn on next LED}if (sensorRead <=lowThresh && waitingForHigh == 1){waitingForHigh = 0;}`
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up

Please enter a valid email to subscribe