Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squarecircleguy/d4f5d41ad30b3ecc95de744d37c535e9 to your computer and use it in GitHub Desktop.
Save squarecircleguy/d4f5d41ad30b3ecc95de744d37c535e9 to your computer and use it in GitHub Desktop.
#include "LowPower.h"
int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
const int analogInPin = A0;
const int speed = 128;
boolean direction = true;
boolean prevdirection = false;
int sensorValue = 0;
int var = 0;
//int outputValue = 0;
void setup()
{
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
//Serial.begin (9600);
//test run setMotor(speed, direction);
//delay(3000);
//setMotor(0, direction);
//delay(3000);
}
void loop()
{
//LowPower.powerDown(SLEEP_8S, ADC_ON, BOD_ON);
var = 0;
while(var < 10){
// do something 10 times
var++;
LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF,
SPI_OFF, USART0_OFF, TWI_OFF);
}
//delay(20);
sensorValue = analogRead(analogInPin);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000);
//Serial.print("sensor = ");
//Serial.println(sensorValue);
//if it is daylight, & the last action is NOT opening, open the door
if (sensorValue >= 350 && prevdirection == direction){
setMotor(speed, direction);
delay(3000);
setMotor(0, direction);
delay(3000);
//switch direction
direction = ! direction;
}
//if it is night, & the last action is NOT closing, close the door
if (sensorValue <= 350 && prevdirection != direction){
setMotor(speed, direction);
delay(3000);
setMotor(0, direction);
delay(3000);
//switch direction
direction = ! direction;
}
}
void setMotor(int speed, boolean direction)
{
analogWrite(enablePin, speed);
digitalWrite(in1Pin, ! direction);
digitalWrite(in2Pin, direction);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment