Pages: [1]   Go Down
Author Topic: Analogue Sensor to Cycle Through LEDs  (Read 408 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

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:

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

Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26421
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's one way that's pretty simple
Code:
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
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

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

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26421
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

That is exactly where I am getting stuck...
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26421
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Something like this then
Code:
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;
}
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Pages: [1]   Go Up
Jump to: