/* 4 light transistors input data from a curved surface and output to an RGB LED. created 10 Dec. 2010 by Ella Peinovich This code is in the public domain. */ // These constants won't change. They're used to give names // to the pins used: int sensor1Value = 0; // value read from the pot int sensor2Value = 0; // value read from the pot int sensor3Value = 0; // value read from the pot int sensor4Value = 0; // value read from the pot int led1red = 3; int led1blue = 5; int led1green = 6; void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); pinMode(led1red,OUTPUT); pinMode(led1blue,OUTPUT); pinMode(led1green,OUTPUT); } void loop() { // read the analog in value: sensor1Value = analogRead(A0); sensor2Value = analogRead(A1); sensor3Value = analogRead(A2); sensor4Value = analogRead(A3); // if bottom sensor is in high light, 0 to 200 //then the Red LED is turned OFF, and the Blue LED is turned ON if(sensor3Value < 200) { digitalWrite(led1red,HIGH); digitalWrite(led1blue,LOW); } // if top sensor is in indirect light, 201 to 1023 //then the Red LED is turned at a value inverse in scale to the light level on the sensor else{ analogWrite(led1red,map(sensor1Value,201,1023,255,0)); } // if top sensor is in high light, 0 to 200 //then the Blue LED is turned OFF, and the Red LED is turned ON if(sensor1Value < 200) { digitalWrite(led1blue,HIGH); digitalWrite(led1red,LOW); } // if bottom sensor is in indirect light, 201 to 1023 //then the Blue LED is turned at a value inverse in scale to the light level on the sensor else{ analogWrite(led1blue,map(sensor3Value,201,1023,255,0)); } // when a shadows crosses the sensor in the middle // the light level on middle sensor drops below the top sensor // then the Green LED is turned on for 100 milliseconds if(sensor2Value > sensor1Value + 400) { digitalWrite(led1green,LOW); delay(100); } // when the light levels are near equal in the middle and top sensor // the Green LED is off else{ digitalWrite(led1green,HIGH); } // print the results to the serial monitor: Serial.print("sensor1 = " ); Serial.print(sensor1Value); Serial.print("sensor2 = " ); Serial.print(sensor2Value); Serial.print("sensor3 = " ); Serial.print(sensor3Value); Serial.print("sensor4 = " ); Serial.println(sensor4Value); // wait 10 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(10); }