Skip to content

Instantly share code, notes, and snippets.

@gatana
Last active September 29, 2015 20:38
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 gatana/9276621a7d95a76eb9fb to your computer and use it in GitHub Desktop.
Save gatana/9276621a7d95a76eb9fb to your computer and use it in GitHub Desktop.
//create variables for pins
const int buttonPin = 2;
const int ledPin = 12;
const int fadeLed = 11;
const int potPin = 0;
const int lightPin = 1;
//timecode
unsigned long previousMillis = 0;
const long interval = 10;
// LED fading variables
boolean fadeUp = true;
int fadeLevel = 0;
//LED blinking variables
int ledState = LOW;
//button variables
int buttonState = 0;
boolean pressed = false;
//analog sensor variables
int lightVar = 0;
int potVar = 0;
void setup() {
// set up pins
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(fadeLed, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && pressed == false ) { //if it is pressed now but was not pressed last loop
delay(5);
// Serial.println("BANG!");
// toggle lED:
if (ledState == LOW) {
ledState = HIGH;
}
else {
ledState = LOW;
}
pressed = true; //change this so we know the button was pressed
}
if (buttonState == LOW && pressed == true ) { //if the button is no longer pressed but was pressed last loop
delay(5);
pressed = false; //change this so we know it is no longer pressed
}
unsigned long currentMillis = millis();
//check sensors
potVar = analogRead(potPin);
lightVar = analogRead(lightPin);
//logic
if (lightVar <= potVar ){
fadeUp = true;
}
else {
fadeUp = false;}
//write to the LED pins
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (fadeUp == true ) {
if (fadeLevel != 255) fadeLevel++;
} else {
if (fadeLevel != 0) fadeLevel --;
}
}
//Serial.println(fadeLevel/4);
digitalWrite(ledPin, ledState);
analogWrite(fadeLed,fadeLevel);
Serial.println(lightVar);
int sensorReading = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the analog input range (in this case, 400 - 1000 from the photoresistor)
// to the output pitch range (120 - 1500Hz)
// change the minimum and maximum input numbers below
// depending on the range your sensor's giving:
int thisPitch = map(sensorReading, 400, 700, 620, 800);
// play the pitch:
tone(9, thisPitch, 1000);
delay(1); // delay in between reads for stability
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment