Skip to content

Instantly share code, notes, and snippets.

@agocs
Created January 29, 2017 20:11
Show Gist options
  • Save agocs/2d9d92010934f1f9e56ac5755fa9de70 to your computer and use it in GitHub Desktop.
Save agocs/2d9d92010934f1f9e56ac5755fa9de70 to your computer and use it in GitHub Desktop.
Arduino powered nightlight
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Here, we're initializing communication from the arduino back to the computer
pinMode(3,OUTPUT); // Here, we're setting pin D3 as an output pin.
}
void loop() {
// put your main code here, to run repeatedly:
int sensorValue = analogRead(A2); // Reading the photosensor
Serial.println(sensorValue); // sending that value back to the computer just so we know
if (sensorValue < 500){ // if the sensor is seeing less than 500 units of light...
// we determined that 500 was roughly the threshold between bright and dark
digitalWrite(3, HIGH); // turn the light on
}else { // otherwise...
digitalWrite(3, LOW); // turn the light off
}
delay(500); // wait half a second and begin again.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment