Hello again!
I've checked the state machine programming, and put together some code. I managed to make the motor "go up" for 6 seconds when starting arduino, and after pressing a button, it "goes down" for 6 seconds. The problem i have now is that it doesn't reinitialize. So it turns up once automatically, i can turn it down once manually, but when pressing the button again, it should again go up, and so on. Well.. it stops after the first two.
I think the problem's in the if statement found in the Wait4Press state, or with those mstatus variables. Here's my code till now.
#include <SM.h>
#define pin12 12
#define pin13 13
#define sw 2
SM ArduinoOn(On);
int mstatus;
//if mstatus = 1 => motor has gone up
//if mstatus = -1=> motor has gone down
void setup()
{
pinMode(pin12, OUTPUT);
pinMode(pin13, OUTPUT);
pinMode(sw, INPUT);
// Serial.begin(9600);
}
void loop()
{
EXEC(ArduinoOn);
}
State On()
{
// Serial.println("Arduino ON, going to Initialize");
if (ArduinoOn.Timeout(1000))
{
ArduinoOn.Set(Initialize);
}
}
State Initialize()//initialize will turn the motor up for 6 sec
{
// Serial.println("Arduino initialized, stopping action, storing monitorOn var");
// Serial.println(" motor variable is now");
// Serial.print(mstatus);
digitalWrite(pin13, HIGH);
mstatus = 1;
if (ArduinoOn.Timeout(6000))
{
ArduinoOn.Set(StopUpAction);
}
}
State StopUpAction()
{
digitalWrite(pin13, LOW);
mstatus = 1;
ArduinoOn.Set(Wait4Press);
}
State Wait4Press()
{
if ( (digitalRead(sw) == HIGH) && mstatus == 1) //if the button is pressed and the motor is UP, pull it down
{
ArduinoOn.Set(GoDownAction);
if ((digitalRead(sw) == HIGH) && mstatus == -1)//if the button is pressed and the motor is DOWN, pull it up
{
ArduinoOn.Set(Initialize);
}
}
}
State GoDownAction()
{
digitalWrite(pin12, HIGH);
mstatus = -1;
if (ArduinoOn.Timeout(6000))
{
ArduinoOn.Set(StopDownAction);
}
}
State StopDownAction()
{
digitalWrite(pin12, LOW);
mstatus = -1;
// Serial.println("action stopped, var is now");
// Serial.print(mstatus);
ArduinoOn.Set(Wait4Press);
}
I've also tried an if/else statement, but had the same result.
Thanks for your help, and sorry for the hold up, but i have exams.