Arduino kezdő

Processing/Wiring (illetve C) nyelvű programozási fogások, tippek. (AVR-Duino, Arduino, EthDuino, Diecimila, Severino, Nano, LilyPad)
Vkrisz
Újonc
Újonc
Hozzászólások: 1
Csatlakozott: 2017. július 1. szombat, 14:03

Arduino kezdő

Hozzászólás Szerző: Vkrisz »

Sziasztok!

Segítséget kérnék egy olyan megoldásában, hogy adott egy nyomógomb, magas jelszint esetén kigyullad egy led(1-es), egy másik(2-es) pedig nem. Alacsony jel mellett az 1-es led elalszik, eddig oké a dolog, a 2 lednek fel kéne gyulladni pár másodperce majd elaludni. Ezzel lenne gondom, hogy ha egy else ágba teszem egy delay-el, mivel állandóan alacsony a bemenetem kapok egy villogó ledet...
Előre is köszi!
vargham
Chipgyilok
Hozzászólások: 308
Csatlakozott: 2014. január 8. szerda, 8:32

Re: Arduino kezdő

Hozzászólás Szerző: vargham »

> ha egy else ágba teszem egy delay-el
Delay tilos, mert VÁRAKOZTATJA a processzort, miközben más eseményekre is kellene reagálni. Használj helyette időzítést, és akkor működni fog.
Avatar
kapu48
Elektronbűvölő
Hozzászólások: 3375
Csatlakozott: 2008. augusztus 29. péntek, 6:00

Re: Arduino kezdő

Hozzászólás Szerző: kapu48 »

Biztos erre gondolt a kolega: Blink Without Delay.
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
vargham
Chipgyilok
Hozzászólások: 308
Csatlakozott: 2014. január 8. szerda, 8:32

Re: Arduino kezdő

Hozzászólás Szerző: vargham »

Például erre.
Gyorob1
DrótVégénSzéndarab
Hozzászólások: 25
Csatlakozott: 2018. május 27. vasárnap, 20:26

Re: Arduino kezdő

Hozzászólás Szerző: Gyorob1 »

Üdv. Van egy kész programom ami 4 relét vezérel 4 nyomógombbal. Tökéletesen működik. Beleakarok szólni egy analóg bemenettel, de az előző kód azt mondja hogy LOW, az analóg bemenet pedig High. Sajnos ez üti egymást, és nem tudok tovább haladni. Valahogy belekene avatkozni a kódba.
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Re: Arduino kezdő

Hozzászólás Szerző: Robert »

Forráskód?
A nélkül elégé nehéz....


Analóg bemenet nem LOW és HIGH, hanem 0....1023 :)
Ha digit bemenetként konfigurálod, akkor viselkedik úgy, mint a Dx lábak.
http://www.tavir.hu - a gazda :)
Gyorob1
DrótVégénSzéndarab
Hozzászólások: 25
Csatlakozott: 2018. május 27. vasárnap, 20:26

Re: Arduino kezdő

Hozzászólás Szerző: Gyorob1 »

const int analogPin = A0;
const int feny = 500;
const int buttonPin = 6;
const int buttonPin2 = 3;
const int ledPin2 = 4;
const int ledPin = 5;
const int buttonPin3 = 8;
const int buttonPin4 = 9;
const int ledPin3 = 10;
const int ledPin4 = 11;
const int time_to_light = 30000;
int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
long buttonCheckInterval = 100;
long buttonDecideInterval = 500;
long previousMillis = 0;
long previousMillis2 =0;
long previousMillis3 = 0;
long previousMillis4 =0;
int button1_pressed = 0;
int button1_pressed_old = 0;
int button2_pressed = 0;
int button2_pressed_old = 0;

int button3_pressed = 0;
int button3_pressed_old = 0;
int button4_pressed = 0;
int button4_pressed_old = 0;

int FSM_State =0;
int FSM_State2 =0;

void setup() {

pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin2, INPUT);

pinMode(ledPin3, OUTPUT);
pinMode(buttonPin3, INPUT);
pinMode(ledPin4, OUTPUT);
pinMode(buttonPin4, INPUT);

}

void loop(){



// read the state of the pushbutton values:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);

if(FSM_State == 0)
{
//We are waiting for button presses, so we disable the LEDs and shutters.
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);


//If the interval has passed
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > buttonCheckInterval)
{
previousMillis = currentMillis;
//If we press the button for less than 200ms, button1_pressed is set to 1, otherwise, it's set to 2
if (buttonState == HIGH)
{
if(button1_pressed_old == 0) button1_pressed=1;
else button1_pressed=2;
}

//The same for the second button

if (buttonState2 == HIGH)
{
if(button2_pressed_old == 0) button2_pressed=1;
else button2_pressed=2;
}
}

//Check if it's time to decide wether we have a momentary press, a press-and-hold, or nothing
if(currentMillis - previousMillis2 > buttonCheckInterval*5)
{
previousMillis2 = currentMillis;

//if button1_pressed is 2, then we have pressed and held the button, and go to state 1
//. If 1, then we pressed it for less than 200ms (momentarily), and go to state 2.
if(button1_pressed == 2) FSM_State = 1; //digitalWrite(ledPin, HIGH);
else if(button1_pressed_old == 1) FSM_State = 2;
button1_pressed_old = button1_pressed;
button1_pressed = 0;

//Do the same for button2
if(button2_pressed == 2) FSM_State = 3; //digitalWrite(ledPin, HIGH);
else if(button2_pressed_old == 1) FSM_State = 4;
button2_pressed_old = button2_pressed;
button2_pressed = 0;
}
}
else if(FSM_State == 1)
{
//Check if we still hold the button every 100ms. If so, keep powering the LED and shutters.
//Otherwise, go to state 0 (idle)
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > buttonCheckInterval)
{
previousMillis = currentMillis;
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
}
else
{
digitalWrite(ledPin, LOW);
FSM_State = 0;
}
}
}
else if(FSM_State == 2)
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
unsigned long currentMillis = millis();

//In case the 2nd button is pressed, go to state 5
if(currentMillis - previousMillis > buttonCheckInterval)
{
previousMillis = currentMillis;
buttonState2 = digitalRead(buttonPin2);
if(buttonState2 == HIGH) FSM_State = 5;
}
//If we reached our count to 30, go to state 0
if(currentMillis - previousMillis2 > time_to_light)
{
previousMillis2 = currentMillis;
digitalWrite(ledPin, LOW);
FSM_State = 0;
}
}
//The same as state 1, for the second button
else if(FSM_State == 3)
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > buttonCheckInterval)
{
previousMillis = currentMillis;
buttonState2 = digitalRead(buttonPin2);
if(buttonState2 == HIGH)
{
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin, LOW);
}
else
{
digitalWrite(ledPin2, LOW);
FSM_State = 0;
}
}
}
//The same as state 2, for the second button
else if(FSM_State == 4)
{
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin, LOW);
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > buttonCheckInterval)
{
previousMillis = currentMillis;
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH) FSM_State = 5;
}

if(currentMillis - previousMillis2 > time_to_light)
{
previousMillis2 = currentMillis;
digitalWrite(ledPin2, LOW);
FSM_State = 0;
}
}
//This state exists so that we can have enough time to release the button if we want to
//momentarily press the button to go from 2 or 4 to 0.
else if(FSM_State == 5)
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > (buttonCheckInterval * 3))
{
previousMillis = currentMillis;
FSM_State = 0;
}
}




buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);

if(FSM_State2 == 0)
{
//We are waiting for button presses, so we disable the LEDs and shutters.
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);


//If the interval has passed
unsigned long currentMillis = millis();
if(currentMillis - previousMillis3 > buttonCheckInterval)
{
previousMillis3 = currentMillis;
//If we press the button for less than 200ms, button1_pressed is set to 1, otherwise, it's set to 2
if (buttonState3 == HIGH)
{
if(button3_pressed_old == 0) button3_pressed=1;
else button3_pressed=2;
}

//The same for the second button

if (buttonState4 == HIGH)
{
if(button4_pressed_old == 0) button4_pressed=1;
else button4_pressed=2;
}
}

//Check if it's time to decide wether we have a momentary press, a press-and-hold, or nothing
if(currentMillis - previousMillis4 > buttonCheckInterval*5)
{
previousMillis4 = currentMillis;

//if button1_pressed is 2, then we have pressed and held the button, and go to state 1
//. If 1, then we pressed it for less than 200ms (momentarily), and go to state 2.
if(button3_pressed == 2) FSM_State2 = 1; //digitalWrite(ledPin, HIGH);
else if(button3_pressed_old == 1) FSM_State2 = 2;
button3_pressed_old = button3_pressed;
button3_pressed = 0;

//Do the same for button2
if(button4_pressed == 2) FSM_State2 = 3; //digitalWrite(ledPin, HIGH);
else if(button4_pressed_old == 1) FSM_State2 = 4;
button4_pressed_old = button4_pressed;
button4_pressed = 0;
}
}
else if(FSM_State2 == 1)
{
//Check if we still hold the button every 100ms. If so, keep powering the LED and shutters.
//Otherwise, go to state 0 (idle)
unsigned long currentMillis = millis();
if(currentMillis - previousMillis3 > buttonCheckInterval)
{
previousMillis3 = currentMillis;
buttonState3 = digitalRead(buttonPin3);
if(buttonState3 == HIGH)
{
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, LOW);
}
else
{
digitalWrite(ledPin3, LOW);
FSM_State2 = 0;
}
}
}
else if(FSM_State2 == 2)
{
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, LOW);
unsigned long currentMillis = millis();

//In case the 2nd button is pressed, go to state 5
if(currentMillis - previousMillis3 > buttonCheckInterval)
{
previousMillis3 = currentMillis;
buttonState4 = digitalRead(buttonPin4);
if(buttonState4 == HIGH) FSM_State2 = 5;
}
//If we reached our count to 30, go to state 0
if(currentMillis - previousMillis4 > time_to_light)
{
previousMillis4 = currentMillis;
digitalWrite(ledPin3, LOW);
FSM_State2 = 0;
}
}
//The same as state 1, for the second button
else if(FSM_State2 == 3)
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis3 > buttonCheckInterval)
{
previousMillis3 = currentMillis;
buttonState4 = digitalRead(buttonPin4);
if(buttonState4 == HIGH)

{
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin3, LOW);
}
else
{
digitalWrite(ledPin4, LOW);
FSM_State2 = 0;
}
}
}
//The same as state 2, for the second button
else if(FSM_State2 == 4)
{
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPin3, LOW);
unsigned long currentMillis = millis();

if(currentMillis - previousMillis3 > buttonCheckInterval)
{
previousMillis3 = currentMillis;
buttonState3 = digitalRead(buttonPin3);
if(buttonState3 == HIGH) FSM_State2 = 5;
}

if(currentMillis - previousMillis4 > time_to_light)
{
previousMillis4 = currentMillis;
digitalWrite(ledPin4, LOW);
FSM_State2 = 0;
}
}
//This state exists so that we can have enough time to release the button if we want to
//momentarily press the button to go from 2 or 4 to 0.
else if(FSM_State2 == 5)
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis3 > (buttonCheckInterval * 3))
{
previousMillis3 = currentMillis;
FSM_State2 = 0;
}
}


}
Gyorob1
DrótVégénSzéndarab
Hozzászólások: 25
Csatlakozott: 2018. május 27. vasárnap, 20:26

Re: Arduino kezdő

Hozzászólás Szerző: Gyorob1 »

Tudom programozni külön az analog bemenetet, de nem tudom ide belerakni. Aki ért hozzá annak nem bonyolult. Az lenne a lényeg ha analog bemeneten mondjuk 700 felett van akkor ledPin2 illetve ledPin4 menjen kb 30 másodpercig. Redőnyt akarok vezérelni vele.
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Re: Arduino kezdő

Hozzászólás Szerző: Robert »

Aki ért hozzá annak nem bonyolult.
Tipikus.... :twisted:

z lenne a lényeg ha analog bemeneten mondjuk 700 felett van akkor ledPin2 illetve ledPin4 menjen kb 30 másodpercig. Redőnyt akarok vezérelni vele.
pszeudokod:

Kód: Egész kijelölése

If (readanalog(analog)>700) {
   ledpin2 = high
ledpin4 = high
delay 30 sec
ledpin2 =low
ledpin4=low
}

és utána a műszaki meglátásaim a programoddal:

- saját kód, vagy "talált"?
- nekem nagyon hiányzik a folyamatábra illetve az állapotgép logikája...
- az analog beolvasás során kellene egy "bizonytalan" terület:
0....650 - állapot 1 és 750....1023 - állapot2. 650 és a 750 közt van a "prell" területe.
Kérdés: miért nem úgy oldod meg, minth gomb/kapcsoló lenne?
digital inputként digitalread(analog) ugyanúgy működik. Low/High az eredmény.


A kódból a
- "nem értelmezhető, hibás beállás" részlet hiányzik,
- hiányzik az áramszünet esetén mi történik? Mi van az áram megjelenésekor?
- mi van 49 nap után, amikor a milis() átcsordul?
http://www.tavir.hu - a gazda :)
Gyorob1
DrótVégénSzéndarab
Hozzászólások: 25
Csatlakozott: 2018. május 27. vasárnap, 20:26

Re: Arduino kezdő

Hozzászólás Szerző: Gyorob1 »

Talált kód, ezt akartam tovább fejleszteni. Hu nem is gondoltam ezekre. Igazából próbálkozok csak még vele.
Gyorob1
DrótVégénSzéndarab
Hozzászólások: 25
Csatlakozott: 2018. május 27. vasárnap, 20:26

Re: Arduino kezdő

Hozzászólás Szerző: Gyorob1 »

Minden tanácsot szívesen fogadok, célom az hogy jól működjön. Igazából még ma kezdtem el írni a programot koránt sincs kész.
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Re: Arduino kezdő

Hozzászólás Szerző: Robert »

Az első javaslat:
- rajzolj egy blokkdiagramm - folyamatábrát. Ezzel válik a program logikája áttekinthetővé. (Papír alapon a legegyszerűbb skiccet rajzolni)
- 60 nap alatt Arduino tanfolyam ?:) http://www.tavir.hu/60nap
http://www.tavir.hu - a gazda :)
Gyorob1
DrótVégénSzéndarab
Hozzászólások: 25
Csatlakozott: 2018. május 27. vasárnap, 20:26

Re: Arduino kezdő

Hozzászólás Szerző: Gyorob1 »

Üdv. Több programrészből áll a programom. Az lenne a kérdés hogy hogyan tudnám megoldani azt hogy ha megy egy bizonyos rész a többit tiltsa le.
Avatar
Robert
Elektronbűvölő
Hozzászólások: 10191
Csatlakozott: 2005. december 9. péntek, 7:00

Re: Arduino kezdő

Hozzászólás Szerző: Robert »

???
AVR chip esetén: 1 processzor mag van, 1 végrehajtási szál.

A kérdés kifejtése lehetne bővebb.....
http://www.tavir.hu - a gazda :)
Gyorob1
DrótVégénSzéndarab
Hozzászólások: 25
Csatlakozott: 2018. május 27. vasárnap, 20:26

Re: Arduino kezdő

Hozzászólás Szerző: Gyorob1 »

Az a lényeg hogy az egyik része a programnak high ra hozza a kimenetet, de egy másik részben Low rá kéne de mivel az előző high on tartja nem megfelelő a kimeneti jel. Tehát 1 kimenetet 2 bemenetről szeretnék vezérelni. Mikor megy az egyik addig állítsa le a másik futasat, utána természetesen vissza. Nem tudom érthető voltam e.?! :D
Válasz küldése