Skip to content

Instantly share code, notes, and snippets.

@psychemist
Last active December 5, 2025 15:27
Show Gist options
  • Select an option

  • Save psychemist/c9ffbb603f66617e6a19bdb7d1928b49 to your computer and use it in GitHub Desktop.

Select an option

Save psychemist/c9ffbb603f66617e6a19bdb7d1928b49 to your computer and use it in GitHub Desktop.
Arduino Starter Project 6
// 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