[QUOTE="windblownsnow;15112393"]Det är väl inga konstigheter egentligen, eller har jag missuppfattat grindens konstruktion? Måste den aktivt hållas öppen hela tiden? Har apparaten persisterande lagringsmedium för att skriva ner diverse olika saker?
Tanken är att om grinden har öpnnats ska den inte direkt stängas om en måln åker förbi och "kyler" ner temperatursensoren.
Om
så betyder det att griden är fullt öppen. Är griden stängd så är digitalRead(2) = 1.
Jag har placerat denna kod i min stora kod. Tror du att denna fungerar? Jag har skapat nu en linjärisering i getTemp();
#include <LedControl.h>
#include <Wire.h>
#include <RTClib.h>
#include <EEPROM.h>
#define SET_TEMP 0
// Define an object
RTC_DS1307 RTC;
// inputs: DIN pin, CLK pin, LOAD pin. number of chips
LedControl mydisplay = LedControl(8, 7, 6, 1);
void setup()
{
mydisplay.shutdown(0, false); // turns on display
mydisplay.setIntensity(0, 15); // 15 = brightest
Wire.begin(); // Begin connection with SDA, SCL
RTC.begin(); // Start RTC-library
pinMode(13, OUTPUT); // Green LED
pinMode(2, INPUT);
digitalWrite(2, 1); // Relay-breake for OPEN
pinMode(1, INPUT);
digitalWrite(1, 1); // Relay-breake for CLOSE
pinMode(9, OUTPUT); // OPEN-relay
pinMode(10, OUTPUT); // CLOSE-relay
digitalWrite(9, 1);
digitalWrite(10, 1);
// Two tiny red LED
pinMode(5, OUTPUT);
digitalWrite(5, 1);
// Red LED
pinMode(3, OUTPUT);
digitalWrite(3, 0); // Red LED OFF
// Green LED
pinMode(4, OUTPUT);
digitalWrite(4, 1); // Green LED ON
if (!RTC.isrunning()) // If the RTC-module is not running
{
RTC.adjust(DateTime(__DATE__, __TIME__)); // Start it
blink_LED(13);
}
}
void loop()
{
/* This is casement state
Show a analog value*/
int state = keypad(); // Get a state from keypad
static int START; // START is default == 0 at the begining
switch (state)
{
case 100: // Stop/Start ALL! Now we can e.g close/open hatch manualy
blink_LED(13);
START = stop_start(START);
break;
case 200: // Set Open hatch-time
if (START == 1)
{
blink_LED(13);
SetOpen();
}
break;
case 300: // Set Close hatch-time
if (START == 1)
{
blink_LED(13);
SetClose();
}
break;
case 400: // Open hatch manualy
if (START == 1)
{
blink_LED(13);
Open_Close();
}
break;
case 500: // Set time
if (START == 1)
{
blink_LED(13);
SetTime();
}
break;
default: // Nothing
break;
}
// START == 0 means that we are in run mode
if (START == 0)
{
show_clock(); // just show the clock
check_time(); // if hatch shall open or not
}
}
void SetTime()
{
mydisplay.setDigit(0, 0, 0, true); // display 0
mydisplay.setDigit(0, 1, 0, true); // -''-
mydisplay.setDigit(0, 2, 0, true); // -''-
mydisplay.setDigit(0, 3, 0, true); // -''-
int abcd[4];
for(int i = 0; i <= 3; i++)
{
int state = keypad();
while(state == 600)
{
state = keypad(); // if state is 600, default. Loop this.
if (state >= 10)
{
// Wrong button pressed. Only 0 to 9.
state = 600;
}
delay(500);
int tinyLED;
if (tinyLED == 0)
{
digitalWrite(5, 1); // two tiny red LED ON
tinyLED = 1;
}
else
{
digitalWrite(5, 0);
tinyLED = 0;
}
}
mydisplay.setDigit(0, i, state, true);
delay(500);
abcd[i] = state; // store
}
// The loop is done!
// Set the time now!
int hh = arraytoint(abcd[0], abcd[1]);
int mm = arraytoint(abcd[2], abcd[3]);
char time_str[10];
sprintf(time_str, "%02d:%02d:00", hh, mm);
RTC.adjust(DateTime("Jan 01 2000", time_str)); // Start it
blink_LED(13);
}
void Open_Close()
{
if(digitalRead(1) == 0 && digitalRead(2) == 1)
{
// Hatch is OPEN.
int break_relay = 1;
while(break_relay == 1)
{
digitalWrite(9, 0); // ON
digitalWrite(10, 1); // OFF
break_relay = digitalRead(2);// if 0, then breake
}
digitalWrite(9, 1); // OFF
}
else if (digitalRead(1) == 1 && digitalRead(2) == 0)
{
// Hatch is CLOSE
int break_relay = 1;
while(break_relay == 1)
{
digitalWrite(10, 0); // ON
digitalWrite(9, 1); // OFF
break_relay = digitalRead(1);// if 0, then breake
}
digitalWrite(10, 1); // OFF
}
else
{
digitalWrite(9, 1); // OFF
digitalWrite(10, 1); // OFF
}
}
void show_clock()
{
// Get the latest time
DateTime current = RTC.now();
int hours = current.hour();
int minutes = current.minute();
if (hours >= 10 && minutes >= 10)
{
String str_hour = String(hours);
String str_minutes = String(minutes);
char buf_h[3];
char buf_m[3];
str_hour.toCharArray(buf_h, 3);
str_minutes.toCharArray(buf_m, 3);
int a = buf_h[0] - '0';
int b = buf_h[1] - '0';
int c = buf_m[0] - '0';
int d = buf_m[1] - '0';
mydisplay.setDigit(0, 0, a, true);
mydisplay.setDigit(0, 1, b, true);
mydisplay.setDigit(0, 2, c, true);
mydisplay.setDigit(0, 3, d, true);
}
else if (hours < 10 && minutes >= 10)
{
String str_hour = String(hours);
String str_minutes = String(minutes);
char buf_h[3];
char buf_m[3];
str_hour.toCharArray(buf_h, 3);
str_minutes.toCharArray(buf_m, 3);
int a = buf_h[0] - '0';
int c = buf_m[0] - '0';
int d = buf_m[1] - '0';
mydisplay.setDigit(0, 0, 0, true);
mydisplay.setDigit(0, 1, a, true);
mydisplay.setDigit(0, 2, c, true);
mydisplay.setDigit(0, 3, d, true);
}
else if (hours < 10 && minutes < 10)
{
String str_hour = String(hours);
String str_minutes = String(minutes);
char buf_h[3];
char buf_m[3];
str_hour.toCharArray(buf_h, 3);
str_minutes.toCharArray(buf_m, 3);
int a = buf_h[0] - '0';
int c = buf_m[0] - '0';
mydisplay.setDigit(0, 0, 0, true);
mydisplay.setDigit(0, 1, a, true);
mydisplay.setDigit(0, 2, 0, true);
mydisplay.setDigit(0, 3, c, true);
}
else if (hours >= 10 && minutes < 10)
{
String str_hour = String(hours);
String str_minutes = String(minutes);
char buf_h[3];
char buf_m[3];
str_hour.toCharArray(buf_h, 3);
str_minutes.toCharArray(buf_m, 3);
int a = buf_h[0] - '0';
int b = buf_h[1] - '0';
int c = buf_m[0] - '0';
mydisplay.setDigit(0, 0, a, true);
mydisplay.setDigit(0, 1, b, true);
mydisplay.setDigit(0, 2, 0, true);
mydisplay.setDigit(0, 3, c, true);
}
}
boolean getTemp()
{
int reading = analogRead(1); // analog pin 1
float voltage = reading * 5.0;
voltage = voltage/1024;
float temperatureC = (voltage - 0.5) * 100;
if (temperatureC > SET_TEMP) // #definie SET_TEMP 0
{
return true;
}
else
{
return false;
}
}
void check_time()
{
// read the eeprom memory
int open_hour = EEPROM.read(0);
int open_minute = EEPROM.read(1);
int close_hour = EEPROM.read(2);
int close_minute = EEPROM.read(3);
// Get real time
DateTime current = RTC.now();
int hours = current.hour();
int minutes = current.minute();
// convert to minutes for easier comparison
int open_in_minutes = 60 * open_hour + open_minute;
int close_in_minutes = 60 * close_hour + close_minute;
int now_in_minutes = 60 * hours + minutes;
int Gate_Open;
static int LOCK; // if the gate is open and temperatur sinks below 0, the gate should not close.
if (open_in_minutes > close_in_minutes) // Exemple: 08:30 > 01:20
{
if ((now_in_minutes >= open_in_minutes) || (now_in_minutes < close_in_minutes))
{
// Example: Gate is open between e.g 09:30 >= 8:00 or 00:03 < 01:00
Gate_Open = 1; // Open the gate now!
}
else
{
Gate_Open = 0; // Close the gate now!
}
}
if (open_in_minutes <= close_in_minutes) // Example: 08:30 < 23:00
{
if ((now_in_minutes >= open_in_minutes) && (now_in_minutes < close_in_minutes))
{
// Example: Gate is open between e.g 09:30 >= 08:30 and 09:30 < 23:45
Gate_Open = 1; // Open the gate now!
}
else
{
Gate_Open = 0; // Close the gate now!
}
}
// check temperature
boolean temp = getTemp();
if (Gate_Open == 1 && temp == true && LOCK == 0)
{
// It's opening time, the temp is OK and the gate is not open
Gate_Open = 1; // Gate should be open
}
else // if temp is not true and gate is open
{
if (LOCK == 1 && Gate_Open == 1) // gate is already open
{
Gate_Open == 1; // Gate should be open
}
else // Gate is not already open
{
Gate_Open = 0; // Gate should be closed
}
}
if (Gate_Open == 1) // open time!
{
int break_relay = digitalRead(1); // if 0, then breake
if (break_relay == 0)
{
LOCK = 1; // The gate is full open
}
else
{
LOCK = 0; // The gate is not full open
}
if (break_relay == 1)
{
digitalWrite(9, 0); // ON
digitalWrite(10, 1); // OFF - just for sure
}
else
{
digitalWrite(9, 1); // OFF
// Now the gate is open
}
}
else // close time!
{
int break_relay = digitalRead(2);
if (break_relay == 1)
{
digitalWrite(10, 0); // ON
digitalWrite(9, 1); // OFF - just for sure
}
else
{
digitalWrite(10, 1); // OFF
LOCK = 0; // The gate is now 100 % closed
}
}
}
void SetClose()
{
mydisplay.setDigit(0, 0, 0, true); // display 0
mydisplay.setDigit(0, 1, 0, true); // -''-
mydisplay.setDigit(0, 2, 0, true); // -''-
mydisplay.setDigit(0, 3, 0, true); // -''-
int abcd[4];
for(int i = 0; i <= 3; i++)
{
int state = keypad();
while(state == 600)
{
state = keypad(); // if state is 600, default. Loop this.
if (state >= 10)
{
// Wrong button pressed. Only 0 to 9.
state = 600;
}
delay(500);
int tinyLED;
if (tinyLED == 0)
{
digitalWrite(5, 1); // two tiny red LED ON
tinyLED = 1;
}
else
{
digitalWrite(5, 0);
tinyLED = 0;
}
}
mydisplay.setDigit(0, i, state, true);
delay(500);
abcd[i] = state; // store
}
// The loop is done!
// Add the adcd array to EEPROM
EEPROM.write(2, arraytoint(abcd[0], abcd[1])); // hour hh
EEPROM.write(3, arraytoint(abcd[2], abcd[3])); // minute mm
blink_LED(13);
}
void SetOpen()
{
mydisplay.setDigit(0, 0, 0, true); // display 0
mydisplay.setDigit(0, 1, 0, true); // -''-
mydisplay.setDigit(0, 2, 0, true); // -''-
mydisplay.setDigit(0, 3, 0, true); // -''-
int abcd[4];
for(int i = 0; i <= 3; i++)
{
int state = keypad();
while(state == 600)
{
state = keypad(); // if state is 600, default. Loop this.
if (state >= 10)
{
// Wrong button pressed. Only 0 to 9.
state = 600;
}
delay(500);
int tinyLED;
if (tinyLED == 0)
{
digitalWrite(5, 1); // two tiny red LED ON
tinyLED = 1;
}
else
{
digitalWrite(5, 0);
tinyLED = 0;
}
}
mydisplay.setDigit(0, i, state, true);
delay(500);
abcd[i] = state; // store
}
// The loop is done!
// Add the adcd array to EEPROM
EEPROM.write(0, arraytoint(abcd[0], abcd[1])); // hour hh
EEPROM.write(1, arraytoint(abcd[2], abcd[3])); // minute mm
blink_LED(13);
}
int stop_start(int START)
{
if (START == 1)
{
// Run mode
START = 0;
digitalWrite(4, 1); // Green LED ON
digitalWrite(3, 0); // Red LED OFF
}
else
{
// Configuration mode
START = 1;
digitalWrite(4, 0); // Green LED OFF
digitalWrite(3, 1); // Red LED ON
}
return START;
}
void blink_LED(int LED)
{
int A = 1;
for (int i = 0; i <= 10; i++)
{
digitalWrite(LED, A);
delay(100);
if (A == 1)
{
A = 0;
}
else
{
A = 1;
}
}
}
int arraytoint(int x, int y)
{
String str_x = String(x);
String str_y = String(y);
String str_x_y = String(str_x + str_y);
return str_x_y.toInt();
}
int keypad()
{
int i = analogRead(0); // Get an analog value from keypad
if (i < 1000 && i > 970)
{
// Stop button
digitalWrite(13, 1);
return 100;
}
else if (i < 930 && i > 900)
{
// 0 button
digitalWrite(13, 1);
return 0;
}
else if (i < 870 && i > 830)
{
// "Set Open hatch time" button
digitalWrite(13, 1);
return 200;
}
else if (i < 800 && i > 750)
{
// 9 button
digitalWrite(13, 1);
return 9;
}
else if (i < 730 && i > 700)
{
// 8 button
digitalWrite(13, 1);
return 8;
}
else if (i < 670 && i > 620)
{
// 7 button
digitalWrite(13, 1);
return 7;
}
else if (i < 600 && i > 550)
{
// "Set Close hatch time" button
digitalWrite(13, 1);
return 300;
}
else if (i < 530 && i > 480)
{
// 6 button
digitalWrite(13, 1);
return 6;
}
else if (i < 450 && i > 420)
{
// 5 button
digitalWrite(13, 1);
return 5;
}
else if (i < 410 && i > 380)
{
// Both 4 and 6 are pressed
digitalWrite(13, 1);
return 500;
}
else if (i < 360 && i > 320)
{
// 4 button
digitalWrite(13, 1);
return 4;
}
else if (i < 300 && i > 250)
{
// "Open/Close hatch manaly" button
digitalWrite(13, 1);
return 400;
}
else if (i < 200 && i > 150)
{
// 3 button
digitalWrite(13, 1);
return 3;
}
else if (i < 120 && i > 70)
{
// 2 button
digitalWrite(13, 1);
return 2;
}
else if (i < 50 && i >= 0)
{
// 1 button
digitalWrite(13, 1);
return 1;
}
else
{
// Do nothing
digitalWrite(13, 0);
return 600;
}
}