4 photoresistors controlling 4 LED's - with switch and buzzer


See the base project to which I added a switch and a buzzer, while improving overall program structure.
(add a switch as described in http://arduino.cc/en/Tutorial/Debounce) (add a speaker as described in http://arduino.cc/en/Tutorial/Tone)

Photo of the addition to my previous breadboard circuit

Source code


/*
 
Controlling 4 led by 4 photoresistor and a switch
Circuit: 
 
See http://www.cesarebrizio.it/Arduino/4P4L.html
(add a switch as described in http://arduino.cc/en/Tutorial/Debounce)
(add a speaker as described in http://arduino.cc/en/Tutorial/Tone)
created 5 Aug 2014
 modified ----
 by Cesare Brizio
 
This example code is in the public domain.
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 diractions 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.
*/
#include "pitches.h"
// 4 photoresitors on analog lines 0-3
#define photoRes0 A0  
#define photoRes1 A1  
#define photoRes2 A2  
#define photoRes3 A3  
// 4 LED's on digital lines 9-12
#define led0 9  
#define led1 10 
#define led2 11 
#define led3 12 
// a "SLEEP" pin to store last activation state
#define SLEEP 7       // PIN 7 = SLP
#define switchPin 2           //define switch to pin 2
// constants and variables declaration and setup
int threshold = 250; // light threshold derived form experiments
boolean lastButton = LOW; //part of debounce function
boolean currentButton = LOW; //part of debounce function
boolean work = LOW; //low puts driver into sleep mode, high turns it on
// notes in the melody:
int melody[] = {NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {4,8,8,4,4,4,4,4 };
// variables to define the sequence of execution of the notes
int note = 1;

void play_note(int thisNote) {
         // to calculate the note duration, take one second
         // divided by the note type.
         //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
         int noteDuration = 1000/noteDurations[thisNote];
         tone(8, melody[thisNote],noteDuration);
         // to distinguish the notes, set a minimum time between them.
         // the note's duration + 30% seems to work well:
         int pauseBetweenNotes = noteDuration * 1.30;
         delay(pauseBetweenNotes);
         // stop the tone playing:
         noTone(8);
  }
  
void play_tune_1() {
      // plays a tune in direct direction
      Serial.println("motivo 1");
             for (note = 0; note < 8; note = note + 1) {
                 play_note(note);
             }
     }
     
void play_tune_2() {
      // plays a tune in reverse direction
      Serial.println("motivo 2");
             for (note = 8; note > 0; note = note - 1) {
                 play_note(note);
             }
     }

boolean debounce(boolean last) //debounce function for switch
{
  boolean current = digitalRead(switchPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(switchPin);
  }
  return current;
}

// Reminder: void loop() cannot be omitted even if
// you put all the code in operate_turret()
// you would need a void function called loop:
void operate_turret()
{
      
        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);                 play_note(2); }
        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);                play_note(3);}
        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);                play_note(6); }
        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);                play_note(7);}
        else                digitalWrite(led3,LOW); // otherwise I turn it off (or I leave it off)
}

void setup() {  
 /* Pin operation mode setup */
 pinMode(photoRes0,INPUT); // Analog input of the photoresistor values
 pinMode(photoRes1,INPUT);  pinMode(photoRes2,INPUT);  pinMode(photoRes3,INPUT);  pinMode(led0,OUTPUT); // Digital output for turning LED's on
 pinMode(led1,OUTPUT);  pinMode(led2,OUTPUT);  pinMode(led3,OUTPUT);  pinMode(switchPin, INPUT); // set pin 8 to input
 pinMode(SLEEP, OUTPUT); // set pin 12 to output
 /* Initialize serial communications */
 Serial.begin(9600); // Initialize serial communications


void loop(){
currentButton = debounce(lastButton);
// I use a button switch to enter a While loop 
// inside the main loop() 
  if (lastButton == LOW && currentButton == HIGH)
  {
    work = !work;  //Work is boolean variable for switch on/off
    play_tune_1(); // play a melody every time the switch is pressed
  }
  lastButton = currentButton;
  digitalWrite(SLEEP, work); //set SLEEP pin to value of Work variable
  while(work == HIGH){
    // I must check the button also inside the while loop..
    // otherwise it will be endless
    currentButton = debounce(lastButton);
    if (lastButton == LOW && currentButton == HIGH)
    {
      work = !work;  //motOn is boolean variable for switch on/off
      play_tune_2(); // play a melody every time the switch is pressed
    }
    lastButton = currentButton;
    digitalWrite(SLEEP, work); //set SLEEP pin to value of motOn variable
    Serial.println("Execution enabled - switch is ON");
    operate_turret();
    }
    Serial.println("Execution disabled - switch is OFF");
}