Created
February 28, 2016 13:48
-
-
Save cyrildiagne/c27630c3f0b0dc8db54c to your computer and use it in GitHub Desktop.
Arduino - Photoresistor to Tune
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "pitches.h" | |
int lightPin = 0; | |
int potPin = 1; | |
int valueToTriggerNote = 0; | |
int tunePin = 9; | |
int feedbackLedPin = 13; | |
int tune = 400; | |
int prevval = 0; | |
void setup() { | |
pinMode(feedbackLedPin, OUTPUT); | |
} | |
void loop() { | |
int lightVal = analogRead(lightPin); | |
int lightDelta = prevval - lightVal; | |
if (lightDelta > 80) { | |
tune *= 1.25; | |
if (tune > 1000) { | |
tune = 400; | |
} | |
playTone(8, tune); | |
delay(300); | |
} | |
prevval = lightVal; | |
delay(100); | |
} | |
void playTone(int pin, int note) { | |
digitalWrite(feedbackLedPin, HIGH); | |
// play a note on pin 8 for 500 ms: | |
tone(tunePin, note); | |
delay(300); | |
digitalWrite(feedbackLedPin, LOW); | |
// turn off tone function for pin 7: | |
noTone(tunePin); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment