For my first attempt at this project, I wanted to use the speakers and photoresistors to create a piano by using the photoresistors as keys which would then make speakers emit different pitches of sound. This effort, however, failed.

Link to First Attempt

[For some reason, every time I tried to embed the YouTube link, the code would disappear when I updated the post.]

The following is the code for my first attempt:

Code:

//MOST NOTATION FOR INITIATION INSPIRED BY, 
IF NOT, FROM SENSORFADE AND 
EVENTLOOPDEMO SCRIPTS BY GARTH ZEGLIN

//PHOTORESISTOR INPUT PORTS
const int SENSORPIN1 = A1;
const int SENSORPIN2 = A2;
const int SENSORPIN3 = A3;
const int SENSORPIN4 = A4;


//SPEAKER OUTPUT PORTS
const int SPEAKERPIN1 = 1;
const int SPEAKERPIN2 = 2;
const int SPEAKERPIN3 = 3;
const int SPEAKERPIN4 = 4;


void setup() {
 Serial.begin(9600);

 pinMode( SPEAKERPIN1, OUTPUT );
 pinMode( SPEAKERPIN2, OUTPUT );
 pinMode( SPEAKERPIN3, OUTPUT );
 pinMode( SPEAKERPIN4, OUTPUT );
}

long DURATION = 1000000; //STANDARD DURATION OF NOTE

int TOGGLESPEAKER1 = LOW;
int TOGGLESPEAKER2 = LOW;
int TOGGLESPEAKER3 = LOW;
int TOGGLESPEAKER4 = LOW;

long TIMECONTROL1 = 0;
long TIMECONTROL2 = 0;
long TIMECONTROL3 = 0;
long TIMECONTROL4 = 0;

void loop() 
{
 long NOW = micros();
 
 int SENSOR1 = analogRead(SENSORPIN1);
 int SENSOR2 = analogRead(SENSORPIN2);
 int SENSOR3 = analogRead(SENSORPIN3);
 int SENSOR4 = analogRead(SENSORPIN4);
 
 Serial.println(SENSOR1);
// Serial.println(SENSOR2);
// Serial.println(SENSOR3);
// Serial.println(SENSOR4);
 
//SPEAKER 1
 if(SENSOR1 < 500 and TOGGLESPEAKER1 == LOW) { TOGGLESPEAKER1 = !TOGGLESPEAKER1; TIMECONTROL1 = NOW; TIMECONTROL1 += DURATION; digitalWrite( SPEAKERPIN1, TOGGLESPEAKER1); } if(NOW > TIMECONTROL1 and TOGGLESPEAKER1 ==HIGH) {
   TOGGLESPEAKER1 = !TOGGLESPEAKER1;
 }

//SPEAKER 2
 if(SENSOR2 < 500 and TOGGLESPEAKER2 == LOW) { TOGGLESPEAKER2 = !TOGGLESPEAKER2; TIMECONTROL2 = NOW; TIMECONTROL2 += DURATION; digitalWrite( SPEAKERPIN2, TOGGLESPEAKER2); } if(NOW > TIMECONTROL2 and TOGGLESPEAKER2 ==HIGH) {
   TOGGLESPEAKER2 = !TOGGLESPEAKER2;
 }


//SPEAKER 3
 if(SENSOR3 < 500 and TOGGLESPEAKER3 == LOW) { TOGGLESPEAKER3 = !TOGGLESPEAKER3; TIMECONTROL3 = NOW; TIMECONTROL3 += DURATION; digitalWrite( SPEAKERPIN3, TOGGLESPEAKER3); } if(NOW > TIMECONTROL3 and TOGGLESPEAKER3 ==HIGH) {
   TOGGLESPEAKER3 = !TOGGLESPEAKER3;
 }

//SPEAKER 4
 if(SENSOR4 < 500 and TOGGLESPEAKER4 == LOW) { TOGGLESPEAKER4 = !TOGGLESPEAKER4; TIMECONTROL4 = NOW; TIMECONTROL4 += DURATION; digitalWrite( SPEAKERPIN4, TOGGLESPEAKER4); } if(NOW > TIMECONTROL4 and TOGGLESPEAKER4 ==HIGH) {
   TOGGLESPEAKER4 = !TOGGLESPEAKER4;
 } 

}


 

My second attempt repurposed this code and attempted to do something very similar with LEDs that would light for different durations activated by photoresistors.

Link to Second Attempt

The following is the code for my second attempt
Code:


//MOST NOTATION FOR INITIATION INSPIRED BY, 
IF NOT, FROM SENSORFADE AND 
EVENTLOOPDEMO SCRIPTS BY GARTH ZEGLIN

//PHOTORESISTOR INPUT PORTS
const int SENSORPIN1 = A1;
const int SENSORPIN2 = A2;
const int SENSORPIN3 = A3;
const int SENSORPIN4 = A4;


//LED OUTPUT PORTS
const int LEDPIN1 = 1;
const int LEDPIN2 = 2;
const int LEDPIN3 = 3;
const int LEDPIN4 = 4;


void setup() {
 Serial.begin(9600);

 pinMode( LEDPIN1, OUTPUT );
 pinMode( LEDPIN2, OUTPUT );
 pinMode( LEDPIN3, OUTPUT );
 pinMode( LEDPIN4, OUTPUT );
}

long DURATION1 = 1000000; 
long DURATION2 = 2000000;
long DURATION3 = 3000000;
long DURATION4 = 4000000;

int TOGGLELED1 = LOW;
int TOGGLELED2 = LOW;
int TOGGLELED3 = LOW;
int TOGGLELED4 = LOW;

long TIMECONTROL1 = 0;
long TIMECONTROL2 = 0;
long TIMECONTROL3 = 0;
long TIMECONTROL4 = 0;

void loop() 
{
 long NOW = micros();
 
 int SENSOR1 = analogRead(SENSORPIN1);
 int SENSOR2 = analogRead(SENSORPIN2);
 int SENSOR3 = analogRead(SENSORPIN3);
 int SENSOR4 = analogRead(SENSORPIN4);
 
// Serial.println(SENSOR1);
 Serial.println(SENSOR2);
// Serial.println(SENSOR3);
// Serial.println(SENSOR4);
 
//LED 1
 if(SENSOR1 < 500 and TOGGLELED1 == LOW) { TOGGLELED1 = !TOGGLELED1; TIMECONTROL1 = NOW; TIMECONTROL1 += DURATION1; digitalWrite( LEDPIN1, TOGGLELED1); } if(NOW > TIMECONTROL1 and TOGGLELED1 == HIGH) {
   TOGGLELED1 = !TOGGLELED1;
   digitalWrite( LEDPIN1, TOGGLELED1);
 }

//LED 2
 if(SENSOR2 < 500 and TOGGLELED2 == LOW) { TOGGLELED2 = !TOGGLELED2; TIMECONTROL2 = NOW; TIMECONTROL2 += DURATION2; digitalWrite( LEDPIN2, TOGGLELED2); } if(NOW > TIMECONTROL2 and TOGGLELED2 == HIGH) {
   TOGGLELED2 = !TOGGLELED2;
   digitalWrite( LEDPIN2, TOGGLELED2);
 }


//LED 3
 if(SENSOR3 < 500 and TOGGLELED3 == LOW) { TOGGLELED3 = !TOGGLELED3; TIMECONTROL3 = NOW; TIMECONTROL3 += DURATION3; digitalWrite( LEDPIN3, TOGGLELED3); } if(NOW > TIMECONTROL3 and TOGGLELED3 ==HIGH) {
   TOGGLELED3 = !TOGGLELED3;
   digitalWrite( LEDPIN3, TOGGLELED3);
 }

//LED 4
 if(SENSOR4 < 500 and TOGGLELED4 == LOW) { TOGGLELED4 = !TOGGLELED4; TIMECONTROL4 = NOW; TIMECONTROL4 += DURATION4; digitalWrite( LEDPIN4, TOGGLELED4); } if(NOW > TIMECONTROL4 and TOGGLELED4 ==HIGH) {
   TOGGLELED4 = !TOGGLELED4;
   digitalWrite( LEDPIN4, TOGGLELED4);
 } 

}