Hello
Im trying to make a little gadget here.
it will use atleast 3 buttons.
1 for selecting a program (program 1-6)
2 and 3 will do some keystrokes
how can i make the 2 and 3 button to do different keystrokes depending on wich program i selected with button 1?
as i understand i need to use switch-case.
i got it to work to change program.
but when i try to add a button action in a case i get stuck.
i will later on use a small oled-screen. but for now i just trying to get it to work with the serial monitor.
this is how far got now
its a mix from 2 other examples.
Can someone please help me why i get stuck in "action1()"
I can not change program. just print Executing #1 with my button.
/*
* This code create a MENU structure on a 16x2 LCD screen.
* Six (6) different screens are implemented, and you can travel
* thru them by pressing a button on Arduino PIN 4. Each press
* of the button advances one (1) screen.
*
* Made by Clovis Fritzen in 05/06/2017
* http://www.FritzenMaker.com
* http://www.FritzenLab.com.br
*/
//Based on YWROBOT's LiquidCrystal_I2C library, Library version:1.1
//Also based on the Debounce.ino sketch that comes with Arduino IDE
#include <Wire.h>
int WhichScreen =1; // This variable stores the current Screen number
boolean hasChanged = true;
const int buttonPin = 7; // the number of the pushbutton pin
int selectButton = 8;
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(selectButton, INPUT);
Serial.begin(9600);
}
void loop()
{
if (hasChanged == true) {
switch(WhichScreen) {
case 1:
{
firstScreen();
}
break;
case 2:
{
secondScreen();
}
break;
case 3:
{
thirdScreen();
}
break;
case 4:
{
fourthScreen();
}
break;
case 5:
{
fifthScreen();
}
break;
case 6:
{
sixthScreen();
}
break;
case 0:
{
}
break;
}
}
//-------------------------------
// BEGIN of the switch debouncing code
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
hasChanged = true;
WhichScreen++;
}
} else {
hasChanged = false;
}
}
// execute button start
if (!digitalRead(selectButton)){
executeAction();
//updateMenu();
delay(100);
while (!digitalRead(selectButton));
}
// execute button end
lastButtonState = reading;
// END of the switch Debouncing code
// --------------------------------------
if (WhichScreen > 6){
WhichScreen = 1;
}
}
void firstScreen()
{
Serial.println("Hello world 1");
}
void secondScreen()
{
Serial.println("Hello world 2");
}
void thirdScreen()
{
Serial.println("Hello world 3");
}
void fourthScreen()
{
Serial.println("Hello world 4");
}
void fifthScreen()
{
Serial.println("Hello world 5");
}
void sixthScreen()
{
Serial.println("Hello world 6");
}
// the executebutton
void executeAction() {
switch (WhichScreen) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
}
}
void action1() {
Serial.println(">Executing #1");
delay(100);
}
void action2() {
Serial.println(">Executing #2");
delay(100);
}
void action3() {
Serial.println(">Executing #3");
delay(100);
}
void action4() {
Serial.println(">Executing #4");
delay(100);
}