Hello, I want to use a 2d array to call up a distance from one room to another. The user inputs his starting point, then ending point, then the program states the distance to get from start to finish. I am having trouble on this 2d array. If anyone can please tell me what to do to have the program display the distance, I would be most grateful. Its the last thing I need for the program to work.
Thanks
Here is a picture of the apartment measurements and grid i made in case it helps.
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();
displayInput(feet);
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 feet)
{
int distance [6] [6] = feet
{
0,0,0,0,0,0 }
,//None
{
0,0,15,18,23,25 }
,//Gerardo Room
{
0,15,0,27,32,34 }
,//Living Room
{
0,18,27,0,13,15 }
,//Kitchen
{
0,23,32,13,0,6 }
,//Bathroom
{
0,25,34,27,6,0 }//Mom Room
}
