Avtomatska vrata za kokošnjak

Arduino je pojav za katerega bi težko rekli, da opisuje samo HW ali samo SW, zato mu tu namenajm poseben prostor.

Moderator: tilz0R

Avtomatska vrata za kokošnjak

OdgovorNapisal/-a robne » 24 Okt 2019, 13:54

Pozdravljeni,
vprašanje sem zastavil tudi na sosednjem forumu, a več glav več ve, tako da zdaj sem tudi tu.
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: Izberi vse
//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);
}
}
robne
 
Prispevkov: 9
Pridružen: 24 Okt 2019, 13:36
Kraj: Gorenjska
Zahvalil se je: 5 krat
Prejel zahvalo: 1 krat
Uporabnika povabil: s54mtb
Število neizkoriščenih povabil: 0

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a xfce » 24 Okt 2019, 14:20

@robne

zanašati na svetlobo se je zelo, zelo težko. Sam to poznam precej dobro. Imaš nešteto razmer, ki ti pokvarijo meritev. Težko je določiti neke absolutne vrednosti za svetlobo, da veš kdaj se zdani in kdaj se znoči.

Recimo oblačen dan vs zelo svetla noč z luno. Potem je še umetna svetloba, umazan senzor itd.. nelinearnost senzorja.

Če bi šel na svetlobo, bi napisal algoritem, ki si zapomni zapomni recimo vrednosti svetlobe za 3 ure z intervalom na 1 minuto... in če recimo zadnjih 20 minut osvetljenost pade ali precej naraste bi lahko rekel da je šlo iz dan v noč ali noči v dan. Verjetno bi moral pa imeti neke fiksne vrednosti za noč..

Samo to je tako, lahko je do 10 h praktično tema in potem posveti sonce, ko gre megla stran :D


Verjetno je bolj optimalno vzeti arduino ploščico z RTC modulom in tam določiš uro odpiranja in zapiranja..
Uporabniški avatar
xfce
 
Prispevkov: 886
Pridružen: 14 Feb 2015, 11:38
Kraj: Poljane nad Škofjo loko
Zahvalil se je: 119 krat
Prejel zahvalo: 256 krat
Uporabnika povabil: S52O
Število neizkoriščenih povabil: 51

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a kobraN » 24 Okt 2019, 15:15

To isto z časovnim odpiranjem sem videl na enem filmčku na Yt.
Brane.
Uporabniški avatar
kobraN
 
Prispevkov: 1427
Pridružen: 26 Mar 2016, 19:39
Kraj: Murska S.
Zahvalil se je: 448 krat
Prejel zahvalo: 210 krat
Uporabnika povabil: mirkatmir
Število neizkoriščenih povabil: 106

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a kobraN » 24 Okt 2019, 15:43

Brane.
Uporabniški avatar
kobraN
 
Prispevkov: 1427
Pridružen: 26 Mar 2016, 19:39
Kraj: Murska S.
Zahvalil se je: 448 krat
Prejel zahvalo: 210 krat
Uporabnika povabil: mirkatmir
Število neizkoriščenih povabil: 106

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a VolkD » 24 Okt 2019, 18:22

Kaj pa kombinacija obojega ? Ura + svetloba. Saj gredo kokoši, če ni svetlobe prej spat.
So poznani primeri, ko so šli purani v hlev ob sončnem mrku.
Dokler bodo ljudje mislili, da živali ne čutijo bolečine, bodo živali čutile, da ljudje ne mislijowww.S5tech.net
Uporabniški avatar
VolkD
Administratorji strani
 
Prispevkov: 47121
Pridružen: 29 Dec 2014, 19:49
Kraj: Kačiče (Divača)
Zahvalil se je: 10761 krat
Prejel zahvalo: 6029 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 27

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a LiPo » 24 Okt 2019, 19:25

VolkD je napisal/-a:Kaj pa kombinacija obojega ? Ura + svetloba. Saj gredo kokoši, če ni svetlobe prej spat.
So poznani primeri, ko so šli purani v hlev ob sončnem mrku.



Najbolj optimalno je:
esp8266 (esp32) z priklopom na net in potem računanje časa soncnega vzhoda in zahoda.
Res, da se slisi divje, ampak je izvedljivo.....
Pa brez nekih nepredvidenih situacij... :_banghead :tappingfoot

LpP
Uporabniški avatar
LiPo
 
Prispevkov: 1037
Pridružen: 04 Apr 2015, 16:30
Kraj: LJUBLJANA
Zahvalil se je: 25 krat
Prejel zahvalo: 145 krat
Uporabnika povabil: cimabella
Število neizkoriščenih povabil: 29

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a forest70 » 24 Okt 2019, 20:32

LiPo je napisal/-a:
VolkD je napisal/-a:Kaj pa kombinacija obojega ? Ura + svetloba. Saj gredo kokoši, če ni svetlobe prej spat.
So poznani primeri, ko so šli purani v hlev ob sončnem mrku.



Najbolj optimalno je:
esp8266 (esp32) z priklopom na net in potem računanje časa soncnega vzhoda in zahoda.
Res, da se slisi divje, ampak je izvedljivo.....
Pa brez nekih nepredvidenih situacij... :_banghead :tappingfoot

LpP

Se popolnoma strinjam s tabo, vendar, če ima težavo naredit v programu neko histerezo, zamisli si kako težavo bo imel za postavit v Arduino okolju ESP32 okolje.
If you pay in bananas, you got monkeys.
Uporabniški avatar
forest70
 
Prispevkov: 2396
Pridružen: 14 Jan 2015, 00:11
Kraj: Koper
Zahvalil se je: 455 krat
Prejel zahvalo: 538 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 125

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a LiPo » 24 Okt 2019, 20:38

forest70 je napisal/-a:
LiPo je napisal/-a:
VolkD je napisal/-a:Kaj pa kombinacija obojega ? Ura + svetloba. Saj gredo kokoši, če ni svetlobe prej spat.
So poznani primeri, ko so šli purani v hlev ob sončnem mrku.



Najbolj optimalno je:
esp8266 (esp32) z priklopom na net in potem računanje časa soncnega vzhoda in zahoda.
Res, da se slisi divje, ampak je izvedljivo.....
Pa brez nekih nepredvidenih situacij... :_banghead :tappingfoot

LpP

Se popolnoma strinjam s tabo, vendar, če ima težavo naredit v programu neko histerezo, zamisli si kako težavo bo imel za postavit v Arduino okolju ESP32 okolje.


Tukaj pa samo Nejc Zaplotnik svoje pove.... Važna je pot.....
Uporabniški avatar
LiPo
 
Prispevkov: 1037
Pridružen: 04 Apr 2015, 16:30
Kraj: LJUBLJANA
Zahvalil se je: 25 krat
Prejel zahvalo: 145 krat
Uporabnika povabil: cimabella
Število neizkoriščenih povabil: 29

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a forest70 » 24 Okt 2019, 21:59

Koda: Izberi vse
//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;
static uint32_t timertick=0; //Definiramo spremnljivko
bool isDay = false;
bool time2open = false ;
//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 lightValTmp = 0, 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()
{
    timertick=millis();
  // 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()
{
  if (timertick + 900000l < millis()) // 15 minut
  {
  //resetira timertick na tenutno vrednost
     timertick=millis();
    if (isDay)
     time2open = true ;
     else
     time2open = false;
 
 }
  //read the light sensor and place it in lightval
  lightValTmp = lightVal;
  lightVal = analogRead(lightSensor);
  if ( lightVal > 1000)
    isDay = true;
  else
    isDay = false;

   if ( isDay  != ( lightValTmp > 1000); // ob vsaki spremembi stanja - (noč - dan), resetira timertick
    timertick=millis();
   
 

 
 
//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) & time2open )
{
 delay(2000);
    motordown();
}
 else if (switchState1 == HIGH & !time2open)
{
  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) & time2open)
 //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 & !time2open)
 {

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

Dodal sem časovno zanko 15 minut, če po 15. minutah še vedno isto stanje, npr dan pol sproži odpiranje vrat. Če pa želiš skrajšat čas, pa namesto 900000l mili sekund, napisi kaj manj. Obvezno na koncu številke dodaj črko "l" kar pomeni long.
Upam, da se nisem kje zmotil in da dela.
Če ti kaj ni jasno ali imaš težave, pa vprašaj.
If you pay in bananas, you got monkeys.
Uporabniški avatar
forest70
 
Prispevkov: 2396
Pridružen: 14 Jan 2015, 00:11
Kraj: Koper
Zahvalil se je: 455 krat
Prejel zahvalo: 538 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 125

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a Iztok » 24 Okt 2019, 22:53

Ker sem že delal podobno stvar imam samo en nasvet za projektanta ... kure nesnice najbolje nesejo, če je dan/svetloba 16 ur dnevno - tudi pozimi ... ;)
ʇɥıs ıs ʇɥǝ ןɐsʇ ʇıɯǝ ı qnʎ ɐ ʞǝʎqoɐɹp ou ǝqɐʎ
Uporabniški avatar
Iztok
 
Prispevkov: 2587
Pridružen: 04 Feb 2015, 01:00
Kraj: LJ
Zahvalil se je: 728 krat
Prejel zahvalo: 587 krat
Uporabnika povabil: Proteus
Število neizkoriščenih povabil: 116

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a robne » 24 Okt 2019, 22:57

Pozdrav,
najprej hvala vsem za odgovore!
Z RTC, kaj šele priklopom na internet se ne bi hecal zaenkrat. :D
Vezje imam že sestavljeno, za cel projekt mislim uporabit stvari ki jih imam doma. Vse deluje ok, nagaja mi le ta prej omenjena histereza. To je že na pol uspelo rešiti uporabniku forest70!
forest70, najlepša hvala za pomoč. Sedaj deluje precej bolj stabilno kot prej, a še vedno noče čakati 15min. Prej nekje med 15-20sek, neglede koliko povečujem število 900000l .
Ne vem ali sem še kaj spregledal?

Lp
robne
 
Prispevkov: 9
Pridružen: 24 Okt 2019, 13:36
Kraj: Gorenjska
Zahvalil se je: 5 krat
Prejel zahvalo: 1 krat
Uporabnika povabil: s54mtb
Število neizkoriščenih povabil: 0

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a forest70 » 24 Okt 2019, 23:06

imaš napako v programu:
else if (switchState1 = HIGH && lightVal < 1000)
Če analiziramo zadevo:
switchState1 = HIGH dodeli switchState1 vrednost HIGH kar je vsakem primeru HIGH !!!!!
Pravilno je else if (switchState1 == HIGH && lightVal < 1000)
Kodo, ki sem jo pripel, sem sedaj popravil.
If you pay in bananas, you got monkeys.
Uporabniški avatar
forest70
 
Prispevkov: 2396
Pridružen: 14 Jan 2015, 00:11
Kraj: Koper
Zahvalil se je: 455 krat
Prejel zahvalo: 538 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 125

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a forest70 » 24 Okt 2019, 23:14

To še popravi:
iz
if (timertick + 900000l > millis()) // 15 minut
v
if (timertick + 900000l < millis()) // 15 minut
Typo :D
Zgornja moja koda je že poravljena.
If you pay in bananas, you got monkeys.
Uporabniški avatar
forest70
 
Prispevkov: 2396
Pridružen: 14 Jan 2015, 00:11
Kraj: Koper
Zahvalil se je: 455 krat
Prejel zahvalo: 538 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 125

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a forest70 » 24 Okt 2019, 23:37

Pa še to dodaj v loop zanko:
Koda: Izberi vse

if (millis() > 0xFFF24000l)
  {
      resetFunc();
  }

To pa čisto na konec skeča.
Koda: Izberi vse
void(* resetFunc) (void) = 0; //declare reset function @ address 0

To je potrebno, ker po 52 dneh, pride do owerflow-a. Računanje z variabilo ko je blizu vrednosti 0xFF FF FF FF + 900000, da neprvidljiv rezultat.
Zato, ko je vrednost ki ga vrne millis() blizu maximum - 900000 enostavno resetiramo arduino, da začne millis() štet z nula, prej ko doseže svoj maximum.
If you pay in bananas, you got monkeys.
Uporabniški avatar
forest70
 
Prispevkov: 2396
Pridružen: 14 Jan 2015, 00:11
Kraj: Koper
Zahvalil se je: 455 krat
Prejel zahvalo: 538 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 125

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a forest70 » 25 Okt 2019, 08:45

Softwerski reset ima eno pomankljivost in sicer, res program štarta ponovno, ampak vsi registri ostanejo taki kot so nastavljeni ( se ne resetirajo na default)
Koda: Izberi vse
void(* resetFunc) (void) = 0; //declare reset function @ address 0


Boljše je, ker izvede hardwerski reset
Koda: Izberi vse
void resetFunc()
{
//izbereš prost pin in ga povežeš z reset pinom
pinMode(prostpin, OUTPUT);
digitalWrite(prostpin, LOW);
}
If you pay in bananas, you got monkeys.
Uporabniški avatar
forest70
 
Prispevkov: 2396
Pridružen: 14 Jan 2015, 00:11
Kraj: Koper
Zahvalil se je: 455 krat
Prejel zahvalo: 538 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 125

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a robne » 25 Okt 2019, 12:07

Pozdrav,
forest70 ponovno hvala za trud, sploh pa za potrpljenje...
Ne uspe mi pravilno vnesti tvojih predlogov, karkoli preizkušam dobim nekaj error-jev. Še najmanj sem jih dobil ob spodnji priloženi različici. Prilagam tudi error message.
Kje ga lomim?



Koda: Izberi vse
Arduino: 1.6.8 (Windows 7), Board: "Arduino Nano, ATmega328"

Build options changed, rebuilding all
proba_kurnk_3:190: error: 'void (* resetFunc)()' redeclared as different kind of symbol

       void(* resetFunc) (void) = 0; //declare reset function @ address 0

                              ^

proba_kurnk_3:184: error: previous declaration of 'void resetFunc()'

             void resetFunc()

                  ^

exit status 1
'void (* resetFunc)()' redeclared as different kind of symbol







Koda: Izberi vse
    //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;
    static uint32_t timertick=0; //Definiramo spremnljivko
    bool isDay = false;
    bool time2open = false ;
    //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 lightValTmp = 0, 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()
    {
        timertick=millis();
      // 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()
    {
      if (timertick + 900000l < millis()) // 15 minut
      {
      //resetira timertick na tenutno vrednost
         timertick=millis();
        if (isDay)
         time2open = true ;
         else
         time2open = false;
     
     }
      //read the light sensor and place it in lightval
      lightValTmp = lightVal;
      lightVal = analogRead(lightSensor);
      if ( lightVal > 1000)
        isDay = true;
      else
        isDay = false;

       if ( isDay  != ( lightValTmp > 1000)); // ob vsaki spremembi stanja - (noč - dan), resetira timertick
        timertick=millis();
       
     

     
     
    //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) & time2open )
    {
     delay(2000);
        motordown();
    }
     else if (switchState1 == HIGH & !time2open)
    {
      delay(2000);
       motorup();
    }
   
   
    if (millis() > 0xFFF24000l)
  {
      resetFunc();
  }
  }

    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) & time2open)
     //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 & !time2open)
     {

        //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);
    }
    }
            void resetFunc()
    {
    //izbereš prost pin in ga povežeš z reset pinom
    pinMode(10, OUTPUT);
    digitalWrite(10, LOW);
    }
      void(* resetFunc) (void) = 0; //declare reset function @ address 0
robne
 
Prispevkov: 9
Pridružen: 24 Okt 2019, 13:36
Kraj: Gorenjska
Zahvalil se je: 5 krat
Prejel zahvalo: 1 krat
Uporabnika povabil: s54mtb
Število neizkoriščenih povabil: 0

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a forest70 » 25 Okt 2019, 17:03

Tako, sedaj pa bi moralo delat, vsaj meni preved brez napak.
Če si naredil mostiček iz pina x na reset, pa odkomentiraj drugo funkcijo, prvo pa zakomentiraj.
Koda: Izberi vse
//ali ena ali druga reset funkcija, obe ne moreta bit hkrati
/*
    void resetFunc()
    {
    //izbereš prost pin in ga povežeš z reset pinom
    pinMode(10, OUTPUT);
    digitalWrite(10, LOW);
    }
*/
//druga reset funkcija
    void(* resetFunc) (void) = 0; //declare reset function @ address 0



//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;
    static uint32_t timertick=0; //Definiramo spremnljivko
    bool isDay = false;
    bool time2open = false ;
    //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 lightValTmp = 0, 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()
    {
        timertick=millis();
      // 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()
    {
      if (timertick + 900000l < millis()) // 15 minut
      {
      //resetira timertick na tenutno vrednost
         timertick=millis();
        if (isDay)
         time2open = true ;
         else
         time2open = false;
     
     }
      //read the light sensor and place it in lightval
      lightValTmp = lightVal;
      lightVal = analogRead(lightSensor);
      if ( lightVal > 1000)
        isDay = true;
      else
        isDay = false;

       if ( isDay  != ( lightValTmp > 1000)); // ob vsaki spremembi stanja - (noč - dan), resetira timertick
        timertick=millis();
       
     

     
     
    //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) & time2open )
    {
     delay(2000);
        motordown();
    }
     else if (switchState1 == HIGH & !time2open)
    {
      delay(2000);
       motorup();
    }
   
   
    if (millis() > 0xFFF24000l)
  {
      resetFunc();
  }
  }

    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) & time2open)
     //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 & !time2open)
     {

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

If you pay in bananas, you got monkeys.
Uporabniški avatar
forest70
 
Prispevkov: 2396
Pridružen: 14 Jan 2015, 00:11
Kraj: Koper
Zahvalil se je: 455 krat
Prejel zahvalo: 538 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 125

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a robne » 25 Okt 2019, 20:41

Zdaj sem poskusil to zadnjo kodo, dodal vez med RST in 10, a ne deluje. Vezje ne reagira sploh več na spremembe svetlobe, edino kar sem spravil iz njega je da je po nekajkratnem pritiskanju zgornjega in spodnjega končnega stikala naenkrat začel vozit gor, ko je prišel do konca se je ustavil, a čez nekaj sekund šel nazaj dol in tako cikliral v nedogled. Spreminjati sem šel 900000 na manjše število (za testiranje), a nobene spremembe... ResetFunc sem vstavil tako;

Koda: Izberi vse

    //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;
        static uint32_t timertick=0; //Definiramo spremnljivko
        bool isDay = false;
        bool time2open = false ;
        //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 lightValTmp = 0, 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()
        {
            timertick=millis();
          // 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()
        {
          if (timertick + 900000l < millis()) // 15 minut
          {
          //resetira timertick na tenutno vrednost
             timertick=millis();
            if (isDay)
             time2open = true ;
             else
             time2open = false;
         
         }
          //read the light sensor and place it in lightval
          lightValTmp = lightVal;
          lightVal = analogRead(lightSensor);
          if ( lightVal > 1000)
            isDay = true;
          else
            isDay = false;

           if ( isDay  != ( lightValTmp > 1000)); // ob vsaki spremembi stanja - (noč - dan), resetira timertick
            timertick=millis();
           
         

         
         
        //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) & time2open )
        {
         delay(2000);
            motordown();
        }
         else if (switchState1 == HIGH & !time2open)
        {
          delay(2000);
           motorup();
        }
       
       
        if (millis() > 0xFFF24000l)
       
      {
          resetFunc();
      }
      }

        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) & time2open)
         //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 & !time2open)
         {

            //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);
        }
        }
           
   
        void resetFunc()
        {
        //izbereš prost pin in ga povežeš z reset pinom
        pinMode(10, OUTPUT);
        digitalWrite(10, LOW);
        }
   



