Hi
I'm trying to create a sequencial control in IDE.
When step 0 is active and the conditions for step 1 are met, step 1 goes true and step 0 goes false. When the conditions for step 2 are met, step 2 goes true and step 1 goes false, and so on.
STEP0, STEP1 and CONDITION FOR STEP1 are alle defined as boolean values.
- am i missing some semicolons somewhere in the loop?
- is it possible to set two boolean values true/false in this way?
- any other obvious syntax errors?
void loop() {
if (STEP0 == true && CONDITION FOR STEP1 == false){
STEP1 = true
STEP0 = false}
am i missing some semicolons somewhere in the loop?
As you did not post your code, who knows ?
This sounds like a job for switch/case and a state machine.
UKHeliBob:
As you did not post your code, who knows ?
I have pasted the part of the coding i am asking about?
But because you are asking for it once Again, you hereby have the complete program so far:
// Hjælpemærker:
boolean STEP0 = true;
boolean STEP1 = false;
boolean STEP2 = false;
boolean STEP3 = false;
boolean STEP4 = false;
boolean BETINGELSE FOR STEP0 = false;
boolean BETINGELSE FOR STEP1 = false;
boolean BETINGELSE FOR STEP2 = false;
boolean BETINGELSE FOR STEP3 = false;
boolean BETINGELSE FOR STEP4 = false;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// Sekvenskæde
//STEP 0
if (STEP0 == true && CONDITION FOR STEP1 == false){
STEP1 = true
STEP0 = false}
// STEP 1
if (STEP1 == true && BETINGELSE FOR STEP2 == false){
STEP2 = true
STEP1 = false}
// STEP 2
if (STEP2 == true && BETINGELSE FOR STEP3 == false){
STEP3 = true
STEP2 = false}
// STEP 3
if (STEP3 == true && BETINGELSE FOR STEP4 == false){
STEP4 = true
STEP3 = false}
// STEP 4
if (STEP4 == true && BETINGELSE FOR STEP0 == false){
STEP0 = true
STEP4 = false}
}
It doesn't compile. The compiler doesn't like constructs like this:
boolean BETINGELSE FOR STEP0 = false;
A variable cannot contain spaces in its name. That's why you were asked to post your code - it was assumed that what you were posting was pseudocode.
You can solve this first issue like this:
boolean BETINGELSE_FOR_STEP0 = false;
I guess i answered my own question no. 1
void loop() {
if (STEP0 == true && ConditionForTep1 == false){
STEP1 = true;
STEP0 = false;}
Correct?
Maybe, although it looks odd - I'd expect to be testing that the condition for step one is true in that if. But it's very hard to tell from a fragment - post the whole thing. I assume it still doesn't compile?
It's probably a good idea to at least try to compile your sketch before asking about it. The IDE is pretty good at picking up "obvious syntax errors".
Steve
I still think that switch/case would be easier
An example for you to build on
enum states
{
state1 = 1,
state2 = 2,
state3 = 3
};
boolean state1ExitCondition = false;
boolean state2ExitCondition = false;
boolean state3ExitCondition = false;
byte state = state1;
void setup()
{
}
void loop()
{
switch(state)
{
case state1:
//code here executed when in state 1
if (state1ExitCondition == true)
{
state = state2;
state2ExitCondition = false;
}
break;
case state2:
//code here executed when in state 2
if (state2ExitCondition == true)
{
state = state3;
state3ExitCondition = false;
}
break;
case state3:
//code here executed when in state 3
if (state3ExitCondition == true)
{
state = state1;
state1ExitCondition = false;
}
break;
}
//any other code that you want to execute in loop()
}
Thanks a lot for the feedback.
The code is compiling now. The problem was the spaces in the variable names and some missing semicolons.
The way I usually do this is with an enum and a case statement:
enum Step { IDLE, ACTIVATING, DOING_THING, DEACTIVATING } step = IDLE;
void loop() {
switch(step) {
case IDLE:
if(need_to_activate()) {
start_activation();
turn_on_activation_lights();
step = ACTIVATING;
}
break;
case ACTIVATING:
if(activation_complete()) {
turn_off_activation_lights();
step = DOING_THING;
}
else if(activation_cancelled()) {
turn_off_activation_lights();
step = IDLE;
}
break;
case DOING_THING:
if(thing_is_complete()) {
commence_deactivation();
turn_on_deactivation_lights();
step = DEACTIVATING;
}
break;
case DEACTIVATING:
if(deactivation_complete()) {
turn_off_deactivation_lights();
step = IDLE;
}
break;
}
}