Arduino Analog Sandbox

From BenningtonWiki
Jump to: navigation, search

Connect various analog devices to analog inputs 0 and 1. LEDs on the outputs reflect various relationships to the inputs. <source lang="C"> // Two analog inputs 0 and 1. // Digital outputs: // 6. Analog 0 > Analog 1 // 7. Analog 0 = Analog 1 // 8. Analog 0 < Analog 1 // PWM outputs: // 9. inverse of level of 0 // 10. inverse of level of 1 // 11. difference.

void setup(void) {

   pinMode(6, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(8, OUTPUT);
   pinMode(9, OUTPUT);
   pinMode(10, OUTPUT);
   pinMode(11, OUTPUT);

}

void loop(void) {

   int light_level_A, light_level_B, diff;
   light_level_A = analogRead(0);
   light_level_B = analogRead(1);
   analogWrite(9, (1023 - light_level_A) / 4);
   analogWrite(10, (1023 - light_level_B) / 4);
   diff = abs(light_level_A - light_level_B);
   analogWrite(11, diff / 4);
   diff = (light_level_A - light_level_B) / 50;
   digitalWrite(6, diff > 0);
   digitalWrite(7, diff == 0);
   digitalWrite(8, diff < 0);

} </source>