Skip to content

Instantly share code, notes, and snippets.

@Servuc
Last active April 28, 2016 21:04
Show Gist options
  • Save Servuc/e4cefd0a67d22fedeca8cdff836d993b to your computer and use it in GitHub Desktop.
Save Servuc/e4cefd0a67d22fedeca8cdff836d993b to your computer and use it in GitHub Desktop.
Arduino shutter
/**
* @author http://servuc.github.io
*
* This code is in public domain
* It's was create for the instructable :
*
* http://www.instructables.com/id/Arduino-Shutter-Opener
*/
#define SWITCH_DOWN_PIN 2
#define SWITCH_UP_PIN 3
#define LIGHT_SENSOR_PIN 4
#define RELAY_DOWN_PIN 6
#define RELAY_UP_PIN 5
#define SHUTTER_UP 2
#define SHUTTER_DOWN 1
#define SHUTTER_STOP 0
#define MILLI_TIME_TO_MOVE_ALL 5000
#define MILLI_TIME_TO_MOVE_PARTIALLY 500
#define MILLI_TIME_TO_SLEEP 10000
int mPreviousDayLight = LOW;
void setup()
{
//Detect move from wall switch
pinMode(SWITCH_DOWN_PIN, INPUT);
pinMode(SWITCH_UP_PIN, INPUT);
attachInterrupt(SWITCH_DOWN_PIN, loop, CHANGE);
attachInterrupt(SWITCH_UP_PIN, loop, CHANGE);
//Detect light
pinMode(LIGHT_SENSOR_PIN, INPUT);
mPreviousDayLight = digitalRead(LIGHT_SENSOR_PIN);
//Activate relay moving
pinMode(RELAY_DOWN_PIN, OUTPUT);
pinMode(RELAY_UP_PIN, OUTPUT);
//Default position for relay
digitalWrite(RELAY_DOWN_PIN, LOW);
digitalWrite(RELAY_UP_PIN, LOW);
Serial.begin(9600);
}
void actionDownShutter()
{
Serial.println("Shutter down !");
digitalWrite(RELAY_DOWN_PIN, HIGH);
digitalWrite(RELAY_UP_PIN, LOW);
}
void actionHighShutter()
{
Serial.println("Shutter up !");
digitalWrite(RELAY_DOWN_PIN, LOW);
digitalWrite(RELAY_UP_PIN, HIGH);
}
void actionStopShutter()
{
Serial.println("Shutter stop !");
digitalWrite(RELAY_DOWN_PIN, LOW);
digitalWrite(RELAY_UP_PIN, LOW);
}
void loop()
{
//Detect day light
//LOW = night, HIGH = SUN
int lightStatus = digitalRead(LIGHT_SENSOR_PIN);
//Detect physical switch
//If to prevent bug
int switchStatus = SHUTTER_STOP;
if(digitalRead(SWITCH_DOWN_PIN) != digitalRead(SWITCH_UP_PIN)) {
//Get actual position
if(digitalRead(SWITCH_DOWN_PIN) == HIGH) {
switchStatus = SHUTTER_DOWN;
}
if(digitalRead(SWITCH_UP_PIN) == HIGH) {
switchStatus = SHUTTER_UP;
}
}
//Display some informations
Serial.print("Light : ");
Serial.print((lightStatus != HIGH) ? "Sun" : "Moon");
Serial.print(" | Previous : ");
Serial.println((mPreviousDayLight != HIGH) ? "Sun" : "Moon");
Serial.print("Switch : ");
Serial.println(switchStatus);
if(lightStatus != mPreviousDayLight && switchStatus == SHUTTER_STOP)
{
//night
if(lightStatus == HIGH)
{
actionDownShutter();
}
else {
actionHighShutter();
}
delay(MILLI_TIME_TO_MOVE_ALL);
actionStopShutter();
mPreviousDayLight = lightStatus;
}
else
{
//Move to switch position
switch(switchStatus)
{
case SHUTTER_UP :
actionHighShutter();
delay(MILLI_TIME_TO_MOVE_PARTIALLY);
break;
case SHUTTER_DOWN :
actionDownShutter();
delay(MILLI_TIME_TO_MOVE_PARTIALLY);
break;
default :
actionStopShutter();
delay(MILLI_TIME_TO_SLEEP);
break;
}
}
//Prevent from useless looping
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment