Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int sensorValue;
- int sensorLow = 1023;
- int sensorHigh = 0;
- const int ledPin = 13;
- void setup() {
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, HIGH);
- // Calibrate the sensor for the first 5 seconds
- while (millis() < 5000) {
- sensorValue = analogRead(A0);
- if (sensorValue > sensorHigh) {
- sensorHigh = sensorValue; // Track the highest sensor value
- }
- if (sensorValue < sensorLow) {
- sensorLow = sensorValue; // Track the lowest sensor value
- }
- }
- digitalWrite(ledPin, LOW); // Turn off the LED after calibration
- }
- void loop() {
- sensorValue = analogRead(A0);
- // Map the sensor value to a pitch range
- int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
- // Play the tone based on the pitch
- tone(8, pitch, 10);
- // Small delay to avoid overwhelming the Arduino
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment