# Distance Program

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");
getInput(startPoint);
displayInput(startPoint);

//Input selection for end point.
Serial.print("Select the ending point\n");
getInput(endPoint);
displayInput(endPoint);
distance();
displayInput(feet);
Serial.print("\n\n\n");
}

void welcome()
{
Serial.print("Room Distance Program\n");
}
{
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.
}

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
}
``````

Did you mean you want to enter starting room instead of starting distance?

What is the problem with your current code? Provide a couple of sample inputs and responses.

elevatedsounds: Hello, I want to use a 2d array to call up a distance from one room to another.

I think you want your array function to look something like this:

``````int distance(int room1,int room2)
{
int distance [6] [6] =
{
{ 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
};
return distance[ room1 ] [ room2 ];
}
``````

Because you need to look up both dimensions, you have to pass in both entries, so room1 & room2. Hope this helps.

Sorry, I did mean starting point. I need to get the distance printed after both start and end points are given. Don’t know how to do it though… I’m stuck. I’m very new to programming and thought that this idea of creating this program would show me some stuff.
For example: if the input was Living room(1) and the ending point was kitchen(3), then the output should be 27 feet.

What do I need to put inside the distance input parenthesis?

elevatedsounds: Sorry, I did mean starting point. I need to get the distance printed after both start and end points are given. Don't know how to do it though... I'm stuck. I'm very new to programming and thought that this idea of creating this program would show me some stuff. For example: if the input was Living room(1) and the ending point was kitchen(3), then the output should be 27 feet.

What did you get instead of 27?

By the way, I think you got a good table. It's symmetric along the top-left / bottom/right diagonal, which makes sense. You want to ask and obtain two numbers, say 2, and 3, (you did a good table but you can't count. It's 2nd column and 3th row, not 1,3) then you take the value of array element distance[2][3], which is supposed to be 27, except for two things. 1) you should get rid of the 0 row and 0 column so there is only 0 on diagonal. It makes zero sense. 2) you will index your array 0-based (sorry for 0 again) so instead of distance [2][3], you do distance [1][2].

Here is a start, correct array, after David cleaned and aligned:

``````int distance [5] [5] =
{
{ 0,15,18,23,25  }, //Gerardo Room
{ 15,0,27,32,34  }, //Living Room
{ 18,27,0,13,15  },//Kitchen
{ 23,32,13,0,6  },//Bathroom
{ 25,34,27,6,0  } //Mom Room
};
``````

Your input code doesn't look right, but you didn't discuss whether it worked or not so it is your turn. Lecture is dismissed.

Nothing happens because I still don't know what to put inside the display input parenthesis .The code is working fine up until I need it to display the distance. I don't know how to make that work.I made the first row and column 0's because I thought arrays counted the first row as 0 and the second as 1(same with column).

elevatedsounds: I made the first row and column 0's because I thought arrays counted the first row as 0 and the second as 1(same with column).

That's true - your first row would be start at index 0 not 1 - but in C that means you use a lot of extra memory (about 44% here), so we all get used to subtracting one when we use arrays like you're doing: [x-1].

As for your formula, do I understand it right: You say "The user inputs his starting distance, then ending distance, then the program states the distance to get from start to finish." So for example, I'd say "I'm at Mom's room (#2), and want to get to the living room (#5), how far is it?" and it would respond '34' (4+9+10+7+4)? If so, then the table/function works - you'd ask 'I am in room 2, and I want to get to room 5':

``````int distanceBetween=distance(2,5); // returns 34
``````

I hope I'm understanding correctly...

That's exactly how to describe it. You can try it on your arduino and see that the only thing left is to display that distance. Is the table function the same as an 2d array? There are so many things I don't know about programming

elevatedsounds:
Is the table function the same as an 2d array?

Yes - the table is a 2d array, which was a good choice for this information.

I went through the code and fixed it somewhat - here’s a working version:

``````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");
getInput(startPoint);
displayInput(startPoint);

//Input selection for end point.
Serial.print("Select the ending point\n");
getInput(endPoint);
displayInput(endPoint);
int feet=distance(startPoint,endPoint); // <--
//  displayInput(feet); <--
Serial.print(feet); // <--
Serial.print("\n\n\n");
}