:_helpsos
robne
 
Prispevkov: 9
Pridružen: 24 Okt 2019, 13:36
Kraj: Gorenjska
Zahvalil se je: 5 krat
Prejel zahvalo: 1 krat
Uporabnika povabil: s54mtb
Število neizkoriščenih povabil: 0

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a forest70 » 25 Okt 2019, 21:03

za začetek spremeni 900000l v 60000l kar je ena minuta. V tem času se stanje ne sme spremenit glede svetlobe. Torej eno minuto naj bo osvetljen, pa bi boral delat. Če se vmes spremeni stanje svetlobe, začne znova štet do ene minute.
PS
Zakaj imaš nastavljeno, da ti šele pri 1000 zazna da je "dan" ? Namreč ADC pri ATMEGA je 10 biten, kar pomeni, da lahko vrene vrednost med 0 in 1023 in ti si na sami zgornji meji.
If you pay in bananas, you got monkeys.
Uporabniški avatar
forest70
 
Prispevkov: 2396
Pridružen: 14 Jan 2015, 00:11
Kraj: Koper
Zahvalil se je: 455 krat
Prejel zahvalo: 538 krat
Uporabnika povabil: Vrtni palček
Število neizkoriščenih povabil: 125

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a robne » 26 Okt 2019, 13:58

Sem spremenil na minuto, a nobene spremembe. Ne reagira na nič. Na 1000 imam nastavljeno ker sem dobil take vrednosti iz fotoupora. Ubistvu je do 1020 svetlo, nad tem je pa noč. Vem da je v programu obrnjeno, a sem mislil pač obrniti polariteto motorja.
robne
 
Prispevkov: 9
Pridružen: 24 Okt 2019, 13:36
Kraj: Gorenjska
Zahvalil se je: 5 krat
Prejel zahvalo: 1 krat
Uporabnika povabil: s54mtb
Število neizkoriščenih povabil: 0

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a xfce » 26 Okt 2019, 21:40

@forest70 to resetiranje je češko :)


@robne malo sem predelal program, samo nujne stvari za enkrat.

začetno zaprianje kurjih vrat sem vrgel v funkcijo motorInit() in sem jo zakomentiral, ker nimam vrat.

1. Namesti knjižnico za shranjevanje vrednosti FIFO, LIFO: CircularBuffer
2. vklopi konzolo in nastavi na 9600, tako, da boš lahko v konzoli spremljal kaj počne program in boš videl, kje gre narobe.
3. Senzorju daj take upore, da bo recimo svetlo, ko bo vrednost ADC-ja manj kot 800 recimo, ker adc gre do 1023 samo.
4. Nastavi vrednosti za dan in noč, vmes daj nedefinirano območje, da imaš histerezo. Da ni tako, da ti na koncu skače vrednost za 1 in ti odpira in zapira.
if (avg < 800) {isDay = true;}
if (avg > 1000) {isDay = false;}


Sedaj program deluje tako, da ti vsako sekundo zmeri vrednost ADC- svetlost in potem ti shrani. Ko je shranjenih 60 vrednosti (minuta), ti izračuna povprečje. Potem preveri ali je dan ali ni:
if (avg < 800) {isDay = true;}
if (avg > 1000) {isDay = false;}

pa tukaj ne pozabi na histerezo. Glej tudi v konzoli kaj program izpisuje, pa boš videl, če je ok. Potem ko preštudiraš povej in gremo naprej :) Ni potrebno nič resetirati.

Koda: Izberi vse
#include <CircularBuffer.h>
//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;
bool isDay = false;
bool time2open = false ;


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


#define INTERVAL 1000ul // proženeje vsako sekundo
uint8_t oneSecondTick() {
   // Get snapshot of time
   static uint32_t previousMillis=0;
   uint32_t currentMillis = millis();
 
 
   // How much time has passed, accounting for rollover with subtraction!
   if ((uint32_t)(currentMillis - previousMillis) >= INTERVAL) {
      // Use the snapshot to set track time until next event
      previousMillis = currentMillis;
      return true ;
   }
   return false;
}

