www.elektronik.si
Avtomatska vrata za kokošnjak

www.elektronik.si -> Arduino sekcija

Avtor: nejckovka PrispevekObjavljeno: Čet Okt 24, 2019 12:55 am    Naslov sporočila: icon_question Avtomatska vrata za kokošnjak
----------------------------------------------------------------------------
Pozdravljeni,
izdelujem avtomatsko odpiranje in zapiranje kokošnjaka glede na svetlobo z Arduino Nanom in fotouporom. Vezja samega mi ni bilo problem sestaviti, a pri programiranju sem naletel na meni prezahteven problem, kateri se mi dozdeva da bo za vas mačji kašelj. Prilagam program, katerega sem našel na tej strani .

Zanima me kako bi lahko v obstoječo kodo dodal funkcijo, da se ne bi vrata odpirala in zapirala ob vsaki nenadni spremembi svetlosti (ponoči npr. mimovozeči avtomobil, zvečer sence...), ampak bi nekako izračunaval povprečje nekaj minut, in nato če bi bilo dovolj časa dovolj temno/svetlo sprožil zapiranje/odpiranje. Torej, da bi reagiral kakih 10-15min po svitanju, in isto ko bi padel mrak šele po tem času.

Že nekaj dni berem vodiče in se igram s tem, a programiranje mi res ne leži, tako da bi bil izredno hvaležen če bi bil kdo pripravljen pomagati.

Hvala vnaprej,
Nejc



Koda:

//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, 7, 9.
//Pins 7 and 8 send high or low to the motor controller.
//pin 10 enables motor one on the motor controller.
int enA = 9;
int in1 = 7;
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 5
int reed1Pin = 5;
//reed2Pin is the top switch on the door. This is digital pin 6
int reed2Pin = 6;
//These are the variables that hold the state of the reed switches
int switchState1 = 0;
int switchState2 = 0;

//This only runs once.
void setup()
{
  // set the motor control pins as outputs. This means pins 7, 8, 9 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 greater than 200 and switchState2 is
  //equal to high then go to the motor down code. But if the light is less than 200 and the switchstate1
  //is equal to high then call motor up code
 if (switchState2 = HIGH && lightVal > 1000)
{
 delay(2000);
    motordown();
}
 else if (switchState1 = HIGH && lightVal < 1000)
{
  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 > 1000)
 //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(2000);
  // 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 < 1000)
 {

    //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);
}
}

Avtor: Peter123Kraj: Lj. PrispevekObjavljeno: Čet Okt 24, 2019 7:44 am    Naslov sporočila:  
----------------------------------------------------------------------------
Za hitre spremembe svetlobe (naprimer avto) lahko napišeš nekaj podobnega:
če je že svetlo, potem počakaj npr. 55s,
pomeri še enkrat, lahko pomeriš večkrat zaporedoma, mogoče bi lahko uporabil tudi povprečenje meritev v času 55s.
če je še bolj svetlo potem odpri ,
če ni še bolj svetlo potem ne odpri.
Povprečenje je tudi eden od načinov, kateri je pa boljši???

Avtor: bobek220Kraj: Maribor PrispevekObjavljeno: Čet Okt 24, 2019 8:29 am    Naslov sporočila:  
----------------------------------------------------------------------------
Za varnost bi lahko dodal še števec. Preveriš če so vse not in potem zapreš, da ne ostane katera zunaj.

Avtor: nejckovka PrispevekObjavljeno: Čet Okt 24, 2019 12:56 pm    Naslov sporočila:  
----------------------------------------------------------------------------
Saj nekaj takega bi rad naredil, a se mi ne sanja niti približno kako bi to spesnil skupaj v program... Jasno mi je kje najdem spremenljivko iz fotoupora (int lightVal = 0; ), a kako naprej mi je pa španska vas.

Avtor: twomKraj: Ljubljana PrispevekObjavljeno: Pet Okt 25, 2019 12:26 am    Naslov sporočila:  
----------------------------------------------------------------------------
Jaz bi se orientiral na uro, v povezavi z datumom (letnim časom, oziroma dolžino dneva).
Ker če bo nevihta s temnimi oblaki, se bojo vrata zaprla tudi sredi dneva.

Stran 1 od 1

Powered by phpBB © 2001,2002 phpBB Group