I am trying to create a state machine but am a little lost. Please go easy on me, i've only been coding 4 months.
Im using a pic16f18325 and coding in mplab
So i think im pretty much there, i know what i want it to do, i have written diagrams etc and think i have set it up correctly. My question is how to i switch between the states? How do i change an event?
If for example i was in the maincode and i wanted to trigger the ENTER event by press of a button. Would i just write:
if (button == 0){ // (button has been defined already)
event = ENTER;
}
void ENTER (void){
// do some stuff
}
mplab is not liking the word "event" im getting a fair few errors that i dont understand:
main.c:33:6: error: redefinition of 'ENTER' as different kind of symbol
void ENTER (void);
^
main.c:26:5: note: previous definition is here
int ENTER; // for entering programming from maincode
^
main.c:34:6: error: redefinition of 'EXIT' as different kind of symbol
void EXIT (void);
^
main.c:27:5: note: previous definition is here
int EXIT; // for exiting programming and returning to maincode
^
main.c:35:6: error: redefinition of 'Reset' as different kind of symbol
void Reset (void);
^
main.c:28:5: note: previous definition is here
int Reset; // to reset the speed and return to main code
^
main.c:36:6: error: redefinition of 'INCREASE' as different kind of symbol
void INCREASE (void);
^
main.c:29:5: note: previous definition is here
int INCREASE; // to increase the speed while in programming
^
main.c:37:6: error: redefinition of 'DECREASE' as different kind of symbol
void DECREASE (void);
^
main.c:30:5: note: previous definition is here
int DECREASE; // to decrease the speed while in programming
^
main.c:110:36: error: function definition is not allowed here
void step_state(enum events event) { // not sure what to write here, will work it out
^
main.c:145:18: error: function definition is not allowed here
void ENTER (void){
^
main.c:148:17: error: function definition is not allowed here
void EXIT (void){
^
main.c:151:18: error: function definition is not allowed here
void Reset (void){
^
main.c:154:21: error: function definition is not allowed here
void INCREASE (void){
^
main.c:157:21: error: function definition is not allowed here
void DECREASE (void){
^
11 errors generated.
Heres my state machine coding:
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/device_config.h"
int ENTER; // for entering programming from maincode
int EXIT; // for exiting programming and returning to maincode
int Reset; // to reset the speed and return to main code
int INCREASE; // to increase the speed while in programming
int DECREASE; // to decrease the speed while in programming
int events;
void ENTER (void);
void EXIT (void);
void Reset (void);
void INCREASE (void);
void DECREASE (void);
void main(void) {
enum states { // this state machine will have 2 states
MAINCODE, // main code where it will sit most of the time
PROGRAMMING, // and then programming which is used to change the speed
} state;
enum events { // we will have 5 events
ENTER, // for entering programming from maincode
EXIT, // for exiting programming and returning to maincode
RESET, // to reset the speed and return to main code
INCREASE, // to increase the speed while in programming
DECREASE, // to decrease the speed while in programming
};
void step_state(enum events event) { // not sure what to write here??? what is step_state? why enum events event?
switch(state) { // the switch is done by a change of state
case MAINCODE: // while in maincode
switch(event) { // and the enter event is initiated
case ENTER: // enter programming
state = PROGRAMMING; // to do this we change the state
break; // break to jump into that state
default: // if something messes up, the default instruction
state = MAINCODE; // is to go back into the maincode
break;
}
break;
case PROGRAMMING: // while in programming state
switch(event) { // and an event is triggered
case EXIT: // which happens to be exit
state = MAINCODE; // enter maincode
break;
case RESET:
// add code to revert default speed value
state = MAINCODE; // then enter maincode
break;
case INCREASE:
// add code for increasing speed
break;
case DECREASE:
// add code for decreasing speed
break;
default:
state = MAINCODE;
break;
}
break;
}
}
if (button == 0){
event = ENTER;
}
void ENTER (void){
state = PROGRAMMING;
}
if (button == 0){
event = EXIT;
}
void EXIT (void){
state = MAINCODE;
}
if (button == 0 && but1 == 0){
event = Reset;
}
void Reset (void){
value = default;
state = MAINCODE;
}
if (but1 == 0){
event = INCREASE;
}
void INCREASE (void){
// add code to increase speed
}
if (but2 == 0){
event = DECREASE;
}
void DECREASE (void){
// add code to decrease speed
}
while (1)
{
}
}