Created
February 26, 2020 04:39
-
-
Save peow2373/0847c7b9172cb8c905e8ad25b9f961db to your computer and use it in GitHub Desktop.
Uses an 8-bit speaker to create a tone output that changes frequency when the photoreceptor senses different amounts of lights
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Analog input, analog output, serial output | |
Reads an analog input pin, maps the result to a range from 0 to 255 and uses | |
the result to set the pulse width modulation (PWM) of an output pin. | |
Also prints the results to the Serial Monitor. | |
The circuit: | |
- potentiometer connected to analog pin 0. | |
Center pin of the potentiometer goes to the analog pin. | |
side pins of the potentiometer go to +5V and ground | |
- LED connected from digital pin 9 to ground | |
*/ | |
int analogInPin = A0; // Analog input | |
int analogOutPin = 8; // Analog output | |
int sensorValue = 0; // value read from the photoreceptors | |
int outputValue = 0; // value output to ledPin1 | |
void setup() { | |
// initialize serial communications at 9600 bps: | |
Serial.begin(9600); | |
} | |
void loop() { | |
// read the analog in value: | |
sensorValue = analogRead(analogInPin); | |
Serial.print("photoreceptors = "); | |
Serial.print(sensorValue); | |
// map it to the range of the analog out: | |
outputValue = map(sensorValue, 0, 1023, 100, 1000); | |
// change the analog out value: | |
tone(analogOutPin, outputValue); | |
// wait 5 milliseconds before the next loop for the analog-to-digital | |
// converter to settle after the last reading: | |
delay(5); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment