Lcd pushbotton toggle multiple menu features in alarm clock code

Hello,

So basically im new to arduino, i want to manipulate my code to add an ultrasonic sensor to it and more features by just a push of a bottom. So i have this alarm clock code and i want to add digitalread features by your help in running one more code of ultrasonic sensor which will change menus for just push buttons so it can be called smart clock. I have all the codes, all u have to help me is manipulate the code in adding a pushbutton to run my other loop of code, toggling between switches basically.

My alarm clock code:

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int starttime;
int activetime;
int prevoustime = 0;

int hours = 0;
int mins = 0;

int ahours = 0;
int amins = 0;

void setup()
{
lcd.begin(16, 2);
lcd.clear();

Serial.begin(9600);

pinMode(13, INPUT);
digitalWrite(13, HIGH);
pinMode(11, INPUT);
digitalWrite(11, HIGH);
pinMode(10, INPUT);
digitalWrite(10, HIGH);

pinMode(8, INPUT);
digitalWrite(8, HIGH);
pinMode(A0, OUTPUT);
digitalWrite(A0, HIGH);

pinMode(9, OUTPUT);

starttime = millis()/1000;
}

void loop()
{
while(digitalRead(8) == LOW)
{
lcd.setCursor(6,1);
lcd.print("Alarm");
lcd.setCursor(6,0);
if(digitalRead(11) == LOW)
{
amins++;
}
else if (digitalRead(10) == LOW)
{
ahours++;
}
lcd.setCursor(6,0);

if(ahours < 10)
{
lcd.print("0");
lcd.print(ahours);
}
else
{
lcd.print(ahours);
}

lcd.print(":");

if (amins < 10)
{
lcd.print("0");
lcd.print(amins);
}
else
{
lcd.print(amins);
}
if(amins > 59)
{
ahours++;
amins = 0;
}
if(ahours > 23)
{
ahours = 0;
}
delay(500);
lcd.clear();
}

if(digitalRead(13) == LOW)
{

lcd.setCursor(5,1);
lcd.print("Set Time");
lcd.setCursor(6,0);
if(digitalRead(11) == LOW)
{
mins++;
}
else if (digitalRead(10) == LOW)
{
hours++;
}

}

activetime = (millis() / 1000) - starttime;
if(prevoustime < (activetime - 59))
{
mins++;
prevoustime = activetime;
}

if(mins > 59)
{
hours++;
mins = 0;
}

if(hours > 23)
{
hours = 0;
}

lcd.setCursor(6,0);

if(hours < 10)
{
lcd.print("0");
lcd.print(hours);
}
else
{
lcd.print(hours);
}

lcd.print(":");

if (mins < 10)
{
lcd.print("0");
lcd.print(mins);
}
else
{
lcd.print(mins);
}

if(ahours == hours && amins == mins && amins != 0)
{
tone(9, 1000, 200);
delay(200);
noTone(9);
delay(200);
}
else
{
delay(300);
}
lcd.clear();

Serial.println(mins);
Serial.println(hours);
Serial.println("");
Serial.println(amins);
Serial.println(ahours);
Serial.println("");
Serial.println(activetime);
Serial.println(prevoustime);
Serial.println(starttime);
Serial.println("");
}

Ultrasonic sensor code:
THIS IS THE OTHER CODE, SO IF I PLAY THIS ON My lcd SCREEN, IT WILL BE BY PRESSING A PUSHBUTTON AND running it.

#include <LiquidCrystal.h> //Load Liquid Crystal Library
LiquidCrystal LCD(10, 9, 5, 4, 3, 2); //Create Liquid Crystal Object called LCD

int trigPin=13; //Sensor Trip pin connected to Arduino pin 13
int echoPin=11; //Sensor Echo pin connected to Arduino pin 11
int myCounter=0; //declare your variable myCounter and set to 0
int servoControlPin=6; //Servo control line is connected to pin 6
float pingTime; //time for ping to travel from sensor to target and return
float targetDistance; //Distance to Target in inches
float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees.

void setup() {

Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
LCD.begin(16,2); //Tell Arduino to start your 16 column 2 row LCD
LCD.setCursor(0,0); //Set LCD cursor to upper left corner, column 0, row 0
LCD.print("Target Distance:"); //Print Message on First Row
}

void loop() {

digitalWrite(trigPin, LOW); //Set trigger pin low
delayMicroseconds(2000); //Let signal settle
digitalWrite(trigPin, HIGH); //Set trigPin high
delayMicroseconds(15); //Delay in high state
digitalWrite(trigPin, LOW); //ping has now been sent
delayMicroseconds(10); //Delay in high state

pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds
pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second)
pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour)
targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour
targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance.
targetDistance= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile)

LCD.setCursor(0,1); //Set cursor to first column of second row
LCD.print(" "); //Print blanks to clear the row
LCD.setCursor(0,1); //Set Cursor again to first column of second row
LCD.print(targetDistance); //Print measured distance
LCD.print(" inches"); //Print your units.
delay(250); //pause to let things settle

}

So please help me manipulate the code by adding a push button and running the other ultrasonic code, and pressing it for it to return to alarm clock. Im very nice to arduino so i need lots of specific help, thanks

Cricboyy12:
Hello,

So basically im new to arduino, i want to manipulate my code to add an ultrasonic sensor to it and more features by just a push of a bottom. So i have this alarm clock code and i want to add digitalread features by your help in running one more code of ultrasonic sensor which will change menus for just push buttons so it can be called smart clock. I have all the codes, all u have to help me is manipulate the code in adding a pushbutton to run my other loop of code, toggling between switches basically.

...

And all u have to read is the post near the top, How to use this forum - please read, then modify your post so we can at least read it. I doubt your code actually has smilies in it:

"While(digitalRead(8) == LOW)"

my code and my information is there, that is enough, u can read my 2 codes, and tell me how to use a switch button to play them. If I just change cursor, the loop while keep running, so in some way i need to add a pushbutton to seperate the loops. Please help on that. thanks, and ill change my code, there was an error

i urge you to look at a state machine approach:

pseudo code:

enum ScreenState{
  MAIN_SCREEN, 
  SCREEN_ONE,
  SCREEN_TWO
};

ScreenState screenState = MAIN_SCREEN;

bool buttonPressed;   //just to compile example
void setup() 
{
  // put your setup code here, to run once:
}

void loop() 
{
  switch (screenState)
  {
    case MAIN_SCREEN:
    //do main screen stuff
    break;
    case SCREEN_ONE:
    // do screen one stuff
    break;
    case SCREEN_TWO:
    //do screen two stuff
    break;
  }
  
  if (buttonPressed && screenState == MAIN_SCREEN)
  {
    screenState = SCREEN_ONE;
  }
}