Skip to content

Instantly share code, notes, and snippets.

@cyrildiagne
Created February 28, 2016 13:48
Show Gist options
  • Save cyrildiagne/c27630c3f0b0dc8db54c to your computer and use it in GitHub Desktop.
Save cyrildiagne/c27630c3f0b0dc8db54c to your computer and use it in GitHub Desktop.
Arduino - Photoresistor to Tune
#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);
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment