Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Locking, Automatic Chicken Coop Door
- //By Seth Johnson Land To House llc 2016
- //This section is for initializing things:
- // Initialize the motor values to the pins 8, 9, 10.
- //Pins 8 and 9 send high or low to the motor controller.
- //pin 10 enables motor one on the motor controller.
- int enA = 10;
- int in1 = 9;
- int in2 = 8;
- //Initialize "lightSensor" as the value from pin A0 and read in the value. This is the photoresistor.
- int lightSensor = analogRead(A0);
- //The lightVal will hold the value of lightsensor in this variable
- int lightVal = 0;
- //These are the pins for the reed switches
- // reed1Pin is the lower switch on the door. This is digital pin 2
- int reed1Pin = 2;
- //reed2Pin is the top switch on the door. This is digital pin 4
- int reed2Pin = 4;
- //These are the variables that hold the state of the reed switches
- int switchState1 = 0;
- int switchState2 = 0;
- //This only runs once.
- #include <Wire.h>
- #include "ds3231.h"
- #include <LiquidCrystal_I2C.h>
- LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // Set the LCD I2C address
- #define BUFF_MAX 128
- uint8_t time[8];
- char recv[BUFF_MAX];
- unsigned int recv_size = 0;
- unsigned long prev, interval = 1000;
- void setup()
- {
- Serial.begin(9600);
- Wire.begin();
- DS3231_init(DS3231_INTCN);
- memset(recv, 0, BUFF_MAX);
- Serial.println("GET time");
- lcd.begin (16,2); // for 16 x 2 LCD module
- lcd.setBacklightPin(3,POSITIVE);
- lcd.setBacklight(HIGH);
- lcd.clear();
- // set the motor control pins as outputs. This means pins 8, 9, 10 are outputs to the l298n motor controller.
- pinMode(enA, OUTPUT);
- pinMode(in1, OUTPUT);
- pinMode(in2, OUTPUT);
- //read the state of switch 1 (the bottom one) and place it in switchState1
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- //this is important to make sure that the door starts up when the program gains power.
- //if switchState2 is not high then go to the while statement
- if (switchState2 != HIGH)
- {
- // this function will run the motor down as long as switch 1 has not been triggered
- while (switchState2 != HIGH)
- {
- // turn on motor and set speed to 255
- analogWrite(enA, 255);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- //read the state of the switch again to make sure that it has not been triggered
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- }
- // once switchstate2 has been triggered turn off the motor
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- }
- }
- //this runs over and over
- void loop()
- {
- //read the light sensor and place it in lightval
- lightVal = analogRead(lightSensor);
- //read the state of switch 1 (the bottom one) and place it in switchState1
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- //the lightSensor is read and placed into lightVal. if its less than 200 and switchState2 is
- //equal to high then go to the motor down code. But if the light is greater than 200 and the switchstate1
- //is equal to high then call motor up code
- if (switchState2 = HIGH && lightVal < 200)
- {
- delay(2000);
- motordown();
- }
- else if (switchState1 = HIGH && lightVal > 200)
- {
- delay(2000);
- motorup();
- }
- }
- void motordown()
- {
- //Read the state of switch 1 (the Bottom one) and place it in switchState1
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- //If switchState2 is high and the light is dark then continue
- if (switchState2 = HIGH && lightVal < 200)
- //wait 2 seconds
- delay(2000);
- //read the state of switch 1 (the bottom one) and place it in switchState1
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- while (switchState1 != HIGH)
- {
- // turn on motor and set speed to 255
- analogWrite(enA, 255);
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- }
- //wait 1 second before turning off the motor to let the locks engage at the bottom
- delay(1000);
- // now turn off motor
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- }
- void motorup()
- {
- //read the state of switch 1 (the bottom one) and place it in switchState2
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- //if switchState1 is high and the light is bright then continue
- if (switchState1 = HIGH && lightVal > 200)
- {
- //read the state of switch 1 (the bottom one) and place it in switchState1
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- //delay 2 seconds
- delay(2000);
- //while switchState2 is not high turn on the motor up
- while (switchState2 != HIGH)
- {
- // this function will run the motor as long as switch 2 has not been triggered
- // turn on motor and set speed to 255
- analogWrite(enA, 255);
- digitalWrite(in1, LOW);
- digitalWrite(in2, HIGH);
- //read the state of switch 1 (the bottom one) and place it in switchState2
- switchState1 = digitalRead(reed1Pin);
- //read the state of switch 2 (the top one) and place it in switchState2
- switchState2 = digitalRead(reed2Pin);
- }
- // now turn off motor
- digitalWrite(in1, LOW);
- digitalWrite(in2, LOW);
- }
- }
- {
- char in;
- char tempF[6];
- char temperature;
- char buff[BUFF_MAX];
- unsigned long now = millis();
- struct ts t;
- // show time once in a while
- if ((now - prev > interval) && (Serial.available() <= 0)) {
- DS3231_get(&t); //Get time
- parse_cmd("F",1);
- temperature = DS3231_get_treg(); //Get temperature
- dtostrf(temperature, 5, 1, tempF);
- lcd.clear();
- lcd.setCursor(1,0);
- lcd.print(t.mday);
- printMonth(t.mon);
- lcd.print(t.year);
- lcd.setCursor(0,1); //Go to second line of the LCD Screen
- lcd.print(t.hour);
- lcd.print(":");
- if(t.min<10)
- lcd.print("0");
- }
- lcd.print(t.min);
- lcd.print(":");
- if(t.sec<10)
- {
- lcd.print("0");
- }
- lcd.print(t.sec);
- lcd.print(' ');
- lcd.print(tempF);
- lcd.print((char)223);
- lcd.print("F ");
- prev = now;
- }
- if (Serial.available() > 0) {
- in = Serial.read();
- if ((in == 10 || in == 13) && (recv_size > 0)) {
- parse_cmd(recv, recv_size);
- recv_size = 0;
- recv[0] = 0;
- } else if (in < 48 || in > 122) {; // ignore ~[0-9A-Za-z]
- } else if (recv_size > BUFF_MAX - 2) { // drop lines that are too long
- // drop
- recv_size = 0;
- recv[0] = 0;
- } else if (recv_size < BUFF_MAX - 2) {
- recv[recv_size] = in;
- recv[recv_size + 1] = 0;
- recv_size += 1;
- }
- }
- }
- void parse_cmd(char *cmd, int cmdsize)
- {
- uint8_t i;
- uint8_t reg_val;
- char buff[BUFF_MAX];
- struct ts t;
- //snprintf(buff, BUFF_MAX, "cmd was '%s' %d\n", cmd, cmdsize);
- //Serial.print(buff);
- // TssmmhhWDDMMYYYY aka set time
- if (cmd[0] == 84 && cmdsize == 16) {
- //T001608603032017
- t.sec = inp2toi(cmd, 1);
- t.min = inp2toi(cmd, 3);
- t.hour = inp2toi(cmd, 5);
- t.wday = inp2toi(cmd, 7);
- t.mday = inp2toi(cmd, 8);
- t.mon = inp2toi(cmd, 10);
- t.year = inp2toi(cmd, 12) * 100 + inp2toi(cmd, 14);
- DS3231_set(t);
- Serial.println("OK");
- } else if (cmd[0] == 49 && cmdsize == 1) { // "1" get alarm 1
- DS3231_get_a1(&buff[0], 59);
- Serial.println(buff);
- } else if (cmd[0] == 50 && cmdsize == 1) { // "2" get alarm 1
- DS3231_get_a2(&buff[0], 59);
- Serial.println(buff);
- } else if (cmd[0] == 51 && cmdsize == 1) { // "3" get aging register
- Serial.print("aging reg is ");
- Serial.println(DS3231_get_aging(), DEC);
- } else if (cmd[0] == 65 && cmdsize == 9) { // "A" set alarm 1
- DS3231_set_creg(DS3231_INTCN | DS3231_A1IE);
- //ASSMMHHDD
- for (i = 0; i < 4; i++) {
- time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // ss, mm, hh, dd
- }
- byte flags[5] = { 0, 0, 0, 0, 0 };
- DS3231_set_a1(time[0], time[1], time[2], time[3], flags);
- DS3231_get_a1(&buff[0], 59);
- Serial.println(buff);
- } else if (cmd[0] == 66 && cmdsize == 7) { // "B" Set Alarm 2
- DS3231_set_creg(DS3231_INTCN | DS3231_A2IE);
- //BMMHHDD
- for (i = 0; i < 4; i++) {
- time[i] = (cmd[2 * i + 1] - 48) * 10 + cmd[2 * i + 2] - 48; // mm, hh, dd
- }
- byte flags[5] = { 0, 0, 0, 0 };
- DS3231_set_a2(time[0], time[1], time[2], flags);
- DS3231_get_a2(&buff[0], 59);
- Serial.println(buff);
- } else if (cmd[0] == 67 && cmdsize == 1) { // "C" - get temperature register
- Serial.print("temperature reg is ");
- Serial.println(DS3231_get_treg(), DEC);
- } else if (cmd[0] == 68 && cmdsize == 1) { // "D" - reset status register alarm flags
- reg_val = DS3231_get_sreg();
- reg_val &= B11111100;
- DS3231_set_sreg(reg_val);
- } else if (cmd[0] == 70 && cmdsize == 1) { // "F" - custom fct
- reg_val = DS3231_get_addr(0x5);
- Serial.print("orig ");
- Serial.print(reg_val,DEC);
- Serial.print("month is ");
- Serial.println(bcdtodec(reg_val & 0x1F),DEC);
- } else if (cmd[0] == 71 && cmdsize == 1) { // "G" - set aging status register
- DS3231_set_aging(0);
- } else if (cmd[0] == 83 && cmdsize == 1) { // "S" - get status register
- Serial.print("status reg is ");
- Serial.println(DS3231_get_sreg(), DEC);
- } else {
- Serial.print("unknown command prefix ");
- Serial.println(cmd[0]);
- Serial.println(cmd[0], DEC);
- }
- }
- void printMonth(int month)
- {
- switch(month)
- {
- case 1: lcd.print(" January ");break;
- case 2: lcd.print(" February ");break;
- case 3: lcd.print(" March ");break;
- case 4: lcd.print(" April ");break;
- case 5: lcd.print(" May ");break;
- case 6: lcd.print(" June ");break;
- case 7: lcd.print(" July ");break;
- case 8: lcd.print(" August ");break;
- case 9: lcd.print(" September ");break;
- case 10: lcd.print(" October ");break;
- case 11: lcd.print(" November ");break;
- case 12: lcd.print(" December ");break;
- default: lcd.print(" Error ");break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment