Pitch Follower with On/Off Switch

[code lang=”arduino” light=”true”]

// ProtoSnap
// switch on for pitch controlled by light

const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize serial communications (for debugging only):
pinMode(buttonPin, INPUT_PULLUP);


void loop() {
//read the button and store it’s state in new variable
buttonState = digitalRead(buttonPin);

// read the light sensor:
int sensorReading = analogRead(A6);
// print the sensor reading so you know its range
// map the pitch to the range of the analog input.
// change the minimum and maximum input numbers below
// depending on the range your sensor’s giving:
int thisPitch = map(sensorReading, 50, 0, 100, 1000);

// if button in pressed do this…
if (buttonState == LOW){

// play the pitch:
tone(7, thisPitch, 10);

delay(1); // delay in between reads for stability



Leave a Reply