www.elektronik.si Seznam forumov www.elektronik.si
Forum o elektrotehniki in računalništvu
 
 PomočPomoč  IščiIšči  Seznam članovSeznam članov  SkupineSkupine  StatisticsStatistika  AlbumAlbum  DatotekeFilemanager DokumentacijaDocDB LinksPovezave   Registriraj seRegistriraj se 
  PravilaPravila  LinksBolha  PriponkePriponke  KoledarKoledar  ZapiskiZapiski Tvoj profilTvoj profil Prijava za pregled zasebnih sporočilPrijava za pregled zasebnih sporočil PrijavaPrijava 

Avtomatska vrata za kokošnjak

 
Objavi novo temo   Odgovori na to temo   Printer-friendly version    www.elektronik.si Seznam forumov -> Arduino sekcija
Poglej prejšnjo temo :: Poglej naslednjo temo  
Avtor Sporočilo
nejckovka
Član
Član



Pridružen-a: Čet 23 Avg 2007 16:31
Prispevkov: 134
Aktiv.: 0.66

PrispevekObjavljeno: Čet Okt 24, 2019 12:55 am    Naslov sporočila: icon_question Avtomatska vrata za kokošnjak Odgovori s citatom

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);
}
}
Nazaj na vrh
Skrit Poglej uporabnikov profil Pošlji zasebno sporočilo
Peter123
Član
Član



Pridružen-a: Tor 13 Jan 2009 15:34
Prispevkov: 1366
Aktiv.: 7.35
Kraj: Lj.

PrispevekObjavljeno: Čet Okt 24, 2019 7:44 am    Naslov sporočila:   Odgovori s citatom

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???
Nazaj na vrh
Odsoten Poglej uporabnikov profil Pošlji zasebno sporočilo
bobek220
Član
Član



Pridružen-a: Tor 28 Jul 2009 9:21
Prispevkov: 196
Aktiv.: 1.09
Kraj: Maribor

PrispevekObjavljeno: Čet Okt 24, 2019 8:29 am    Naslov sporočila:   Odgovori s citatom

Za varnost bi lahko dodal še števec. Preveriš če so vse not in potem zapreš, da ne ostane katera zunaj.
_________________
Kak je lepo ko je fajn
Nazaj na vrh
Odsoten Poglej uporabnikov profil Pošlji zasebno sporočilo
nejckovka
Član
Član



Pridružen-a: Čet 23 Avg 2007 16:31
Prispevkov: 134
Aktiv.: 0.66

PrispevekObjavljeno: Čet Okt 24, 2019 12:56 pm    Naslov sporočila:   Odgovori s citatom

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.
Nazaj na vrh
Skrit Poglej uporabnikov profil Pošlji zasebno sporočilo
twom
Član
Član



Pridružen-a: Ned 26 Okt 2003 0:37
Prispevkov: 868
Aktiv.: 3.90
Kraj: Ljubljana

PrispevekObjavljeno: Pet Okt 25, 2019 12:26 am    Naslov sporočila:   Odgovori s citatom

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.
Nazaj na vrh
Odsoten Poglej uporabnikov profil Pošlji zasebno sporočilo
Pokaži sporočila:   
Objavi novo temo   Odgovori na to temo   Printer-friendly version    www.elektronik.si Seznam forumov -> Arduino sekcija Časovni pas GMT + 2 uri, srednjeevropski - poletni čas
Stran 1 od 1

 
Pojdi na:  
Ne, ne moreš dodajati novih tem v tem forumu
Ne, ne moreš odgovarjati na teme v tem forumu
Ne, ne moreš urejati svojih prispevkov v tem forumu
Ne, ne moreš brisati svojih prispevkov v tem forumu
Ne ne moreš glasovati v anketi v tem forumu
Ne, ne moreš pripeti datotek v tem forumu
Ne, ne moreš povleči datotek v tem forumu

Uptime: 68 dni


Powered by phpBB © 2001, 2005 phpBB Group