full remaking

This commit is contained in:
ivan 2018-03-22 19:26:11 +03:00
parent 47c0b33de8
commit ec9e35e700
3 changed files with 54 additions and 34 deletions

Binary file not shown.

View File

@ -1,5 +1,5 @@
/* /*
* Hencoop with automatic lamp and door timer * Hencoop with automatic lamp and door timer, TM1637 display and two buttons
*/ */
#include <Arduino.h> #include <Arduino.h>
@ -7,47 +7,55 @@
#include <TimeLib.h> #include <TimeLib.h>
#include <Thread.h> #include <Thread.h>
#include <DS1307RTC.h> #include <DS1307RTC.h>
#include <SevenSegmentExtended.h> // Extended TM1637 library https://github.com/bremme/arduino-tm1637
long openDoorVar = 3900; // Interval for door opening long openDoorVar = 3780; // Interval for door opening
long closeDoorVar = 3200; // Interval for door closing long closeDoorVar = 3600; // Interval for door closing
const byte rotatePlus = 2; // Motor relay control const byte PIN_CLK = 2; // Define CLK pin (for 4-Digit Display)
const byte rotateMinus = 3; // Motor relay control const byte PIN_DIO = 3; // Define DIO pin (for 4-Digit Display)
const byte redButton = 4; // RedButton: light on, open door const byte redButton = 4; // RedButton: light on, open door
const byte blackButton = 5; // BlackButton: light off, close door const byte blackButton = 5; // BlackButton: light off, close door
const byte pinDC = 6; // DC motor relay const byte pinLight = 6; // Light relay
const byte pinMotor = 7; // Temporarily motor relay activation (without diods) const byte pinDC = 7; // DC motor relay
const byte pinLight = 8; // Light relay const byte pinRelay3 = 8; // Motor relay control
const byte pinRelay4 = 9; // Motor relay control
const byte led = 13; const byte led = 13;
long ledPulse = 1000; // Interval for LED blinking pulse SevenSegmentExtended display(PIN_CLK, PIN_DIO);
// long ledPulse = 1000; // Interval for LED blinking pulse
long buttonCheck = 200; // Interval for checking button state long buttonCheck = 200; // Interval for checking button state
byte buttonCommand = 0; // Variable for buttons value: 0 - nothing, 1 - light on, 2 - light off byte buttonCommand = 0; // Variable for buttons value: 0 - nothing, 1 - light on, 2 - light off
byte displayWork = 0; // Variable for display status: 0 - nothing, 1 - work
long previousButtonMillis = 0; // Button previous press counter long previousButtonMillis = 0; // Button previous press counter
long buttonPressed = 0; // Button ms pressed counter long buttonPressed = 0; // Button ms pressed counter
long buttonLongPress = 5000; // Interval for long press button action long buttonLongPress = 5000; // Interval for long press button action
long buttonShortPress = 200; // Interval for short press button action long buttonShortPress = 600; // Interval for short press button action
unsigned long lastButtonPressed;
// Threads: // Threads:
Thread ledThread = Thread(); // Create thread for LED pulse indication
Thread pressButtonThread = Thread(); // Create thread for button state checking Thread pressButtonThread = Thread(); // Create thread for button state checking
// Thread ledThread = Thread(); // Create thread for LED pulse indication
void setup() { void setup() {
Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug Serial.begin(9600); // Initializes the Serial connection @ 9600 baud for debug
serStr("starting setup..."); serStr("starting setup...");
display.begin(); // Initializes the display
display.setBacklight(100); // Set the brightness to 100 %
display.print("INIT"); // Display INIT on the display
pinMode(rotatePlus, OUTPUT); pinMode(pinRelay3, OUTPUT);
pinMode(rotateMinus, OUTPUT); pinMode(pinRelay4, OUTPUT);
pinMode(pinDC, OUTPUT); pinMode(pinDC, OUTPUT);
pinMode(pinMotor, OUTPUT); // Temporarily (without diods)
pinMode(pinLight, OUTPUT); pinMode(pinLight, OUTPUT);
pinMode(led, OUTPUT); pinMode(led, OUTPUT);
pinMode(redButton, INPUT); pinMode(redButton, INPUT);
pinMode(blackButton, INPUT); pinMode(blackButton, INPUT);
digitalWrite(rotatePlus, HIGH); digitalWrite(pinRelay3, HIGH);
digitalWrite(rotateMinus, HIGH); digitalWrite(pinRelay4, HIGH);
digitalWrite(pinDC, HIGH); // Temporarily (without diods) digitalWrite(pinDC, HIGH);
digitalWrite(pinMotor, HIGH);
digitalWrite(pinLight, HIGH); digitalWrite(pinLight, HIGH);
while (!Serial); // Wait until Arduino Serial Monitor opens while (!Serial); // Wait until Arduino Serial Monitor opens
@ -58,24 +66,31 @@ void setup() {
Serial.println("RTC has set the system time"); Serial.println("RTC has set the system time");
// LED blinking thread: // LED blinking thread:
ledThread.onRun(ledBlink); // ledThread.onRun(ledBlink);
ledThread.setInterval(ledPulse); // Interval for LED blinking // ledThread.setInterval(ledPulse); // Interval for LED blinking
// Button state cheking thread: // Button state cheking thread:
pressButtonThread.onRun(pressButton); pressButtonThread.onRun(pressButton);
pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing pressButtonThread.setInterval(buttonCheck); // Interval for checking button pressing
delay (1000);
display.off();
serStr("...setup finished"); serStr("...setup finished");
} }
void loop() { void loop() {
// Threads init: // Threads init:
if (ledThread.shouldRun()) // if (ledThread.shouldRun())
ledThread.run(); // ledThread.run();
if (pressButtonThread.shouldRun()) if (pressButtonThread.shouldRun())
pressButtonThread.run(); pressButtonThread.run();
if (timeStatus() == timeSet) { // If RTC works - call the checkTime function if (timeStatus() == timeSet) { // If RTC works - call the checkTime function
checkTime(); checkTime();
} else {
display.on();
display.print("SET TIME");
}
/*
} else { // Fast blinking indicates that time is } else { // Fast blinking indicates that time is
int i; // not set or DS1307RTC is not plugged int i; // not set or DS1307RTC is not plugged
for (i = 1; i <= 500; i++) { for (i = 1; i <= 500; i++) {
@ -85,6 +100,7 @@ void loop() {
delay(50); delay(50);
} }
} }
*/
} }
void checkTime() { void checkTime() {
@ -174,17 +190,29 @@ void checkTime() {
} }
} }
/*
// LED pulse blinking thread // LED pulse blinking thread
void ledBlink() { void ledBlink() {
static bool ledStatus = false; // LED status bool static bool ledStatus = false; // LED status bool
ledStatus = !ledStatus; // Invert LED state ledStatus = !ledStatus; // Invert LED state
digitalWrite(led, ledStatus); // Activate LED state digitalWrite(led, ledStatus); // Activate LED state
} }
*/
// Check button pressing thread // Check button pressing thread
void pressButton() { void pressButton() {
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
if (((currentMillis - previousButtonMillis) > 20000) && (displayWork == 1)) {
display.off();
displayWork = 0;
}
if (digitalRead(redButton) == HIGH || digitalRead(blackButton) == HIGH) { if (digitalRead(redButton) == HIGH || digitalRead(blackButton) == HIGH) {
// if (digitalRead(redButton) == HIGH) serStr("Red button pressed, time showed");
// if (digitalRead(blackButton) == HIGH) serStr("Black button pressed, time showed");
display.on();
displayWork = 1;
display.printTime(hour(), minute(), true);
buttonPressed = buttonPressed + 200; buttonPressed = buttonPressed + 200;
if (buttonPressed > buttonShortPress) { if (buttonPressed > buttonShortPress) {
if (digitalRead(redButton) == HIGH) buttonCommand = 1; if (digitalRead(redButton) == HIGH) buttonCommand = 1;
@ -224,33 +252,25 @@ void lightOff() {
void openDoor() { void openDoor() {
serStr("Door opening started..."); serStr("Door opening started...");
digitalWrite(rotatePlus, LOW);
digitalWrite(rotateMinus, LOW);
digitalWrite(pinDC, LOW); // DC on digitalWrite(pinDC, LOW); // DC on
delay(1000); delay(1000);
digitalWrite(pinMotor, LOW); // Motor on (temporarily, without diods) digitalWrite(pinRelay4, LOW);
delay(openDoorVar); delay(openDoorVar);
digitalWrite(pinMotor, HIGH); // Motor off (temporarily, without diods) digitalWrite(pinRelay4, HIGH);
delay(1000); delay(1000);
digitalWrite(pinDC, HIGH); // DC off digitalWrite(pinDC, HIGH); // DC off
digitalWrite(rotatePlus, HIGH);
digitalWrite(rotateMinus, HIGH);
serStr("...door opening finished"); serStr("...door opening finished");
} }
void closeDoor() { void closeDoor() {
serStr("Door closing started..."); serStr("Door closing started...");
digitalWrite(rotatePlus, HIGH);
digitalWrite(rotateMinus, HIGH);
digitalWrite(pinDC, LOW); // DC on digitalWrite(pinDC, LOW); // DC on
delay(1000); delay(1000);
digitalWrite(pinMotor, LOW); // Motor on (temporarily, without diods) digitalWrite(pinRelay3, LOW);
delay(closeDoorVar); delay(closeDoorVar);
digitalWrite(pinMotor, HIGH); // Motor off (temporarily, without diods) digitalWrite(pinRelay3, HIGH);
delay(1000); delay(1000);
digitalWrite(pinDC, HIGH); // DC off digitalWrite(pinDC, HIGH); // DC off
digitalWrite(rotatePlus, HIGH);
digitalWrite(rotateMinus, HIGH);
serStr("...door closing finished"); serStr("...door closing finished");
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 MiB

After

Width:  |  Height:  |  Size: 234 KiB