back


Decisions: if/else


Reading a Poti and deciding which LED to light and wich to turn off, using the Arduino if/else statements.


/* switch LEDs

 *
 * Switch between 3 LEDs using analog input and an if/else decision
 *

 * Making Things, http://makingthings.andreasmuxel.com
 * Based on the SensorAktor-Basics of the Lab3, Academy of Media Arts Cologne
 * http://interface.khm.de

 * pin mapping SensorAktor Shield
 * import SensorAktor.h to get already defined mapping
 *
 * analog inputs: analog0 = pin0, analog1 = pin1, analog2 = pin2,
 * analog3 = pin3, analog4 = pin4, analog5 = pin5
 * built-in poti: poti = pin5
 * amplified input: mic = pin0
 * switches: switch1 = pin1, switch2 = pin2, switch3 = pin3
 * leds: led1 = pin3, led2 = pin5, led3 = pin6
 * power outs: out1 = pin3, out2 = pin5, out3 = pin6
 * motor outs: motor1_1 = pin8, motor1_2 = pin13, motor2_1 = pin11, motor2_2 = pin12
 * motor pwms: pwmMotor1 = pin10, pwmMotor2 = pin9
 * servo outs: servo1 = pin2, servo2 = pin4, servo3 = pin7

 */

// inlcude sensor aktor library for pin mapping
#include <SensorAktor.h>

// poti value
int potiValue;

// this block is executed one time when programm starts
void setup(){

  // set pin modes
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);

}

// this block is executed in a loop after setup is called one time
void loop(){

  // get value of poti (value between 0 and 1023)
  potiValue = analogRead(poti);
  // decide to turn on and off LEDs
  if (potiValue < 341) {
    digitalWrite(led1,HIGH);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  } else if (potiValue < 682) {
    digitalWrite(led1,LOW);
    digitalWrite(led2,HIGH);
    digitalWrite(led3,LOW);
  } else {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,HIGH);
  }
}


Files needed


SensorAktor.zip

ifElse.zip