Re: Automatické zatváranie kurníku pre sliepky
Napsal: 30 dub 2020, 12:52
Přeci jen je potřeba doladit jednu drobnost. Po zapnutí do sítě se motor točí bez ohledu na ldr čidlo a polohu dveří. Dumám, jak docílit toho, aby motor po zapnutí stál až do prvního čtení světla..
Kód: Vybrat vše
const int EnMotorPin = 9; // enable motor - pin 9 PWM
const int CloseDoorPin = 8; // direction close motor - pin 8
const int OpenDoorPin = 7; // direction open motor - pin 7
const int photocellPin = A0; // photocell connected to analog 0
const long eventTime_1_LDR = 20000; // 20 vteřin
unsigned long previousTime_1 = 0;
int photocellVal; // analog reading of the photocel
bool daylight = true; // daylight status
int darkthreshold = 20;
int daythreshold = 350;
int systemState = 0;
int SPEED = 220;
int bottomSwitchPin = 2; // Reed switches top and bottom of coop door
int topSwitchPin = 4;
bool topSwitchState; // true if door is up
bool bottomSwitchState; // true if door is down
void setup() {
Serial.begin(9600);
pinMode(bottomSwitchPin, INPUT);
pinMode(topSwitchPin, INPUT);
pinMode(EnMotorPin, OUTPUT);
pinMode(CloseDoorPin, OUTPUT);
pinMode(OpenDoorPin, OUTPUT);
}
void loop() {
systemState = 0;
unsigned long currentTime = millis();
if ( currentTime - previousTime_1 >= eventTime_1_LDR) {
photocellVal = analogRead(photocellPin); // read inputs
previousTime_1 = currentTime;
}
if (photocellVal > daythreshold)
{
daylight = true;
}
if (photocellVal < darkthreshold)
{
daylight = false;
}
topSwitchState = digitalRead(topSwitchPin);
bottomSwitchState = digitalRead(bottomSwitchPin);
// read system status
if (daylight == true && topSwitchState == true)
{
systemState = 0; // motor stop
}
if (daylight == true && topSwitchState == false)
{
systemState = 1; // drive door up
}
if (daylight == false && bottomSwitchState == true)
{
systemState = 0; // motor stop
}
if (daylight == false && bottomSwitchState == false)
{
systemState = 2; // drive door down
}
DispVals();
switch (systemState) {
case 0: // motor stop
digitalWrite(CloseDoorPin, LOW);
digitalWrite(OpenDoorPin, LOW);
analogWrite(EnMotorPin, 0);
break;
case 1: //drive door up
digitalWrite(CloseDoorPin, LOW);
digitalWrite(OpenDoorPin, HIGH);
analogWrite(EnMotorPin, SPEED);
break;
case 2: // drive door down
digitalWrite(CloseDoorPin, HIGH);
digitalWrite(OpenDoorPin, LOW);
analogWrite(EnMotorPin, SPEED);
break;
default:
digitalWrite(CloseDoorPin, LOW);
digitalWrite(OpenDoorPin, LOW);
analogWrite(EnMotorPin, 0);
break;
}
}
void DispVals()
{
Serial.print("topSW ");
Serial.print(topSwitchState);
Serial.print("\t");
Serial.print("botSW ");
Serial.print(bottomSwitchState);
Serial.print("\t");
Serial.print("PhotoVal ");
Serial.print(photocellVal);
Serial.print("\t");
Serial.print("daylight ");
Serial.print(daylight);
Serial.print("\t");
Serial.print("systemState ");
Serial.print(systemState);
Serial.print("\t");
if (systemState == 0)
{
Serial.println("\t door stopped");
}
if (systemState == 1)
{
Serial.println("\t door driving up");
}
if (systemState == 2)
{
Serial.println("\t door driving down");
}
}