Skip to content

Instantly share code, notes, and snippets.

@ungood
Created May 24, 2011 20:07
Show Gist options
  • Select an option

  • Save ungood/989564 to your computer and use it in GitHub Desktop.

Select an option

Save ungood/989564 to your computer and use it in GitHub Desktop.
coop code
const int topPin = 3;
const int bottomPin = 4;
const int upPin = 5;
const int downPin = 6;
const int sensorPin = 0; // analog pin
const int nightThreshold = 50;
const int dayThreshold = 900;
const int nightLength = 8 * 60 * 60 * 1000; // 8 hours in ms.
void setup() {
// Set internal pullupresistors on the switches
pinMode(topPin, INPUT);
digitalWrite(topPin, HIGH);
pinMode(bottomPin, INPUT);
digitalWrite(bottomPin, HIGH);
pinMode(sensorPin, INPUT);
stop();
}
void up() {
digitalWrite(upPin, LOW);
digitalWrite(downPin, HIGH);
}
void down() {
digitalWrite(downPin, LOW);
digitalWrite(upPin, HIGH);
}
void stop() {
digitalWrite(downPin, LOW);
digitalWrite(upPin, LOW);
}
void open() {
up();
while(digitalRead(topPin) == LOW);
stop();
}
void close() {
down();
while(digitalRead(bottomPin) == LOW);
stop();
}
boolean isNight(int lightValue) {
return lightValue <= nightThreshold
}
void loop() {
int lightValue = analogRead(sensorPin);
if(isNight(lightValue)) {
close();
delay(nightLength);
open();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment