Microcontroller used is Teensy 3.2
Arduino 1.8.1
I have pushbutton and a toggle switch (ON1,OFF,ON2)
Pushbutton changes the screen.
Push once: Screen 1
Push twice: Screen 2
Push thrice: Screen 3
Push 4th time: Screen 1
Push 5th time: Screen 2
.
.
.
Continues.
In my screen 1, when the toggle switch's position is changed, I want it to immediately show in my LCD.
What is happening now is that when it is in screen 1, and I changed the toggle switch's position, the code is stuck in that loop and it will not display that change unless I push the button until it completes the loop.
Below is what I am trying to explain above.
Screen 1 shows(position 1 of toggle switch):
on11
toggle switch position switched to off
Screen 1 still shows:
on11
toggle switch position switched to on2
Screen 1 still shows:
on11
The only problem I am facing is this. Is there a way to code so that I don't need to rewrite my code? I've tried interrupts and it does not seem to be working or I may be doing it the wrong way.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
const int pbPin = 10;
const int on1 = 11;
const int on2 = 12;
int bState=0;
int prevbState=0;
int counter=0;
int counterA=0;
int on11=0; //to read on1
int on22=0; //to read on2
void setup()
{
pinMode(10,INPUT); //pushbutton (pbPin)
pinMode(11,INPUT); //toggle-switch (on1)
pinMode(12,INPUT); //toggle-switch (on2)
//attachInterrupt(digitalPinToInterrupt(14), screen, HIGH);
//attachInterrupt(digitalPinToInterrupt(15), screen, HIGH);
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD, was a standard code when I copied from the web
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Hello, world!");
lcd.setCursor(2,1);
lcd.print("Ywrobot Arduino!");
lcd.setCursor(0,2);
lcd.print("Arduino LCM IIC 2004");
lcd.setCursor(2,3);
lcd.print("Power By Ec-yuan!");
Serial.begin(9600);
delay(1000);
lcd.clear();
}
void loop()
{
lcd.setCursor(0,0);
on11=digitalRead(on1);
on22=digitalRead(on2);
bState = digitalRead(pbPin);
Serial.println(counter);
counterA=counter%3;
if(bState!=prevbState)
{
if(bState==HIGH)
{
counter++;
lcd.print(counter);
lcd.setCursor(0,1);
switch(counterA)
{
case 0:
lcd.clear();
lcd.setCursor(0,1);
if(on11==HIGH)
{
lcd.print("on11");
}
else if(on22==HIGH)
{
lcd.print("on22");
}
else
{
lcd.print("off");
}
break;
case 1:
lcd.clear();
lcd.print("Screen 2");
break;
case 2:
lcd.clear();
lcd.print("Screen 3");
break;
default:
lcd.print("0");
break;
}
}
else{}
}
prevbState = bState;
}
Thanks in advance for taking the time to read. Appreciated