I am building a device and I want the user to acces four different settings menus with the help of 4 momentary switches. Then, when done, return to the main function with the help of a fifth momentary switch. I do not want to use interrupts as I need to use the switches inside the four functions. I have tried a why loop:
const int SetSwitch = 2;
const int UpSwitch = 3;
const int DownSwitch = 4;
const int LeftSwitch = 5;
const int RightSwitch = 6;
volatile bool SetSwitchState;
volatile bool UpSwitchState;
volatile bool DownSwitchState;
volatile bool LeftSwitchState;
volatile bool RightSwitchState;
void SettingMenu0 () {
Serial.println("0");
}
void SettingMenu1 () {
Serial.println("1");
}
void SettingMenu2 () {
Serial.println("2");
}
void SettingMenu3 () {
Serial.println("3");
}
void MainLoop() {
Serial.println("Working...");
}
int main() {
// put your main code here, to run repeatedly:
Serial.begin(9600);
pinMode(SetSwitch, INPUT_PULLUP);
pinMode(UpSwitch, INPUT_PULLUP);
pinMode(DownSwitch, INPUT_PULLUP);
pinMode(LeftSwitch, INPUT_PULLUP);
pinMode(RightSwitch, INPUT_PULLUP);
SetSwitchState = (digitalRead(SetSwitch));
UpSwitchState = (!digitalRead(UpSwitch));
DownSwitchState = (!digitalRead(DownSwitch));
LeftSwitchState = (!digitalRead(LeftSwitch));
RightSwitchState = (!digitalRead(RightSwitch));
while (!SetSwitchState) {
if (UpSwitchState) {
SettingMenu0();
delay(50);
}
if (DownSwitchState); {
SettingMenu1();
delay(50);
}
if (LeftSwitchState) {
SettingMenu2();
delay(50);
}
if (RightSwitchState) {
SettingMenu3();
delay(50);
}
MainLoop();
}
}
I have also tried a switch loop:
const int SetSwitch = 2;
const int UpSwitch = 3;
const int DownSwitch = 4;
const int LeftSwitch = 5;
const int RightSwitch = 6;
volatile bool SetSwitchState;
volatile bool UpSwitchState;
volatile bool DownSwitchState;
volatile bool LeftSwitchState;
volatile bool RightSwitchState;
char Switches;
void SettingMenu0 () {
Serial.println("0");
}
void SettingMenu1 () {
Serial.println("1");
}
void SettingMenu2 () {
Serial.println("2");
}
void SettingMenu3 () {
Serial.println("3");
}
void MainLoop() {
Serial.println("Working...");
}
int main() {
// put your main code here, to run repeatedly:
Serial.begin(9600);
pinMode(SetSwitch, INPUT_PULLUP);
pinMode(UpSwitch, INPUT_PULLUP);
pinMode(DownSwitch, INPUT_PULLUP);
pinMode(LeftSwitch, INPUT_PULLUP);
pinMode(RightSwitch, INPUT_PULLUP);
SetSwitchState = (digitalRead(SetSwitch));
UpSwitchState = (!digitalRead(UpSwitch));
DownSwitchState = (!digitalRead(DownSwitch));
LeftSwitchState = (!digitalRead(LeftSwitch));
RightSwitchState = (!digitalRead(RightSwitch));
switch (Switches) {
case 'UpSwitchState':
SettingMenu0();
case 'DownSwitchState':
SettingMenu1();
case 'LeftSwitchState':
SettingMenu2();
case 'RightSwitchState':
SettingMenu3();
default:
MainLoop();
}
}
Both Sketches are failing. Am I missing something? Is there another way to achieve that without using interrupts ?
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
Then you could pick one of those .ino's that you want to get working, with its code posted, you could describe what the code is supposed to do and what its doing instead.
Spend some time studying functions, then use them as a way to exit loop and return at the next instruction. They are very powerful and most programs will not be viable without using them. They also save a lot of redundant code. I taught myself "C" by working with one instruction until I understood it, the next day started over with a new instruction. Sometimes it took a few days to master one. No I do not fully understand them but I can now do what I want to do or have the basic understanding to look up the needed information. Start with the Serial.print("hello gstemarie"); then Serial.println(F("hello gstemarie")); then Serial.print(F("\n\thello gstemarie\n"));. After that add an additional line. Learn the "" options in the print function. Remember you are learning a non spoken language and probably electronics at the same time. These are some of the very basic functions built in. Most important have fun!