Skip to content

Instantly share code, notes, and snippets.

Created February 26, 2020 04:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
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:
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
Serial.print("photoreceptors = ");
// 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:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment