Skip to content

Instantly share code, notes, and snippets.

@eroeber
Created October 4, 2018 00:53
Show Gist options
  • Save eroeber/beea0ba2ddde215071b19af66c1c9841 to your computer and use it in GitHub Desktop.
Save eroeber/beea0ba2ddde215071b19af66c1c9841 to your computer and use it in GitHub Desktop.
// object lab 3 part 3
int photoValue = 0; // stores value of light sensor
int photoPin = A0; // analog input
int potValue = 0; // stores value of potentiometer
int potPin = A1; // analog input
int speakerPin = 8; // speaker output
float sound = 0; // stores frequenct variable for speaker
void setup() {
// initialize serial communications
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);
}
void loop() {
photoValue = analogRead(photoPin); // read the analog input of photometer
potValue = analogRead(potPin); // read the analog input of potentiometer
// debug print to serial monitor
Serial.print("Photo value: ");
Serial.print(photoValue);
Serial.print(" Potentiometer value: ");
Serial.println(potValue);
// Here are the four notes of my the song
// C plays if the photo sensor is covered and the potentiometer is high
if((photoValue < 300) && (potValue > 1000)){
tone(8, 523.251, 500); // C
}
// D plays if the photo sensor is covered and the potentiometer is low
else if((photoValue < 300) && (potValue < 1000)){
tone(8, 587.33, 500); // D
}
// E plays if the photo sensor is uncovered and the potentiometer is high
else if((photoValue > 700) && (potValue > 1000)){
tone(8, 659.255, 500); // E
}
// F plays if the photo sensor is uncovered and the potentiometer is low
else if((photoValue > 700) && (potValue < 1000)){
tone(8, 698.456, 500); // F
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment