Arduino void loop only running once - arduino

The void loop is only running once. Please help. I am inexperienced with Arduino, so any help is much-appreciated. When I open the serial monitor I only see zero once. This code was working earlier today. Sorry if the solution is very simple. Some code has been removed to fit within post restrictions such as ints and ultrasonic declarations and includes.
void setup() {
Serial.begin(9600); //sets up Serial monitor
InfraredSeeker::Initialize();
}
void loop() {
if(u = 1) {
ang = uang;
} else{
ang = irang;
}
Serial.println(uang);
m1 = 200*cos((30-ang)*0.0174533);
m2 = 200*cos((270-ang)*0.0174533);
m3 = 200*cos((150-ang)*0.0174533);
InfraredResult InfraredBall = InfraredSeeker::ReadAC();
switch (InfraredBall.Direction) {
case 1:
irang = 160;
break;
case 2:
irang = 120;
break;
case 3:
irang = 90;
break;
case 4:
irang = 50;
break;
case 5:
irang = 0;
break;
case 6:
irang = 310;
break;
case 7:
irang = 270;
break;
case 8:
irang = 240;
break;
case 9:
irang = 200;
break;
case 0:
irang = 160;
break;
default:
break;
}
lultra = uleft.Ranging(CM);
rultra = urigth.Ranging(CM);
fultra = ufront.Ranging(CM);
if (lultra < 15){
u = 1;
uang = 270;
}
if (rultra < 15) {
u = 1;
uang = 90;
}
if (fultra < 15) {
u = 1;
uang = 180;
}
}

Related

Trouble setting ADC to fully respond

I have made a simple program that controls a 10-LED bar graph and the speed in which the LEDs change is supposed to be controlled by a 10K POT. I am using 16F88 (and CSS Compiler/Mplab v8.91) for this simple Program, but the 10k POT is not responding to a full turn of the POT. Only toward in the Last 5% of MAX on the POT, does the speed change a few steps. From Minimum to 95% (of MAX) there is no change in the speed of output of the LEDs.
All other parts of the code with. Just need to get this POT figured out.
Thanks in Advance!
#include <16f88.h>
#device ADC=10
#device *=16
#ZERO_RAM
//////// Fuses: LP,XT,HS,EC_IO,NOWDT,WDT,NOPUT,PUT,MCLR,NOMCLR,BROWNOUT
//////// Fuses: NOBROWNOUT,LVP,NOLVP,CPD,NOCPD,WRT,NOWRT,DEBUG,NODEBUG,CCPB0
//////// Fuses: CCPB3,PROTECT,NOPROTECT,INTRC,INTRC_IO,RC,RC_IO,FCMEN
//////// Fuses: NOFCMEN,IESO,NOIESO
#fuses INTRC_IO,NOWDT,PUT, NOMCLR, PROTECT, NOBROWNOUT, NOLVP, NOWRT, NOCPD
#use delay(clock=4000000)
#use fast_io(A)
#use fast_io(B)
#byte PORTA = 0x5
#byte PORTB = 0x6
/**********************************************************************/
/* PIC interface */
/**********************************************************************/
#bit LED1 = PORTA.1 //Output
#bit LED2 = PORTA.2
#bit LED3 = PORTA.3
#bit LED4 = PORTA.4
#bit LED5 = PORTA.6
#bit LED6 = PORTA.7
#bit LED7 = PORTB.0
#bit LED8 = PORTB.1
#bit LED9 = PORTB.2
#bit LED10 = PORTB.3
#bit LED_GREEN = PORTB.4
#bit SP_OUT = PORTB.5 //Short Pulse
/**************************************************************************
* Global Variables
**************************************************************************/
int speed, state, adc0;
unsigned int16 t,t1, t2,t3, t4;
short flag, flag1,flag2;
/**************************************************************************
* Speed
**************************************************************************/
void f_speed(void) {
SET_ADC_CHANNEL(0);
delay_us(100);
adc0 = READ_ADC();
if(adc0<1) adc0=1;
if(adc0>0 && adc0<=32)
t2=2800; //280ms
if(adc0>32 && adc0<=64)
t2=2450;; //245ms
if(adc0>64 && adc0<=96)
t2=2100; //210ms
if(adc0>96 && adc0<=128)
t2=1750;
if(adc0>128 && adc0<=160)
t2=1400;
if(adc0>160 && adc0<=192)
t2=1050;
if(adc0>192 && adc0<=224)
t2=700;
if(adc0>224 && adc0<=256)
t2=350; //35ms
}
/**************************************************************************
* Mode 1 basic Scan
**************************************************************************/
void f_mode_1(void) {
// SPM_BASIC_SCAN
if(flag1){
flag1=0;
flag2=1;
t=0;
}
if(flag2){
if(t>10){
SP_OUT=1;
flag2=0;
t4=0;
}
}
switch(state){
case 1:
// PORTA = 0b00000001; // SCAN STARTS
LED1=1;
if(t3>t2){
t3=0;
state=2;
LED1=0;
}
break;
case 2:
LED2=1;
if(t3>t2){
t3=0;
state=3;
LED2=0;
}
break;
case 3:
LED3=1;
if(t3>t2){
t3=0;
state=4;
LED3=0;
}
break;
case 4:
LED4=1;
if(t3>t2){
t3=0;
state=5;
LED4=0;
}
break;
case 5:
LED5=1;
if(t3>t2){
t3=0;
state=6;
LED5=0;
}
break;
case 6:
LED6=1;
if(t3>t2){
t3=0;
state=7;
LED6=0;
}
break;
case 7:
LED7=1;
if(t3>t2){
t3=0;
state=8;
LED7=0;
}
break;
case 8:
LED8=1;
if(t3>t2){
t3=0;
state=9;
LED8=0;
}
break;
case 9:
LED9=1;
if(t3>t2){
t3=0;
state=10;
LED9=0;
}
break;
case 10:
LED10=1;
if(t3>t2){
t3=0;
state=11;
LED10=0;
}
break;
case 11:
LED9=1;
if(t3>t2){
t3=0;
state=12;
LED9=0;
}
break;
case 12:
LED8=1;
if(t3>t2){
t3=0;
state=13;
LED8=0;
}
break;
case 13:
LED7=1;
if(t3>t2){
t3=0;
state=14;
LED7=0;
}
break;
case 14:
LED6=1;
if(t3>t2){
t3=0;
state=15;
LED6=0;
}
break;
case 15:
LED5=1;
if(t3>t2){
t3=0;
state=16;
LED5=0;
}
break;
case 16:
LED4=1;
if(t3>t2){
t3=0;
state=17;
LED4=0;
}
break;
case 17:
LED3=1;
if(t3>t2){
t3=0;
state=18;
LED3=0;
}
break;
case 18:
LED2=1;
if(t3>t2){
t3=0;
state=1;
LED2=0;
}
break;
}
}
/**************************************************************************
* Clear_LED
**************************************************************************/
void Clear_LED(void){
LED1=0;
LED2=0;
LED3=0;
LED4=0;
LED5=0;
LED6=0;
LED7=0;
LED8=0;
LED9=0;
LED10=0;
}
/*************************************************************************
* timer0 Interrupts 100useg
**************************************************************************/
#INT_TIMER0
void TIMER0_isr()
{
set_timer0(156);
t3++; // Speed timer
t4++; //Short pulse timer
if(t4>5000) {
t4=0; //500ms
SP_OUT = 0;
}
}
/*************************************************************************
* timer1 Interrupts 100ms
**************************************************************************/
#INT_TIMER1
void TIMER1_isr()
{
set_timer1(53036);
t++;
t1++;
}
/**************************************************************************
* interruption for change RB4-RB7
**************************************************************************/
#int_rb
void detect_rb_change(void) {
disable_interrupts(INT_RB);
int current;
static int last=0;
current=PORTB;
if ((!bit_test(current,7))&&(bit_test(last,7))) {
flag=!flag;
if(flag){ //ON
LED_GREEN=1;
state=1;
t3=0;
f_speed();
flag1=1;
} else{ //OFF
LED_GREEN=0;
}
}
last=current;
enable_interrupts(INT_RB);
}
/**************************************************************************
* Main Progran
**************************************************************************/
void main()
{
set_tris_a(0x00);
set_tris_b(0x80);
setup_adc_ports(sAN0);
setup_adc(VSS_VDD | ADC_CLOCK_DIV_8);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_0(T0_INTERNAL|T0_DIV_0);
Clear_LED();
LED_GREEN=0;
SP_OUT=0;
flag=false;
state=1;
f_speed();
t3=0;
t1=0;
set_timer1(53036); //Interrupts 100ms
set_timer0(156); //Interrupts 100us
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RB);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
if(flag){ //ON
if(t1>10){
t1=0;
f_speed();
}
f_mode_1();
} else{
Clear_LED();
}
}
}

Bop it Arduino Circuit Playground

I'm trying to create a bop it game with 2 players where:
Green LED – Gyro Sensor (Z-axis)
Red LED – Temperature sensor
Blue LED – Sound sensor
Yellow LED – Light Sensor
However when I press the left button to start the game, only the yellow light above GND goes on and doesn't respond to anything, what can be wrong with my code?
long randNumber;
int temp;
int val;
int number;
int score;
boolean state;
int light;
uint8_t pixeln = 0;
int sound;
int shake;
unsigned long lastmillis = 0;
boolean game;
const byte numPins = 7;
byte pins[] = {0, 1, 2, 3, 4, 5, 6, 7};
int player1 = 0;
int player2 = 0;
#include "Adafruit_CircuitPlayground.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
//timer();
}
void loop() {
if (digitalRead(4) == HIGH && digitalRead(19) == LOW){
Serial.println("Button Pressed");
lastmillis = millis();
for (int i=0; i < 10; i ){
randNumber = random(4);
//Serial.println("Loop");
switch (randNumber){
//Serial.println("Switch");
case 0:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 255,0,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
temp = analogRead(A0);
//Serial.println(temp);
while (state == true) {
val = analogRead(A0);
number = val - temp;
if (number > 10){
score ;
state = false;
//Serial.println("Temperature");
delay(500);
}
}
break;
case 1:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 127,127,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
light = analogRead(A5);
//Serial.println(light);
while (state == true) {
val = analogRead(A5);
number = light - val;
if (number > 20){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 2:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,0,255);
CircuitPlayground.strip.show();
}
pixeln = 0;
state = true;
sound = analogRead(A4);
//Serial.println(sound);
while (state == true) {
val = analogRead(A4);
number = val - sound;
if (number > 50){
score ;
state = false;
//Serial.println(number);
delay(500);
}
}
break;
case 3:
while (pixeln < 10){
CircuitPlayground.setPixelColor(pixeln , 0,255,0);
CircuitPlayground.strip.show();
}
pixeln = 0;
val = CircuitPlayground.motionZ();
//Serial.println(val);
state = true;
while (state == true) {
shake = CircuitPlayground.motionZ();
number = val - shake;
//Serial.println(number);
if (number > 15 or number < -15){
score ;
state = false;
//Serial.println("shake");
delay(500);
}
}
break;
case 4:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(0, 0,255,0);
CircuitPlayground.setPixelColor(1, 0,255,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 5:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(3, 255,0,0);
CircuitPlayground.setPixelColor(4, 255,0,0);
CircuitPlayground.strip.show();
delay(3000);
break;
case 6:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(5, 127,0,255);
CircuitPlayground.setPixelColor(6, 127,0,255);
CircuitPlayground.strip.show();
delay(3000);
break;
case 7:
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(8, 255,128,0);
CircuitPlayground.setPixelColor(9, 255,128,0);
CircuitPlayground.strip.show();
delay(3000);
}
}
if (CircuitPlayground.slideSwitch()) {
player1 = (millis() - lastmillis) / 1000;
Serial.println(player1);
CircuitPlayground.clearPixels();
byte num = player1;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}
} else {
player2 = (millis() - lastmillis) /1000;
Serial.println(player2);
CircuitPlayground.clearPixels();
byte num = player2;
for (byte i=0; i < numPins; i ){
byte state = bitRead(num, i);
if (state == 1){
CircuitPlayground.setPixelColor(pins[i], 255,255,255);
CircuitPlayground.strip.show();
}
}}
CircuitPlayground.playTone(330, 500);
}
while (digitalRead(19) == HIGH && digitalRead(4) == LOW){
if (player1 < player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 5; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else if (player1 > player2){
CircuitPlayground.clearPixels();
delay(250);
for (int i =5; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
else {
CircuitPlayground.clearPixels();
delay(250);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, 255,255,255);
CircuitPlayground.strip.show();
}
delay(250);
CircuitPlayground.clearPixels();
}
}
if (digitalRead(19) == HIGH && digitalRead(4) == HIGH){
player1 = 0;
player2 = 0;
for (int a =0; a < 10; a ){
CircuitPlayground.clearPixels();
delay(100);
for (int i =0; i < 10; i ){
CircuitPlayground.setPixelColor(i, CircuitPlayground.colorWheel(25 * i));
CircuitPlayground.strip.show();
}
delay(100);
CircuitPlayground.clearPixels();
}
}
}
visual after pressing left button:

Arduino Uno - minimize memory usage

I have a Weather Meter, and I am able to read data from it.
My problem occurs, when I try to use the library UnoWiFiDevEd.h send the data to store the information on the internet.
Arduino tells following:
Global variables use 1648 bytes (80%) of dynamic memory, leaving 400 bytes for local variables.
Maximum is 2048 bytes. Low memory available, stability problems may occur.
I have read, that another means, that the Uno WiFi library itself leads almost 50% Dynamic Memory usage.
Do anyone have an idea, how I can minimize memory usage?
Thank you in advance
#include <UnoWiFiDevEd.h>
//temporary rain
float calcTemp1;
int calcTemp2;
//Wind vane
const float table[16] = {3.84, 1.98, 2.25, 0.41, 0.45, 0.32, 0.90, 0.62, 1.40, 1.19, 3.08, 2.93, 4.62, 4.32, 4.78, 3.43}; //charecter 13 is not correct, but is changed due to failure in windvane
char buffer[20];
//Anometer - windpower
volatile unsigned int windRotation = 0;
//Used for timing
float windTimer = 0;
int angle = 0;
//Rain gauge
float RainMeasurement = 0;
unsigned long LastRainReset = 0;
void setup() {
Serial.begin(9600);
Ciao.begin(); // CIAO INIT
Serial.write(13);
delay(2000);
Serial.println("Initialiserer...");
initWind();
initRain();
Serial.println();
delay(1000);
}
void loop() {
doRequest();
Serial.println();
delay(30000);
}
//Gets data about wind
void getWindData(void)
{
Serial.print("Vindretning: ");
Serial.println(printWindDirection(36));
unsigned long WindReading;
WindReading = Vind_GetHastighed();
sprintf(buffer, "Hastighed: %d m/s ", WindReading);
Serial.println(buffer);
}
//Gets data about rain
void getRainData(void)
{
if (LastRainReset+86400000 < millis()) { // LastRainReset > 24 timer
RainMeasurement = 0;
LastRainReset = millis();
}
calcTemp1 = (float)RainMeasurement;
calcTemp2 = (calcTemp1 - (int)calcTemp1) * 100;
sprintf(buffer, "%0d.%d", (int)calcTemp1, calcTemp2);
Serial.print("Nedb\xF8r: ");
Serial.print(buffer);
Serial.println(" mm");
}
void doRequest(){
String resource = "upload.php?key=secretKey";
resource += "&windDir=";
resource += String(angle);
getWindData();
resource += "&windSpeed=";
resource += String(Vind_GetHastighed());
resource += "&rainAmount=";
getRainData();
resource += String(buffer);
Serial.println(resource);
CiaoData data = Ciao.write("rest", "http://example.com/", resource, "GET");
if (!data.isEmpty()){
Ciao.println( "Link: " + String (resource) );
Ciao.println( "State: " + String (data.get(1)) );
Ciao.println( "Response: " + String (data.get(2)) );
Ciao.println();
}
else{
Ciao.println ("Write Error");
Ciao.println();
}
}
// Initializing processes
void initWind(void)
{
pinMode(3, INPUT);
attachInterrupt(1, windSpeed, RISING);
windTimer=millis();//start timing
Serial.println("* Vindretning");
Serial.println("* Vindhastighed");
}
//initializing rainmeasure
void initRain(void)
{
attachInterrupt(0, Rain_Measure, RISING); //analog port 0
LastRainReset = millis();
Serial.println("* Regn");
}
//Counts amount of rain
void Rain_Measure(void)
{
volatile byte hit = 1;
if (hit == 1) {
hit = 2;
} else if (hit == 2) {
hit = 3;
} else if (hit == 3) {
RainMeasurement = RainMeasurement + 0.2794;
hit = 1;
}
}
//Prints winddirection
String printWindDirection(byte y)
{
// read the analog input into a variable:
String windDir = "unknown";
float voltage = analogRead(0)/204.6;
for (int i = 0; i < 16; i++) {
if (voltage <= table[i]+0.03 && voltage >= table[i]-0.03) {
angle = i;
break;
}
}
//Serial.println(angle); //print the result
switch (angle) {
case 0:
windDir = "N";
break;
case 1:
windDir = "NNE";
break;
case 2:
windDir = "NE";
break;
case 3:
windDir = "E";
break;
case 4:
windDir = "E";
break;
case 5:
windDir = "E";
break;
case 6:
windDir = "SE";
break;
case 7:
windDir = "SSE";
break;
case 8:
windDir = "S";
break;
case 9:
windDir = "SW";
break;
case 10:
windDir = "SW";
break;
case 11:
windDir = "WSW";
break;
case 12:
windDir = "W";
break;
case 13:
windDir = "WNW";
break;
case 14:
windDir = "NW";
break;
case 15:
windDir = "NNW";
break;
default:
break;
}
return windDir;
}
//Prints windspeed
int Vind_GetHastighed()
{
/*
The cup-type anemometer measures wind speed by closing a contact as
a magnet moves past a switch. A wind speed of 1.492 MPH (2.4 km/h)
causes the switch to close once per second.
*/
//Check using Interrupt
float windDtime = millis()-windTimer;
windTimer = millis();
windDtime = windDtime/1000;
float windSpeed = windRotation/windDtime;//rotation per second
windRotation = 0;
windSpeed = windSpeed*2/3;//1 rotation per second equals 2.4 km/h = 2/3 m/s
return int(windSpeed);
}
void windSpeed()
{
windRotation++;
}
Arduino has the F() macro for constant strings to simply indicate that the string should be used from the 'ROM' flash memory of Harvard architecture CPU and not loaded into 'dynamic memory' (RAM). By wrapping constant strings into F macro you reduce the usage of RAM.

optimize the conversion function (string to byte)

Hy community, I'm writing a program with arduino. I have write a function that convert string in byte but it it occupies so much memory. My question is if there is a function that I can use and it does not occupy so much memory on my arduino.
I post the code below.
byte strConv(String str)
{
byte a = 0;
char n = 2;
char g = 3;
char stringa1;
char stringa2;
char m;
stringa1 = str.charAt(n);
stringa2 = str.charAt(g);
m = costanti (stringa1);
if (m >= '0' && m <= '9' || m >= 'A' && m <= 'F') {
a += m;
} else {
a += m;
}
a = a << 4;
m = 0;
m = costanti (stringa2);
if (m >= '0' && m <= '9' || m >= 'A' && m <= 'F') {
a += m;
} else {
a += m;
}
return a;
}
and the second function:
char costanti(char n) {
char num;
switch (n) {
case '0':
num = 0;
break;
case '1':
num = 1;
break;
case '2':
num = 2;
break;
case '3':
num = 3;
break;
case '4':
num = 4;
break;
case '5':
num = 5;
break;
case '6':
num = 6;
break;
case '7':
num = 7;
break;
case '8':
num = 8;
break;
case '9':
num = 9;
break;
case 'A':
num = 10;
break;
case 'B':
num = 11;
break;
case 'C':
num = 12;
break;
case 'D':
num = 13;
break;
case 'E':
num = 14;
break;
case 'F':
num = 15;
break;
}
return num;
}
Can I reduce this two functions in a new optimized function?
As far as I understand you want something like this:
char strConv(String str)
{
if(str.length() > 2)
return -1; // return something which makes sense to you for "invalid"
return static_cast<char>(strtol(str.c_str(), NULL, 16));
}
String myString(26, HEX); // creates the string "1A" for you
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("String: \""+myString+'"');
char val = strConv(myString); //gives you the 26 again
Serial.print("int: ");
Serial.println(static_cast<int>(val));
delay(1000);
}
This prompts me the following:
String: "1a"
int: 26
String: "1a"
int: 26
...

How can I get out of the loop?

I'd like to get out of the loop in my Arduino project. Currently I am programming a digital watch, everything works just fine but I wanted to add options menu by clicking a button, but after clicking it nothing pops up even if I have something inside the code. Take a look at it. I don't know how to write it better. If you have some ideas, please you could rewrite some parts of the code and explain why you did so. Thanks forwardly.
Please do not mind s = s + 1, I wanted it like that.
#include "LiquidCrystal.h"
#include <EEPROM.h>
LiquidCrystal lcd(12,11,5,4,3,2);
int h = 0;
int m = 0;
int s = 0;
int right = 8;
int left = 9;
int buttonStateLeft = 0;
String when;
uint8_t EEPROMaddress_sec = 1;
uint8_t EEPROMaddress_min = 2;
uint8_t EEPROMaddress_hour = 3;
bool clockShown = true;
bool menuShown = false;
void setup()
{
lcd.begin(16,2);
pinMode(right, INPUT);
pinMode(left, INPUT);
}
void loop()
{
if(menuShown)
{
lcd.setCursor(0,0);
lcd.print("jozo je kkt");
delay(200);
}
if(clockShown) {
lcd.setCursor(0,0);
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}
s = EEPROM.read(EEPROMaddress_sec);
m = EEPROM.read(EEPROMaddress_min);
h = EEPROM.read(EEPROMaddress_hour);
s = s + 1;
if(h > 12)
when = "PM";
if(h < 12)
when = "AM";
if(h == 12)
when = "PM";
lcd.print("Cas: ");
if(h<10)lcd.print("0");
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);
lcd.print(" ");
lcd.print(when);
if(s == 60)
{
s = 0;
m = m+1;
}
if(m == 60)
{
s = 0;
m = 0;
h = h+1;
}
if(h == 24)
{
m = 0;
s = 0;
h = 0;
}
EEPROM.write(EEPROMaddress_sec, s);
EEPROM.write(EEPROMaddress_min, m);
EEPROM.write(EEPROMaddress_hour, h);
delay(1000);
}
}
In order to do that you will have to use Interrupts, note that you must connect your button to an interrupt pin (not every pin is an interrupt pin) you can google "what are the interrupt pins of 'your_card_name' ", the code would have to change, you can follow these :
In the setup function replace :
pinMode(left, INPUT);
by :
attachInterrupt(digitalPinToInterrupt(left), switchMode, RISING);
add this function before setup(){...}
int lastPressTime=millis();
void switchMode(){ // function called when the button is pressed
if((millis()-lastPressTime)>60){ // for debouncing
clockShown = false;
menuShown = true;
lcd.clear();
lastPressTime=millis();
}
}
and remove this part from your code : (the one in the loop() function)
buttonStateLeft = digitalRead(left);
if(buttonStateLeft == HIGH)
{
clockShown = false;
menuShown = true;
lcd.clear();
}

Resources