//Usa automata pentru cotetul gainilor
// sketch original: Seth Johnson Land To House llc 2016
// modificat: Pekatonix 2023
int enA = 10; //pinul D10 activează motorulA pe controlerul motorului.
int in1 = 9; //pinii D8 și D9 trimit un nivel High sau Low la controlerul motorului.
int in2 = 8;
int Up_Led = 5; // pinul D5 Led rosu - usa deschisa
int Down_Led = 6; // pinul D6 Led verde - usa inchisa
int lightSensor = analogRead(A0); //Se initializeaza „lightSensor” ca valoare la pinul A0 și citeste valoarea. Acesta este fotorezistorul.
int lightVal = 0; //LightVal va menține valoarea senzorului de lumină în această variabilă
int switch1 = 2; // comutatorul 1 este comutatorul inferior al ușii.
int switch2 = 4; //comutatorul 2 este comutatorul superior al ușii.
//Acestea sunt variabilele care dețin starea comutatoarelor de pozitie
int switchState1 = 0;
int switchState2 = 0;
void setup()
{
pinMode(enA, OUTPUT); // setați pinii de control al motorului ca ieșiri. Aceasta înseamnă că pinii 8, 9, 10 sunt ieșiri către controlerul de motor L298N.
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(Up_Led, OUTPUT); // setati pinii LED ca iesiri
pinMode(Down_Led, OUTPUT);
switchState1 = digitalRead(switch1); //read the state of switch 1 (the bottom one) and place it in switchState1
switchState2 = digitalRead(switch2); //read the state of switch 2 (the top one) and place it in switchState2
//acest lucru este important pentru a vă asigura că ușa pornește atunci când programul câștigă putere.
if (switchState2 != HIGH) //dacă switchState2 nu este High, atunci mergeți la instrucțiunea while
{
while (switchState2 != HIGH) // această funcție va opri motorul atâta timp cât întrerupătorul 1 nu a fost declanșat
{
analogWrite(enA, 255); // porneste motorul, setat viteza la 255
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(Up_Led, HIGH);
digitalWrite(Down_Led, LOW);
switchState1 = digitalRead(switch1); //se citeste din nou starea comutatorului pentru a vă asigura că nu a fost declanșat
switchState2 = digitalRead(switch2); //se citeste starea comutatorului 2 (cel de sus) și se plaseaza în switchState2
}
digitalWrite(in1, LOW); // odată ce switchstate2 a fost declanșat, se opreste motorul
digitalWrite(in2, LOW);
digitalWrite(Up_Led, HIGH);
digitalWrite(Down_Led, LOW);
}
}
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(switch1);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(switch2);
//the lightSensor is read and placed into lightVal. if its less than 50 and switchState2 is
//equal to high then go to the motor down code. But if the light is greater than 50 and the switchstate1
//is equal to high then call motor up code
if (switchState2 = HIGH && lightVal < 50)
{
delay(1500);
motordown();
}
else if (switchState1 = HIGH && lightVal > 50)
{
delay(1500);
motorup();
}
}
void motordown()
{
//Read the state of switch 1 (the Bottom one) and place it in switchState1
switchState1 = digitalRead(switch1);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(switch2);
//If switchState2 is high and the light is dark then continue
if (switchState2 = HIGH && lightVal < 50)
//wait 1 seconds
delay(1500);
//read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(switch1);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(switch2);
while (switchState1 != HIGH) {
// turn on motor and set speed to 255
// analogWrite(enA, 255);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(Up_Led, LOW);
digitalWrite(Down_Led, HIGH);
//read the state of switch 2 (the top one) and place it in switchState2
switchState1 = digitalRead(switch1);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(switch2);
}
//wait 1 second before turning off the motor to let the locks engage at the bottom
delay(1500);
// now turn off motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(Up_Led, LOW);
digitalWrite(Down_Led, HIGH);
}
void motorup()
{
//read the state of switch 1 (the bottom one) and place it in switchState2
switchState1 = digitalRead(switch1);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(switch2);
//if switchState1 is high and the light is bright then continue
if (switchState1 = HIGH && lightVal > 50)
{
//read the state of switch 1 (the bottom one) and place it in switchState1
switchState1 = digitalRead(switch1);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(switch2);
//delay 1 seconds
delay(1500);
//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);
digitalWrite(Up_Led, HIGH);
digitalWrite(Down_Led, LOW);
//read the state of switch 1 (the bottom one) and place it in switchState2
switchState1 = digitalRead(switch1);
//read the state of switch 2 (the top one) and place it in switchState2
switchState2 = digitalRead(switch2);
}
// now turn off motor
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(Up_Led, HIGH);
digitalWrite(Down_Led, LOW);
}
}