Wednesday, April 9, 2014

LED's and an analog input.


Here's a program that turns on one of eight LEDs depending on where the potentiometer, connected to analog input 5, is set.  I feel like there could be a more concise way to write code that accomplishes this same function, but for now this is as short as I have it. 

Not sure if I should post the code, since it makes my post take up a bunch of room, but here goes...

PotentiometerLEDS:
  /*
    By:
    The Almighty Biglesword
   
    Reference:
    Adafruit Arduino - Lesson 8. Analog Inputs
   
    */
    int ledPin6 = 6;
    int ledPin7 = 7;
    int ledPin8 = 8;
    int ledPin9 = 9;
    int ledPin10 = 10;
    int ledPin11 = 11;
    int ledPin12 = 12;
    int ledPin13 = 13;
    int potPin = 5;
    
    void setup()
    {
    pinMode(ledPin6, OUTPUT);
    pinMode(ledPin7, OUTPUT);
    pinMode(ledPin8, OUTPUT);
    pinMode(ledPin9, OUTPUT);
    pinMode(ledPin10, OUTPUT);
    pinMode(ledPin11, OUTPUT);
    pinMode(ledPin12, OUTPUT);
    pinMode(ledPin13, OUTPUT);
    Serial.begin(9600);
   
    }
    
    void loop()
    {
    int reading = analogRead(potPin);//The analog reading from the potentiometer 0 - 1000ish
    int potPosition = choice(reading);//a number between 6-13 indicating which led to turn on
    Serial.println(potPosition);//tells you where the pot is via serial comm. if you're interested
    delay(1);
   
    if (potPosition == 6)//See where the potentiometer is at for specific led
 {
   digitalWrite(ledPin6, HIGH);
 }
 else
 {
   digitalWrite(ledPin6, LOW);
 }

     if (potPosition == 7)//See where the potentiometer is at for specific led
 {
   digitalWrite(ledPin7, HIGH);
 }
 else
 {
   digitalWrite(ledPin7, LOW);
 }

      if (potPosition == 8)//See where the potentiometer is at for specific led
 {
   digitalWrite(ledPin8, HIGH);
 }
 else
 {
   digitalWrite(ledPin8, LOW);
 }

       if (potPosition == 9)//See where the potentiometer is at for specific led
 {
   digitalWrite(ledPin9, HIGH);
 }
 else
 {
   digitalWrite(ledPin9, LOW);
 }
       
        if (potPosition == 10)//See where the potentiometer is at for specific led
 {
   digitalWrite(ledPin10, HIGH);
 }
 else
 {
   digitalWrite(ledPin10, LOW);
 }

         if (potPosition == 11)//See where the potentiometer is at for specific led
 {
   digitalWrite(ledPin11, HIGH);
 }
 else
 {
   digitalWrite(ledPin11, LOW);
 }

          if (potPosition == 12)//See where the potentiometer is at for specific led
 {
   digitalWrite(ledPin12, HIGH);
 }
 else
 {
   digitalWrite(ledPin12, LOW);
 }
   
          if (potPosition == 13)//See where the potentiometer is at for specific led
 {
   digitalWrite(ledPin13, HIGH); }
 else
 {
   digitalWrite(ledPin13, LOW);
 }
   
    }
   
   
   
    int choice (int reading)//analog scaling
      {
      int ledChoice;
            if (reading > 0, reading < 100)
 {
   ledChoice = 6;
   return ledChoice;
  }
      if (reading > 100, reading < 200)
 {
   ledChoice = 7;
   return ledChoice;
  }
        if (reading > 200, reading < 300)
 {
   ledChoice = 8;
   return ledChoice;
  }
          if (reading > 400, reading < 500)
 {
   ledChoice = 9;
   return ledChoice;
  }
            if (reading > 500, reading < 600)
 {
   ledChoice = 10;
   return ledChoice;
  }
              if (reading > 600, reading < 700)
 {
   ledChoice = 11;
   return ledChoice;
  }
              if (reading > 800, reading < 900)
 {
   ledChoice = 12;
   return ledChoice;
  }
              if (reading > 900, reading < 1020)
 {
   ledChoice = 13;
   return ledChoice;
  } 
 
 
      }

1 comment:

Mitch_was_here said...

You asked for more concise code and here it is. consumes less memory on the arduino as well.

/*
By:
The Almighty Biglesword

Reference:
Adafruit Arduino - Lesson 8. Analog Inputs

*/
int ledPin6 = 6;
int ledPin7 = 7;
int ledPin8 = 8;
int ledPin9 = 9;
int ledPin10 = 10;
int ledPin11 = 11;
int ledPin12 = 12;
int ledPin13 = 13;
int potPin = 5;

void setup() {
int stp = 0;
// set pins 1 - 13 as ouputs
for (stp = 6; stp < 14; stp++) {
pinMode(stp, OUTPUT);
}
Serial.begin(9600);
}

void loop() {
int testPos;
//The analog reading from the potentiometer 0 - 1000ish
int reading = analogRead(potPin);
//a number between 6-13 indicating which led to turn on
int potPosition = choice(reading);
//tells you where the pot is via serial comm. if you're interested
Serial.println(potPosition);
delay(1);

LED_OFF();

// see where the potentiometer is at for specific led
for (testPos = 6; testPos < 14; testPos++) {
if (potPosition == testPos) {
digitalWrite(testPos, HIGH);
testPos = 14; // ? if this doesnt exit try 13 instead of 14 ?
}
}
}

int LED_OFF() { // turn leds OFF
int ledPos;
for (ledPos = 6; ledPos < 14; ledPos++) {
digitalWrite(ledPos, LOW);
}
}

int choice (int reading) {//analog scaling
if (reading > 0, reading < 100) {
return 6;
} else if (reading > 100, reading < 200) {
return 7;
} else if (reading > 200, reading < 300) {
return 8;
} else if (reading > 400, reading < 500) {
return 9;
} else if (reading > 500, reading < 600) {
return 10;
} else if (reading > 600, reading < 700) {
return 11;
} else if (reading > 800, reading < 900) {
return 12;
} else if (reading > 900, reading < 1020) {
return 13;
}
}