Air pressure: Motorola

This example is for Wiring version 0024+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.

Light Equalizer by Ana Lucia Martinez Arias

Demosntrates how to connect and use a SHARP GP2D12 and GP2D120 infrared ranger sensor. Interprets distance from the ranger sensor value turning On a number of lights accordingly. The LEDs are connected to the digital pins 0 to 9


int distance=0;

void turn_all_off() {  // function to turn off all the lights
  for(int i=0; i<10; i++)  //LEDs connected to digital pins 0-9
  {
    digitalWrite(i, LOW);
  }
}

void setup(){
  Serial.begin(9600);	// set the serial port to 9600
  for (int i=0; i<10; i++){
    pinMode(i, OUTPUT);  // set pins 0-9 as outputs
  }
  pinMode(48,OUTPUT);
  digitalWrite(48,HIGH);  // turn ON onboard LED
}

void loop(){
  turn_all_off();                 // turns all lights off
  distance = analogRead(0);       // sensor connected to analog pin 0
  Serial.println(distance, DEC);  // prints the value read from the sensor

  if((distance>50)){
    digitalWrite(0, HIGH);   // sets the current light on
  }

  if((distance>100)){
    digitalWrite(1, HIGH);   // sets the current light on
  }

  if((distance>150)){
    digitalWrite(2, HIGH);   // sets the current light on
  }

  if((distance>200)){
    digitalWrite(3, HIGH);   // sets the current light on
  }

  if((distance>250)){
    digitalWrite(4, HIGH);   // sets the current light on
  }

  if((distance>300)){
    digitalWrite(5, HIGH);   // sets the current light on
  }

  if((distance>350)){
    digitalWrite(6, HIGH);   // sets the current light on
  }

  if((distance>400)){
    digitalWrite(7, HIGH);   // sets the current light on
  }

  if((distance>450)){
    digitalWrite(8, HIGH);   // sets the current light on
  }

  if((distance>500)){
    digitalWrite(9, HIGH);   // sets the current light on
  }

  delay(50);
}