Last active
December 5, 2025 15:27
-
-
Save psychemist/c9ffbb603f66617e6a19bdb7d1928b49 to your computer and use it in GitHub Desktop.
Arduino Starter Project 6
This file contains hidden or 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
| // create variables for calibrating the sensor | |
| int sensorValue; | |
| int sensorLow = 1023; | |
| int sensorHigh = 0; | |
| // create constant for calibration indicator at on-board LED connected to pin 13 | |
| const int ledPin = 13; | |
| void setup() { | |
| // set digital pin direction and turn it high | |
| pinMode(ledPin, OUTPUT); | |
| digitalWrite(ledPin, HIGH); | |
| // calibrate sensor's minimum and maximum values | |
| while ( millis() < 5000 ) { | |
| sensorValue = analogRead(A0); | |
| // compare sensor values for calibration | |
| if ( sensorValue > sensorHigh ) { | |
| sensorHigh = sensorValue; | |
| } | |
| else if ( sensorValue < sensorLow ) { | |
| sensorLow = sensorValue; | |
| } | |
| // indicate calibration has finished by turning on indicator | |
| digitalWrite(ledPin, LOW); | |
| } | |
| } | |
| void loop() { | |
| // read and store the value at A0 | |
| sensorValue = analogRead(A0); | |
| // map sensor value to frequency | |
| int pitch = map( sensorValue, sensorLow, sensorHigh, 50, 4000 ); | |
| // play the frequency | |
| tone(8, pitch, 20); | |
| delay(10); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment