Hello, I'm learning how to create a program that asks the user what his/her start point and end point are to give them the distance needed to get there. I am having a hard time figuring out how to make it so that if they pick values example; 5 (Mom room start) and 4(Bathroom end), it will only print 6 feet and nothing else(Start program over). I was searching online for 2D array instructions but couldn't pull it off. I went on and tried this way of doing the program which doesn't seem to be the most practical.
If anyone can please show me an example or help me with a good link of how to do the 2d array I would be most thankful. If anything I would like to learn how to continue to use the if statements in the void distance loop; so that only distance prints
Thank you for reading
Ps: I added a picture of the house and table of distances in a grid to use for the array
// Room Distance Program
void setup()
{
Serial.begin(9600);
}
void loop()
{
int startPoint=NULL;
int endPoint=NULL;
//Input selection for start point.
Serial.print("Select the starting point\n");
menu();
getInput(startPoint);
Serial.print("Your starting point is: ");
displayInput(startPoint);
//Input selection for end point.
Serial.print("Select the ending point\n");
menu();
getInput(endPoint);
Serial.print("Your ending point is: ");
displayInput(endPoint);
Serial.print("Your Distance is:");
distance();
Serial.print("\n\n\n");
}
void welcome()
{
Serial.print("Room Distance Program\n");
}
void menu()
{
delay(3000);
Serial.print(" 1 - Gerardo's Room\n");
Serial.print(" 2 - Living Room\n");
Serial.print(" 3 - Kitchen\n");
Serial.print(" 4 - Bathroom\n");
Serial.print(" 5 - Mom's room\n");
Serial.print("\n\n\n");
}
void getInput(int& number)
{
//This is a loop of to input the value of the whatever is passed into this function
//This assumes there are only 5 rooms available, and will only accept a number between
// 1 and 5.
do
{
delay(500);
if(Serial.available())
{
//For some reason 1=49, so we subtract by 48 to give the correct value.
number=Serial.read()-48;
}
if((number<0) || (number>5))
{
//This lets the user know that they entered a number outside the acceptable range.
//It also resets number to null, preventing it from looping constantly.
Serial.print(number);
Serial.print("\nMust be between 1-5\n");
number=NULL;
}
} while ((number<0) || (number>5) || (number==NULL));
}
void displayInput(int selection)
{
switch (selection) {
case 1:
Serial.print("Gerardo's Room\n");
break;
case 2:
Serial.print("Living Room\n");
break;
case 3:
Serial.print("Kitchen\n");
break;
case 4:
Serial.print("Bathroom\n");
break;
case 5:
Serial.print("Mom's room\n");
break;
}
}
void distance()
{
int startPoint=NULL;
int endPoint=NULL;
if((startPoint),(endPoint)=(5),(4))
{Serial.print("6 feet");}
else if((startPoint),(endPoint)=(3),(4))
{Serial.print("13 feet");}
}
