Arduino kezdő

Processing/Wiring (illetve C) nyelvű programozási fogások, tippek. (AVR-Duino, Arduino, EthDuino, Diecimila, Severino, Nano, LilyPad)
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 »

Nem. Nem volt érthető...

Olyan, hogy tartja nincs, hogy
- program magasan _tartja_ a kimenetet.
Pl. digitalwrite(D10,HIGH)-l beállítod magasra kimenetet. Akár a következő utasítással LOW-ba viheted...

Folyamatábrában rajzold meg mit szeretnél. Onnan sokkal egyszerűbb lesz a programváz. Ne If/Else alapon gondolkodj!
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 »

http://www.elektromanoid.hu/progi22.html

Itt talán szó van róla.
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 »

Van egy netről leszedett kódom ami tökéletesen működik. (néhány hozzá szólással ezelőtt felraktam) azt próbálom bővíteni. Redőnyt vezérelne. 2 redőny van a kódban (ezt tudom bővíteni). Azt szeretném hogy legyen benne egy főnyomógomb ami mindet vezérli egyszerre.
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 »

Kulcsszó: állapotgép.

Azaz a feltételeket a főgombos _és_ a többi gonb határozza meg.
A linkelt megszkítás alapú kezelés - nem erre való....
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 »

Értem. Akkor teljesen rossz az irány?
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 »

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

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 »

Ebbe a kódba szerinted bele lehetne írni?
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 »

Persze. Ha jó a kód a keretrendszer lefordítja :)
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 »

Te hogy kezdenéd el? :D
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 »

#define...

setup()


loop()

0. lépés telefon kikapcsol, A4 papírlap és ceruza, radír elővesz..
De az első:
folyamatábra!
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 »

Rendben van. Megpróbalom. Köszönöm :)
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 »

Nem jövök rá. Működik a kapcsolás de mivel az elsö parancs High-ra hozza a kimenetet mikor low-ra akarom hozni egy másik paranccsal akkor sipol a relé. 2-3v közt van a kimenet. Rossz a programom.
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 buttonPin17 = 12; //főnyomó
const int buttonPin18 = 13; //főnyomó
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 = 5000;

int buttonState = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState17 = 0;
int buttonState18 = 0;

long buttonCheckInterval = 100;
long buttonDecideInterval = 500;

long previousMillis = 0;
long previousMillis2 =0;
long previousMillis3 = 0;
long previousMillis4 =0;
long previousMillis17 =0;
long previousMillis18 =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 button17_pressed = 0;
int button17_pressed_old = 0;
int button18_pressed = 0;
int button18_pressed_old = 0;

int FSM_State =0;
int FSM_State2 =0;
int FSM_State3 =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);

pinMode(buttonPin17, INPUT);
pinMode(buttonPin18, INPUT);


}

void loop(){





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


if(FSM_State == 0)
{

//Várjuk a gombnyomásokat, így letiltjuk a LED-eket és a redőnyöket.

digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);


//Ha az intervallum lejárt.
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;
}
}



// read the state of the pushbutton values:------------------------------------FŐnyomó----------------------------






buttonState17 = digitalRead(buttonPin17);
buttonState18 = digitalRead(buttonPin18);

if(FSM_State3 == 0)
{
//We are waiting for button presses, so we disable the LEDs and shutters.
//*******************************************************************************************************************************************************

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

//The same for the second button

if (buttonState18 == HIGH)
{
if(button18_pressed_old == 0) button18_pressed=1;
else button18_pressed=2;
}
}

//Check if it's time to decide wether we have a momentary press, a press-and-hold, or nothing
if(currentMillis - previousMillis18 > buttonCheckInterval*5)
{
previousMillis18 = 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(button17_pressed == 2)
FSM_State3 = 1;
//digitalWrite(ledPin, HIGH);
else if(button17_pressed_old == 1)
FSM_State3 = 2;

button17_pressed_old = button17_pressed;
button17_pressed = 0;

//Do the same for button2
if(button18_pressed == 2) FSM_State3 = 3; //digitalWrite(ledPin, HIGH);
else if(button18_pressed_old == 1) FSM_State3 = 4;
button18_pressed_old = button18_pressed;
button18_pressed = 0;
}
}
else if(FSM_State3 == 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 - previousMillis17 > buttonCheckInterval)
{
previousMillis17 = currentMillis;
buttonState17 = digitalRead(buttonPin17);
if(buttonState17 == HIGH)
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, LOW);
}
else
{
digitalWrite(ledPin, LOW);
digitalWrite(ledPin3, LOW);
FSM_State3 = 0;
}
}
}
else if(FSM_State3 == 2)
{
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, LOW);
unsigned long currentMillis = millis();

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

if(currentMillis - previousMillis17 > buttonCheckInterval)
{
previousMillis17 = currentMillis;
buttonState17 = digitalRead(buttonPin17);
if(buttonState17 == HIGH) FSM_State3 = 5;
}

if(currentMillis - previousMillis18 > time_to_light)
{
previousMillis18 = currentMillis;
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin4, LOW);
FSM_State3 = 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_State3 == 5)
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis17 > (buttonCheckInterval * 3))
{
previousMillis17 = currentMillis;
FSM_State3 = 0;
}

}




}
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 »

A rajzolt folyamatábra hol van? Ezt így a programból visszarajzolni nem valami élvezet:(.
http://www.tavir.hu - a gazda :)
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 »

Tisztelt Gyorob1!
Mivel a kezdők rovatba tetted fel a kérdésed, adok 1 kezdő tanácsot!
Ilyen hosszú és bonyolult programba nem próbálunk belenyúlni kezdőként!
És ha mégis megteszed, akkor legalább megtiszteljük a fórum tagokat, hogy figyelmesek vagyunk.
Mikor felteszel 1 kérdést, megnyomod alul a válasz gombot, felugrik a hozzászólás küldése ablak.
A szerkesztő mező felett találsz szerkesztést segítő gombokat!
Lényeges lenne, hogy használd a Code gombot mikor forráskódot akarsz beilleszteni!
És a beugró 2 záró tag között villogó kurzor helyére illeszteni a programodat.
Ez által a szövegszerkesztő nem szedi ki a tördeléseket az egészből, mivel azok lényeges szükséges részei a program értelmezéséhez.

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

Igy kellene kinézni e a beszúrt kódnak:
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;
      }
    }
Ezek után bocsi, ha ezt nem fogja neked senki javítani!
Igaza van Robinak! Programozást tanulni a programozó ABC-töl kel
Válasz küldése