Created
April 22, 2016 22:33
-
-
Save rafacouto/29d059be42869e296b9289d41dba9feb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Arduino pin config | |
#define PIN_M1 5 | |
#define PIN_M2 4 | |
#define PIN_M3 3 | |
#define PIN_M4 2 | |
#define PIN_LDR A4 | |
#define PIN_HES A7 | |
// LDR config | |
#define LDR_DAY 100 | |
#define LDR_NIGHT 110 | |
#define LDR_TEST_SECONDS 10 | |
#define LDR_TEST_COUNT 18 | |
////////////////////////////////////////////////// | |
// end of config | |
////////////////////////////////////////////////// | |
// other defines | |
#define DIR_OPEN -1 | |
#define DIR_CLOSE 1 | |
////////////////////////////////////////////////// | |
// motor movement | |
////////////////////////////////////////////////// | |
const int motor_steps[][4] = { | |
{HIGH, LOW , LOW , LOW }, | |
{HIGH, HIGH, LOW , LOW }, | |
{LOW , HIGH, LOW , LOW }, | |
{LOW , HIGH, HIGH, LOW }, | |
{LOW , LOW , HIGH, LOW }, | |
{LOW , LOW , HIGH, HIGH}, | |
{LOW , LOW , LOW , HIGH}, | |
{HIGH, LOW , LOW , HIGH} | |
}; | |
void motorStep(int dir) { | |
static int motor_step_idx = 0; | |
motor_step_idx += dir; | |
motor_step_idx = (motor_step_idx + 8) % 8; | |
digitalWrite(PIN_M1, motor_steps[motor_step_idx][0]); | |
digitalWrite(PIN_M2, motor_steps[motor_step_idx][1]); | |
digitalWrite(PIN_M3, motor_steps[motor_step_idx][2]); | |
digitalWrite(PIN_M4, motor_steps[motor_step_idx][3]); | |
} | |
////////////////////////////////////////////////// | |
// light detection | |
////////////////////////////////////////////////// | |
bool testIsDay() { | |
static bool isDay = true; | |
static int ldr_last[LDR_TEST_COUNT]; | |
static int ldr_last_idx = 0; | |
static long ldr_last_millis = millis(); | |
long now = millis(); | |
if (now > ldr_last_millis && // avoid millis() overflow | |
now - ldr_last_millis > LDR_TEST_SECONDS * 1000) { | |
ldr_last_millis = now; | |
int ldr_val = analogRead(PIN_LDR); | |
ldr_last[ldr_last_idx++] = ldr_val; | |
if (ldr_last_idx == LDR_TEST_COUNT) { | |
ldr_last_idx = 0; | |
int avg = 0; | |
for (int i = 0; i < LDR_TEST_COUNT; i++) avg += ldr_last[i]; | |
avg /= LDR_TEST_COUNT; | |
if (isDay && (avg > LDR_NIGHT)) | |
isDay = false; | |
else if (!isDay && (avg < LDR_DAY)) | |
isDay = true; | |
} | |
} | |
return isDay; | |
} | |
////////////////////////////////////////////////// | |
// door limits | |
////////////////////////////////////////////////// | |
bool testDoorIsOpen() { | |
// TODO: HES sensor | |
} | |
bool testDoorIsClosed() { | |
// TODO: motor steps? | |
} | |
////////////////////////////////////////////////// | |
// main program | |
////////////////////////////////////////////////// | |
void setup() { | |
Serial.begin(9600); | |
pinMode(PIN_M1, OUTPUT); | |
pinMode(PIN_M2, OUTPUT); | |
pinMode(PIN_M3, OUTPUT); | |
pinMode(PIN_M4, OUTPUT); | |
pinMode(PIN_LDR, INPUT_PULLUP); | |
pinMode(PIN_HES, INPUT); | |
} | |
void loop() { | |
int direction = (testIsDay() ? DIR_OPEN : DIR_CLOSE); | |
if (!testDoorIsOpen() && direction == DIR_OPEN) { | |
// TODO: activate buzzers | |
motorStep(DIR_OPEN); | |
} else if (!testDoorIsClosed() && direction == DIR_CLOSE) { | |
motorStep(DIR_CLOSE); | |
} | |
// don't burn the CPU | |
delay(50); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment