4 photoresistors controlling 4 LED's


Circuit of Arduino breadboard assembly - made with online circuit builder by www.mekanizmalar.com

Arduino breadboard assembly - 4 photoresistors drive 4 LED's

Details of breadboard connections

Details of breadboard connections

Source code


/*
Cesare Brizio, August 2014
Controlling 4 led by 4 photoresistor
This sketch is an intermediate step towards the construction
of a light-tracking turret based on two stepper motors.
The turret will rotate 90 in the vertical and 180 in the 
horizontal plane, and so tracking directions will include
up-down and left-right. A 4-partition head, each partition
containing a photoresistor, will be aimed at light by 
minimizing the delta among the four photoresistor, by moving
in the direction of the most illuminated, minimum resistance
photoresistor.
The turret won't have rotation limit switches and will need 
manual centering before starting the software, that will 
implement soft rotation limits.
*/
#define photores0 A0  
#define led0 9  
#define photores1 A1  
#define led1 10 
#define photores2 A2  
#define led2 11 
#define photores3 A3  
#define led3 12 
#define threshold 250
#define max_cycles 1000
void setup() {  
 pinMode(A0,INPUT); // Analog input of the photoresistor values
 pinMode(A1,INPUT);  pinMode(A2,INPUT);  pinMode(A3,INPUT);  pinMode(9,OUTPUT); // Digital output for turning LED's on
 pinMode(10,OUTPUT);  pinMode(11,OUTPUT);  pinMode(12,OUTPUT);  Serial.begin(9600); // Initialize serial communications
}  
// Reminder: void loop() cannot be omitted even if
// you put all the code in Setup() - even in that case 
// you would need a void function called loop:
// void loop() {} 
void loop(){
int cycle_count=0;
boolean exit_loop=false;
// I use a While loop based on a given max_cycles number
// just to have an impression of control - actually,
// Arduino does never stop and will keep on going even
// if you break the loop... so I put a while loop 
// inside the main loop() - when the internal while loop 
// is broken, execution falls on an infinite while(1) {} 
// still inside the main void loop() 
//
// To set additional conditions for loop execution, I prepared 
// a boolean value that you may set true 
//       if (check_condition)
//           exit_loop=true;
// To implement this check in the while condition, use:
//   while (conta_cicli<max_cicli && esci==false);
while(cycle_count<max_cycles){
        cycle_count++;
        Serial.println("-------------");
        Serial.println("I'm at cycle");
        Serial.println(cycle_count);
        Serial.println(" of ");
        Serial.println(max_cycles);
        Serial.println("-------------");
        
        int val0 = analogRead(photores0); // val0 is used to save the reading from photoresistor 0
        Serial.println("photoresistor 0 - ");
        Serial.println(val0, DEC); // val0 is printed in decimal format
        if(val0<threshold) // if the value (light brightness) is below the threshold, I turn on the corresponding led
               digitalWrite(led0,HIGH);         else                digitalWrite(led0,LOW); // otherwise I turn it off (or I leave it off)
        int val1 = analogRead(photores1); // val1 is used to save the reading from photoresistor 1
        Serial.println("photoresistor 1 - ");
        Serial.println(val1, DEC); // val1 is printed in decimal format
        if(val1<threshold) // if the value (light brightness) is below the threshold, I turn on the corresponding led
               digitalWrite(led1,HIGH);         else                digitalWrite(led1,LOW); // otherwise I turn it off (or I leave it off)
        int val2 = analogRead(photores2); // val2 is used to save the reading from photoresistor 2
        Serial.println("photoresistor 2 - ");
        Serial.println(val2, DEC); // val2 is printed in decimal format
        if(val2<threshold) // if the value (light brightness) is below the threshold, I turn on the corresponding led
               digitalWrite(led2,HIGH);         else                digitalWrite(led2,LOW); // otherwise I turn it off (or I leave it off)
        int val3 = analogRead(photores3); // val3 is used to save the reading from photoresistor 1
        Serial.println("photoresistor 3 - ");
        Serial.println(val3, DEC); // val3 is printed in decimal format
        if(val3<threshold) // if the value (light brightness) is below the threshold, I turn on the corresponding led
               digitalWrite(led3,HIGH);         else                digitalWrite(led3,LOW); // otherwise I turn it off (or I leave it off)
    }
        // Useless loop, just in case you want to do something else...
        while(1) {
        Serial.println(max_cycles);
        Serial.println(" were exceeded - Idling");
        }
}