void motorInit(){
if (switchState2 != HIGH)
{
  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 only runs once.
void setup()
{
  Serial.begin(9600);
  Serial.println("Kokoduri v1.0");
  Serial.println("Program je bil preveden na dan:");
  Serial.print (F(__DATE__)) ;
  Serial.print (" ") ;
  Serial.println (F(__TIME__)) ;
  Serial.println("\n");

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

  // zaprem vrata, če so odprta
  Serial.println("Zapiranje vrat");
  // motorInit();
  Serial.println("Vrata so OK, grem v glavno zanko");


Serial.println("program je incializiran in grem v glavno zanko");
}

//this runs over and over
uint16_t lightVal = analogRead(A0);


CircularBuffer<uint16_t,60> lightValues;       // uses 638 bytes

void loop()
{
  if (oneSecondTick()){
    lightVal = analogRead(A0); // izmerim svetlost
    Serial.print("svetlost:");
    Serial.println(lightVal);

    if (!lightValues.isFull()){
      lightValues.push(lightVal);
      }else{
        Serial.println("buffer je poln, racunam povprecje:");
        uint16_t sum = 0;
        uint8_t buffSize = lightValues.size();
        while(!lightValues.isEmpty()){
          sum += lightValues.pop();
          }
          uint16_t avg = (uint16_t)(sum/buffSize);
          Serial.print("POVPREČJE 60 vrednosti:");
          Serial.println(avg);

          if (avg < 800) {isDay = true;}
          if (avg > 1000) {isDay = false;}

          if (isDay) {
            Serial.println("Zunaj je dan!");
            time2open = true;
            } else {
            Serial.println("Zunaj je noč!");
            time2open = false;
            }
        }
    }
 
 
//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) & time2open )
{
 delay(2000);
    motordown();
}
 else if (switchState1 == HIGH & !time2open)
{
  delay(2000);
   motorup();
}
       
} // loop

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) & time2open)
 //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 & !time2open)
 {

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

Uporabniški avatar
xfce
 
Prispevkov: 886
Pridružen: 14 Feb 2015, 11:38
Kraj: Poljane nad Škofjo loko
Zahvalil se je: 119 krat
Prejel zahvalo: 256 krat
Uporabnika povabil: S52O
Število neizkoriščenih povabil: 51

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a skat » 20 Nov 2019, 14:33

1K UPOR NA +5V foto upor vežeš proti masi sredino pa meriš zadeva je zelo zvezna glede na osvetlitev seveda je logiika v tem primeru obrnjena ko je najbolj temno je vrednost največja ko je svetlo pa najmanjša.
to uporabljam pri sončnem kolektorju. Sem namreč CharlyG ker se ne morem nikakor prijaviti v forum mi je Skat to omogočil.Drugače pa je ta vezava tudi glede motenj zelo odporna in z tako vezavo merim štiri foto upore
(vogali) na osnovi tega vrtim kolektor in zadeeva dela.
ne sekiraj se, če kaj crkne, vsaka šola nekaj stane!
skat
 
Prispevkov: 271
Pridružen: 30 Jan 2015, 07:50
Kraj: Ribnica
Zahvalil se je: 51 krat
Prejel zahvalo: 14 krat
Uporabnika povabil: s56rga
Število neizkoriščenih povabil: 6

Re: Avtomatska vrata za kokošnjak

OdgovorNapisal/-a robne » 17 Dec 2020, 16:46

Vsem skupaj bi se (po precej dolgem) času rad najlepše zahvalil za predloge in pomoč! Do realizacije projekta na koncu žal ni prišlo...

Lp!
robne
 
Prispevkov: 9
Pridružen: 24 Okt 2019, 13:36
Kraj: Gorenjska
Zahvalil se je: 5 krat
Prejel zahvalo: 1 krat
Uporabnika povabil: s54mtb
Število neizkoriščenih povabil: 0


Vrni se na Arduino

Kdo je na strani

Po forumu brska: 0 registriranih uporabnikov in 1 gost