void welcome()
{
Serial.print("Room Distance Program\n");
}
{
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.
}

//    if((number<0) || (number>5)) <--
if((number<0)) // <-- ignore control characters
{  // <-- ignore control characters
}  // <-- ignore control characters
else if(number>5) // <-- above 5 - possible error
{
//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;
}
}

int distance(int room1,int room2)
{
int distance [6] [6] =
{
{ 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
};
return distance[ room1 ] [ room2 ];
}
``````

Besides the distance function I showed earlier, you only needed two tweaks:

1. when displaying the distance, you needed to pass it the end and start now, and print the distance, rather than use displayInput (which showed rooms, not feet):
``````int feet=distance(startPoint,endPoint); // <--
//  displayInput(feet); <--
Serial.print(feet); // <--
``````
1. you were getting extra entries in the entry form because you need to press return after each entry, and these were causing problems, so I broke your test if() into two parts, one for <0 and one for >5:
``````    if((number<0)) // <-- ignore control characters
{  // <-- ignore control characters
}  // <-- ignore control characters
else if(number>5) // <-- above 5 - possible error
``````

If an entry is <0, it’s likely a control char you can ignore, so no need for the warning message; if over 5, then maybe they entered something bad, so the warning is worthwhile. It wasn’t strictly necessary, but looks cleaner when running it. Hope this helps.

David, Thank you very much. You just opened many doors for me. It's 2:10am and I have the biggest smile on my face. Thanks for your time and knowledge. Thanks for helping me out. I'm very happy!!! lol Thanks again!!!! Wow I can't believe it works!!! Your the best

Truth Passion Peace and Love

Hello, I made a program with the help of the Arduino forum members. I now want to have words printed instead of distances. What Do I need to do in order for this to work?

this is the distance program

``````//Distance Program
void setup()
{
Serial.begin(9600);
Serial.print("Distance Program\n\n\n\n\n");
delay(3000);
}
void loop()
{
int startPoint=NULL;
int endPoint=NULL;

Serial.print("Select the Starting Location(enter # value) \n");
getInput(startPoint);
displayInput(startPoint);

Serial.print("Select the Ending Location(enter # value)\n");
getInput(endPoint);
displayInput(endPoint);

int feet=distance(startPoint,endPoint);
Serial.print(feet);
Serial.print("feet");
Serial.print("\n\n\n\n\n\n\n");
}

{
delay(2000);
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 - Marta's room\n");
Serial.print("6 - Laurice's House\n");
Serial.print("\n\n\n");
}

void getInput(int& number)
{
do
{
delay(500);
if(Serial.available())
{
}
if((number<0))
{
}
else if(number>6)
{
Serial.print(number);
Serial.print("\n# Must Be from 1-6\n");
number=NULL;
}
}
while ((number<0) || (number>6) || (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("Marta's room\n");
break;
case 6:
Serial.print("Laurice's House\n");
break;
}
}

int distance(int room1,int room2)
{
int distance[7][7]=
{
{0,0,0,0,0,0,0},
{0,0,15,18,23,25,317441},
{0,15,0,27,32,34,17424},
{0,18,27,0,13,15,17451},
{0,23,32,13,0,6,17456},
{0,25,34,27,6,0,17458},
{0,17441,17424,17451,17456,17458,0},

};
return distance [room1][room2];
}
``````

This is what I would like it to look like with the words

``````//Distance Program
void setup()
{
Serial.begin(9600);
Serial.print("Distance Program\n\n\n\n\n");
delay(3000);
}
void loop()
{
int startPoint=NULL;
int endPoint=NULL;

Serial.print("Select the Starting Location(enter # value) \n");
getInput(startPoint);
displayInput(startPoint);

Serial.print("Select the Ending Location(enter # value)\n");
getInput(endPoint);
displayInput(endPoint);

char feet=distance(startPoint,endPoint);
Serial.print(feet);
Serial.print("feet");
Serial.print("\n\n\n\n\n\n\n");
}

{
delay(2000);
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 - Marta's room\n");
Serial.print("6 - Laurice's House\n");
Serial.print("\n\n\n");
}

void getInput(int& number)
{
do
{
delay(500);
if(Serial.available())
{
}
if((number<0))
{
}
else if(number>6)
{
Serial.print(number);
Serial.print("\n# Must Be from 1-6\n");
number=NULL;
}
}
while ((number<0) || (number>6) || (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("Marta's room\n");
break;
case 6:
Serial.print("Laurice's House\n");
break;
}
}

char distance(char room1,char room2)
{
char distance[7][7]=
{
{0,0,0,0,0,0,0},
{0,saturn,mercury,urunus,sun,earth,pluto},
{0,notes,delete,sum,ligit,monkey,money},
{0,nail,nail,destroy,laugh,rich,plant},
{0,bed,clock,drapes,fallacies,click,door},
{0,elephant,table,mouse,star,ratio,magic},

};
return distance [room1][room2];
}
``````

I put this code up but I only get one letter back which is not related to the word.

``````//Distance Program
void setup()
{
Serial.begin(9600);
Serial.print("Distance Program\n\n\n\n\n");
delay(3000);
}
void loop()
{
int startPoint=NULL;
int endPoint=NULL;

Serial.print("Select the Starting Location(enter # value) \n");
getInput(startPoint);
displayInput(startPoint);

Serial.print("Select the Ending Location(enter # value)\n");
getInput(endPoint);
displayInput(endPoint);

char feet=distance(startPoint,endPoint);
Serial.print(feet);
Serial.print("feet");
Serial.print("\n\n\n\n\n\n\n");
}

{
delay(2000);
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 - Marta's room\n");
Serial.print("6 - Laurice's House\n");
Serial.print("\n\n\n");
}

void getInput(int& number)
{
do
{
delay(500);
if(Serial.available())
{
}
if((number<0))
{
}
else if(number>6)
{
Serial.print(number);
Serial.print("\n# Must Be from 1-6\n");
number=NULL;
}
}
while ((number<0) || (number>6) || (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("Marta's room\n");
break;
case 6:
Serial.print("Laurice's House\n");
break;
}
}

char distance(char room1,char room2)
{
char distance[7][7]=
{
{0,0,0,0,0,0,0},
{0,'saturn','mercury','urunus','sun','earth','pluto'},
{0,'notes','delete','sum','ligit','monkey','money'},
{0,'nail','nail','destroy','laugh','rich','plant'},
{0,'bed','clock','drapes','fallacies','click','door'},
{0,'elephant','table','mouse','star','ratio','magic'},

};
return distance [room1][room2];
}
``````

What letter? Where? When?

Your first program should print a lot of words. So should your second and third.

It's hard to understand what your problem is.

Which Arduino are you using? The number of strings in each code could be causing memory issues.

The random letter (random to me at least) shows up after I enter in starting and ending location. That would be the amount of feet if it were the original distance program. For example if my starting distance is 2 and the ending distance is 3, the word that shows up is m. The first program prints the words in regards to location and at the end it prints out the distance in feet. There really isn't a problem. i just want to learn how to print out words instead of number in the 2d array. I'm using the Duemilanove.

Do you mean that in place of printing 132 you print out "one hundred and thirty two"?

No I want it to print out the corresponding word in the array

``````char distance(char room1,char room2)
{
char distance[7][7]=
{
{0,0,0,0,0,0,0},
{0,'saturn','mercury','urunus','sun','earth','pluto'},
{0,'notes','delete','sum','ligit','monkey','money'},
{0,'nail','nail','destroy','laugh','rich','plant'},
{0,'bed','clock','drapes','fallacies','click','door'},
{0,'elephant','table','mouse','star','ratio','magic'},

};
return distance [room1][room2];
}
``````

I'm not sure how to make that happen correctly though

You have an array of characters. 'mercury' is NOT a character. A character variable can hold a single character.

Technically, this is not entirely accurate, but practically, as used on an Arduino, it is.

You need an array of string:- http://www.uni-weimar.de/medien/wiki/Crash_Course_Programming_for_Arduino#Creating_an_Array_of_Strings

elevatedsounds: David, Thank you very much.

You're quite welcome - I'm sorry it took so many questions to arrive at the solution, but I'm delighted it works for you.