I am quite new on arduino and Im trying to make a 4 layer menu. Im done with the first layer I can scroll through it. I am also done making the contents of the 2nd layer menu together with its "control" (scrolling function) but everytime i try to scroll the items on the 2nd layer the function I made for scrolling the first layer runs the same time as the scroll function of the second layer. I need a code to stop the function of the first layer when I'm on the 2nd layer or any layer and call the function which is responsible for the control of the current layer. Thank you.
Your sketch needs to store state information. Usually the easiest way is to name each possible state in an 'enum' and use the names to set a state variable. You would typically use a 'switch/case' statement to preform the actions of each state:
enum {Menu1, Menu1A, Menu1B, Menu1C, Menu2} State = Menu1;
void loop()
{
switch(State)
{
case Menu1:
case Menu1A:
case Menu1B:
case Menu1C:
case Menu2:
}
}
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <ButtonDebounce.h> //Library for Debouncing Buttons
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
ButtonDebounce up(8, 75); //name_of_pin(pinnumber, debouncing_delay)
ButtonDebounce down(9, 75);
ButtonDebounce select(10, 75);
int process = 0;
int assy = 0;
int wind = 0;
int startupRunOnce = 0;
void setup() {
lcd.init(); //start LCD
lcd.backlight(); //start LCD backlight
processMenuTents();
}
void loop() {
up.update(); // read UP button
down.update(); // read DOWN button
select.update(); // read SELECT button
processMenuCon();
if (startupRunOnce == 0) {
STARTUP();
startupRunOnce++;
}
}
void STARTUP() {
lcd.clear();
lcd.setCursor(3,1);
lcd.print("COIL RESISTANCE");
lcd.setCursor(2,2);
lcd.print("INSPECTION TESTER");
}
/* ===================================================================================================================================================================================
PROCESS MENU CONTROLS
===================================================================================================================================================================================*/
void processMenuCon(){
if(up.state() == HIGH){
process++;
processMenuTents();
delay(100);
}
if(down.state() == HIGH){
process--;
processMenuTents();
delay(100);
}
if(select.state() == HIGH){
turnToModel();
windMenuTents();
windMenuCon();
assyMenuTents();
assyMenuCon();
delay(100);
}
}
/* ===================================================================================================================================================================================
PROCESS MENU CONTENTS
===================================================================================================================================================================================*/
void processMenuTents() {
switch (process){
case 0:
process = 3;
case 1:
lcd.clear();
lcd.setCursor(16,0);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("WINDING COIL");
lcd.setCursor(0,1);
lcd.print("ASSEMBLY COIL");
break;
case 2:
lcd.clear();
lcd.setCursor(16,1);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("WINDING COIL");
lcd.setCursor(0,1);
lcd.print("ASSEMBLY COIL");
break;
case 3:
process = 0;
}
}
/* ===================================================================================================================================================================================
TURNING TO MODEL MENU
===================================================================================================================================================================================*/
void turnToModel() {
switch (process){
case 1:
windMenuTents();
break;
case 2:
assyMenuTents();
break;
}
}
/* ===================================================================================================================================================================================
WIND MENU CONTROLS
===================================================================================================================================================================================*/
void windMenuCon(){
if(up.state() == HIGH){
wind++;
windMenuTents();
delay(100);
}
if(down.state() == HIGH){
wind--;
windMenuTents();
delay(100);
}
if(select.state() == HIGH){
delay(100);
}
}
/* ===================================================================================================================================================================================
WIND MENU CONTENTS
===================================================================================================================================================================================*/
void windMenuTents() {
switch (wind){
case 0:
wind = 8;
case 1:
lcd.clear();
lcd.setCursor(16,0);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 2:
lcd.clear();
lcd.setCursor(16,1);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 3:
lcd.clear();
lcd.setCursor(16,2);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 4:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 5:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCR");
lcd.setCursor(0,1);
lcd.print("MCU");
lcd.setCursor(0,2);
lcd.print("ME");
lcd.setCursor(0,3);
lcd.print("MS");
break;
case 6:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCR");
lcd.setCursor(0,1);
lcd.print("MCU");
lcd.setCursor(0,2);
lcd.print("ME");
lcd.setCursor(0,3);
lcd.print("MS");
break;
case 7:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCU");
lcd.setCursor(0,1);
lcd.print("ME");
lcd.setCursor(0,2);
lcd.print("MS");
lcd.setCursor(0,3);
lcd.print("MZ");
break;
case 8:
wind = 0;
}
}
/* ===================================================================================================================================================================================
ASSEMBLY MENU CONTROLS
===================================================================================================================================================================================*/
void assyMenuCon(){
if(up.state() == HIGH){
assy++;
assyMenuTents();
delay(100);
}
if(down.state() == HIGH){
assy--;
assyMenuTents();
delay(100);
}
if(select.state() == HIGH){
processMenuTents();
delay(100);
}
}
/* ===================================================================================================================================================================================
ASSEMBLY MENU CONTENTS
===================================================================================================================================================================================*/
void assyMenuTents() {
switch (assy){
case 0:
assy = 8;
case 1:
lcd.clear();
lcd.setCursor(16,0);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 2:
lcd.clear();
lcd.setCursor(16,1);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 3:
lcd.clear();
lcd.setCursor(16,2);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 4:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 5:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCR");
lcd.setCursor(0,1);
lcd.print("MCU");
lcd.setCursor(0,2);
lcd.print("ME");
lcd.setCursor(0,3);
lcd.print("MS");
break;
case 6:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCR");
lcd.setCursor(0,1);
lcd.print("MCU");
lcd.setCursor(0,2);
lcd.print("ME");
lcd.setCursor(0,3);
lcd.print("MS");
break;
case 7:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCU");
lcd.setCursor(0,1);
lcd.print("ME");
lcd.setCursor(0,2);
lcd.print("MS");
lcd.setCursor(0,3);
lcd.print("MZ");
break;
case 8:
assy = 0;
}
}
/* ===================================================================================================================================================================================
TURNING TO MODEL MENU
===================================================================================================================================================================================*/
[/quote]
Here is my code for reference.
How do I store the status of a function if its running or not?
Why ?
What are you trying to accomplish ?
If you are running code in that function, then you know you must be in that function.
I would like to use that in if/else to call another function to run.\
just like if this function is running run function1 else run function 2.
i hope it makes sense
int pin = 2;
void setup() {
Serial.begin(9600);
pinMode(pin , INPUT);
}
void loop() {
pinRead = digitalRead(pin);
if(pinRead)
function1();
else
function2();
delay(500);
}
void function1()
{
Serial.println("Function1 Runing ");
}
void function2()
{
Serial.println("Function2 Runing = ");
}
Not really.
I don't think you understand how functions work.
When you call a function it runs until it ends. There is nothing you can do about it until it ends. Then you can call the second function, which behaves the same.
Are you using some sort of operating system with a dual core processor?
What sort of Arduino do you have?
Mega ADK
Very short answer?
Anyway, did you understand the rest of the things I said in my post?
nope.
Yes, I'm making a multi layered menu. On the first layer I have the list and the function that controls is. now on my the second layer which is selecting one of the item on the main, I have multiple items too and a function that supposed to cotrol the scrolling but running it seems to run both the main menu funtion and 2nd layer menu function for scrolling. I need them to run at their perspective layer only
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <ButtonDebounce.h> //Library for Debouncing Buttons
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
ButtonDebounce up(8, 75); //name_of_pin(pinnumber, debouncing_delay)
ButtonDebounce down(9, 75);
ButtonDebounce select(10, 75);
int process = 0;
int assy = 0;
int wind = 0;
int startupRunOnce = 0;
void setup() {
lcd.init(); //start LCD
lcd.backlight(); //start LCD backlight
processMenuTents();
}
void loop() {
up.update(); // read UP button
down.update(); // read DOWN button
select.update(); // read SELECT button
processMenuCon();
if (startupRunOnce == 0) {
STARTUP();
startupRunOnce++;
}
}
void STARTUP() {
lcd.clear();
lcd.setCursor(3,1);
lcd.print("COIL RESISTANCE");
lcd.setCursor(2,2);
lcd.print("INSPECTION TESTER");
delay(3000);
}
/* ===================================================================================================================================================================================
PROCESS MENU CONTROLS
===================================================================================================================================================================================*/
void processMenuCon(){
if(up.state() == HIGH){
process++;
processMenuTents();
delay(100);
}
if(down.state() == HIGH){
process--;
processMenuTents();
delay(100);
}
if(select.state() == HIGH){
turnToModel();
delay(100);
}
}
/* ===================================================================================================================================================================================
PROCESS MENU CONTENTS
===================================================================================================================================================================================*/
void processMenuTents() {
switch (process){
case 0:
process = 3;
case 1:
lcd.clear();
lcd.setCursor(16,0);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("WINDING COIL");
lcd.setCursor(0,1);
lcd.print("ASSEMBLY COIL");
break;
case 2:
lcd.clear();
lcd.setCursor(16,1);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("WINDING COIL");
lcd.setCursor(0,1);
lcd.print("ASSEMBLY COIL");
break;
case 3:
process = 0;
}
}
/* ===================================================================================================================================================================================
TURNING TO MODEL MENU
===================================================================================================================================================================================*/
void turnToModel() {
switch (process){
case 1:
windMenuTents();
windMenuCon();
break;
case 2:
assyMenuTents();
assyMenuCon();
break;
}
}
/* ===================================================================================================================================================================================
WIND MENU CONTROLS
===================================================================================================================================================================================*/
void windMenuCon(){
if(up.state() == HIGH){
wind++;
windMenuTents();
delay(100);
}
if(down.state() == HIGH){
wind--;
windMenuTents();
delay(100);
}
if(select.state() == HIGH){
delay(100);
}
}
/* ===================================================================================================================================================================================
WIND MENU CONTENTS
===================================================================================================================================================================================*/
void windMenuTents() {
switch (wind){
case 0:
wind = 8;
case 1:
lcd.clear();
lcd.setCursor(16,0);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 2:
lcd.clear();
lcd.setCursor(16,1);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 3:
lcd.clear();
lcd.setCursor(16,2);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 4:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 5:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCR");
lcd.setCursor(0,1);
lcd.print("MCU");
lcd.setCursor(0,2);
lcd.print("ME");
lcd.setCursor(0,3);
lcd.print("MS");
break;
case 6:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCR");
lcd.setCursor(0,1);
lcd.print("MCU");
lcd.setCursor(0,2);
lcd.print("ME");
lcd.setCursor(0,3);
lcd.print("MS");
break;
case 7:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCU");
lcd.setCursor(0,1);
lcd.print("ME");
lcd.setCursor(0,2);
lcd.print("MS");
lcd.setCursor(0,3);
lcd.print("MZ");
break;
case 8:
wind = 0;
}
}
/* ===================================================================================================================================================================================
ASSEMBLY MENU CONTROLS
===================================================================================================================================================================================*/
void assyMenuCon(){
if(up.state() == HIGH){
assy++;
assyMenuTents();
delay(100);
}
if(down.state() == HIGH){
assy--;
assyMenuTents();
delay(100);
}
if(select.state() == HIGH){
delay(100);
}
}
/* ===================================================================================================================================================================================
ASSEMBLY MENU CONTENTS
===================================================================================================================================================================================*/
void assyMenuTents() {
switch (assy){
case 0:
assy = 8;
case 1:
lcd.clear();
lcd.setCursor(16,0);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 2:
lcd.clear();
lcd.setCursor(16,1);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 3:
lcd.clear();
lcd.setCursor(16,2);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 4:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCF");
lcd.setCursor(0,1);
lcd.print("MCR");
lcd.setCursor(0,2);
lcd.print("MCU");
lcd.setCursor(0,3);
lcd.print("ME");
break;
case 5:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCR");
lcd.setCursor(0,1);
lcd.print("MCU");
lcd.setCursor(0,2);
lcd.print("ME");
lcd.setCursor(0,3);
lcd.print("MS");
break;
case 6:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCR");
lcd.setCursor(0,1);
lcd.print("MCU");
lcd.setCursor(0,2);
lcd.print("ME");
lcd.setCursor(0,3);
lcd.print("MS");
break;
case 7:
lcd.clear();
lcd.setCursor(16,3);
lcd.print("<---");
lcd.setCursor(0,0);
lcd.print("MCU");
lcd.setCursor(0,1);
lcd.print("ME");
lcd.setCursor(0,2);
lcd.print("MS");
lcd.setCursor(0,3);
lcd.print("MZ");
break;
case 8:
assy = 0;
}
}
/* ===================================================================================================================================================================================
TURNING TO MODEL MENU
===================================================================================================================================================================================*/
As @Grumpy_Mike says
Now you need to decide when a function ends. This is a simple example of BlinkWithoutDelay.
void loop()
{
// function starts
bwod();
// function did end
}
/*
blink without delay
*/
void bwod()
{
static uint32_t delayStarttime;
if (millis() - delayStarttime >= 1000)
{
delayStarttime = millis();
if (ledStatus == HIGH)
{
ledStatus = LOW;
digitalWrite(ledPin, ledStatus);
}
else
{
ledStatus = HIGH;
digitalWrite(ledPin, ledStatus);
}
}
else
{
}
// function ends here
}
Now if you want to know when the led changed, you can modify the code to return a bool.
void loop()
{
// function starts
bool rv = bwod();
// function ends
// check result
if (rv == true)
{
Serial.println(F("Led changed"));
}
}
/*
blink without delay
Returns:
true if led changed, else false
*/
bool bwod()
{
static uint32_t delayStarttime;
if (millis() - delayStarttime >= 1000)
{
delayStarttime = millis();
if (ledStatus == HIGH)
{
ledStatus = LOW;
digitalWrite(ledPin, ledStatus);
}
else
{
ledStatus = HIGH;
digitalWrite(ledPin, ledStatus);
}
return true;
// function ends with the return statement
}
else
{
return false;
// function ends with the return statement
}
// you will not get here
}
If your function is more complex, you might have to make use of an enum to be able to determine where your function exactly returned.
//Edit
Written before you posted your code so I did not look at that; your function can also return numbers instead of a bool to indicate which menu selection was made.
What ever is your problem is you are NOT running two functions at the same time.
Does my code makes sense tho? I'm kinda not sure about it.
I'm trying to scroll through the second layer but it goes back to my main menu. Any ideas y? thanksss
No. It is all over the place and has no logical structure I can see.
You are not passing values to functions and using global variables for everything.
That is because that is what your code is telling it to do.
You want to draw a functional diagram of the states you want to do and check this against your code.
What should I do?