Skip to content

Instantly share code, notes, and snippets.

@scowler1
Created May 5, 2019 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scowler1/385101e5cfa3d720a1508caaa7c1d73e to your computer and use it in GitHub Desktop.
Save scowler1/385101e5cfa3d720a1508caaa7c1d73e to your computer and use it in GitHub Desktop.
/* Noise machine using three potentiometers connected to analog inputs
and a piezo or small speaker. A pushbutton turns on the noise, the potentiometers
control the pitch using the Arduino tone() function, and two delay
values which control the length of each tone and length between
each tone. The potentiometers give analog values which are changed
using the map() function into larger or smaller ranges to suit your
musical tastes.
This code is in the public domain.
Matt Thomas 05/04/2019
*/
const int buttonPin = 2; // Pushbutton pin 2
const int speaker = 9; // Speaker or piezo in pin 9
int buttonState = 0; // Variables for the button
int potZero; // and potentiometers
int potOne;
int potTwo;
void setup() {
pinMode(9, OUTPUT); // Speaker/piezo output pin
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the pushbutton state
potZero = analogRead(A0); //Variables for reading the analog values
potOne = analogRead(A1);
potTwo = analogRead(A2);
int htz = map(potZero, 0,1023, 0,8800); // Map the analog readings into
int high = map(potOne, 0,1023, 0,100); // new number ranges and create
int low = map(potTwo, 0,1023, 0,100); // new variables
if (buttonState == HIGH) { // If the pushbutton is pressed...
tone(speaker, htz); // Sound on
delay(high); // Length of tone
noTone(speaker); // Sound off
delay(low); // Time until next tone
} else {
noTone(speaker); // No tone if the button is released
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment