Skip to content

Instantly share code, notes, and snippets.

@eroeber
Created October 2, 2018 20:15
Show Gist options
  • Save eroeber/2c4ab0f8304ecac445e062162d07644d to your computer and use it in GitHub Desktop.
Save eroeber/2c4ab0f8304ecac445e062162d07644d to your computer and use it in GitHub Desktop.
// object lab 3 part 2
int photoValue = 0; // stores value of light sensors
int sensorPin = A0; // analog input
float sound = 0; // stores frequency variable for speaker
int speakerPin = 8; // speaker output
void setup() {
// initialize serial communications
Serial.begin(9600);
pinMode(speakerPin, OUTPUT);
}
void loop() {
// read the analog input on A0
photoValue = analogRead(sensorPin); // both photosensors are connected to pin A0
// print it to the serial monitor
Serial.println(photoValue); // make sure both sensors affect the analog reading
// map sensorReading to an output between 100 and 1000, set frequency to this
// sound is my mapped variable that stores the frequency value to send to my speaker
sound = map(photoValue, 80, 919, 100, 1000); // 80 and 919 were the average high and lows of the photosensors
// change the pitch, play for 10ms - use tone()
tone(8, sound); // parameters are the pin, and frequency
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment