Pages: [1]   Go Down
Author Topic: STATE MACHINE  (Read 367 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy everybody,
many thanks in advance at who will tell me where i'm wrong.
I want just control an led, with BUTTON to switch it on and with BUTTON1 to switch it off. If now i run the code the led is still on and no control with buttons.
Thanks again.
Andy.
Code:
PIN definitions
#define RELAY_PIN 42
#define BUTTON 22
#define BUTTON1 24
// FSM states
#define PINZA_CHIUSA  0
#define PINZA_APERTA   1

int statopinza;

void setup() {
pinMode(BUTTON, INPUT);
pinMode(BUTTON1, INPUT);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
statopinza = PINZA_CHIUSA;
}

void loop() {
int apri;
int chiudi; 
apri=digitalRead(BUTTON);
chiudi=digitalRead(BUTTON1);
switch(statopinza)
{
case PINZA_CHIUSA:
 
if(apri=HIGH)
{
digitalWrite(RELAY_PIN, HIGH);
statopinza = PINZA_APERTA;
}
break;
case PINZA_APERTA:

if (chiudi=HIGH)
{
digitalWrite(RELAY_PIN, LOW);
statopinza =PINZA_CHIUSA;
}   
break;

  }
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How are your switches wired?

Code:
int apri;
int chiudi; 
apri=digitalRead(BUTTON);
chiudi=digitalRead(BUTTON1);
Why do you feel it necessary to separate the declaration from the initialization?

Code:
if(apri=HIGH)
Most people find that == (the equality comparison operator) works better than = (the assignment operator) in an if statement.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy Pauls,
many many many thanks. I'm starting to work with Arduino and i'm non expert. thanks to your suggestion (==) now is perfectly running.
About to separate the declaration from the initialization, i just copied an example found on the web: is that wrong?
Many thanks again.
Andy.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49270
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is that wrong?
It isn't right or wrong. It is unnecessary. Keeping the declaration and initialization together makes it clear what type of variable is being used, and uses fewer lines.

Code:
int apri = digitalRead(BUTTON);
int chiudi = digitalRead(BUTTON1);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hy Pauls,
you mean is just a question of clarity!
Thanks again for your help.
Andy
Logged

Pages: [1]   Go Up
Jump to: