Go Down

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

311x311

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.

Any advice is greatly appreciated.

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



CrossRoads

Here's one way that's pretty simple
Code: [Select]

void loop(){
sensorRead = analogRead(sensorPin);  // read the analog pin
digitalWrite(ledPin, LOW);  // turn off prior LED
ledPin =  map(sensorRead, 0, 1023, 2, 19);  // map analog reading to the new pin. 0,1 left for Serial comm's
digitalWrite(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

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.

CrossRoads

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

That is exactly where I am getting stuck...

CrossRoads

Something like this then
Code: [Select]

waitingForHigh = 1;
void loop(){
sensorRead = analogRead(sensorPin);  // read the analog pin
if (sensorRead >=highThreshValue && waitingForHigh == 0){
waitingForHigh = 1;  // ensures no change until level has gone low
digitalWrite(ledPin, LOW);  // turn off prior LED
ledPin = ledPin +1;
if (ledPin ==20){ledPin = 2;}  // wrap back to beginning
digitalWrite(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