Skip to content

Instantly share code, notes, and snippets.

@zach-c-d
Last active January 27, 2016 20:53
Show Gist options
  • Save zach-c-d/c232c85399123b99a119 to your computer and use it in GitHub Desktop.
Save zach-c-d/c232c85399123b99a119 to your computer and use it in GitHub Desktop.
//blink LED at rate specified by analog input from light resistor
//analogue input gives numbers between 0-1033
//analogue output expects numbers between 0-255
int light_resistor = 0; //light resistor port
int LED = 9; //LED port
int i = 0; //level of resistance from light_resistor
void setup() {
Serial.begin(9600); //begin serial communication
pinMode(light_resistor, INPUT); //initialize light resistor
pinMode(LED, OUTPUT); //intitialize LED
}
void loop() {
Serial.print("value= ");//print light_resistor
Serial.println(analogRead(light_resistor)); //value to serial
i = analogRead(light_resistor); // store LDR reading in i
analogWrite(LED, i / 4);
if (100 > i) {
analogWrite(LED, i * 0); //for some reason it doesn't work if I
//I set it to (LED,0);
}
/*
//not sure why this code wouldn't work
if (350> i > 251) {
analogWrite(LED, i);
} else if (250 > i > 121) {
analogWrite(LED, i / 10); //change LED brightness with LDR reading
}
if ( i < 100) {
analogWrite(LED, 0);
}
delay(10);
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment