Guest User

Arduino Light Theremin Code

a guest
Oct 28th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. int sensorValue;
  2. int sensorLow = 1023;
  3. int sensorHigh = 0;
  4. const int ledPin = 13;
  5.  
  6. void setup() {
  7. pinMode(ledPin, OUTPUT);
  8. digitalWrite(ledPin, HIGH);
  9.  
  10. // Calibrate the sensor for the first 5 seconds
  11. while (millis() < 5000) {
  12. sensorValue = analogRead(A0);
  13. if (sensorValue > sensorHigh) {
  14. sensorHigh = sensorValue; // Track the highest sensor value
  15. }
  16. if (sensorValue < sensorLow) {
  17. sensorLow = sensorValue; // Track the lowest sensor value
  18. }
  19. }
  20.  
  21. digitalWrite(ledPin, LOW); // Turn off the LED after calibration
  22. }
  23.  
  24. void loop() {
  25. sensorValue = analogRead(A0);
  26.  
  27. // Map the sensor value to a pitch range
  28. int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);
  29.  
  30. // Play the tone based on the pitch
  31. tone(8, pitch, 10);
  32.  
  33. // Small delay to avoid overwhelming the Arduino
  34. delay(50);
  35. }
  36.  
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment