Maze solving algorith help!

Hi everyone, Im a complete newbie at this and am trying to make a left wall following maze solving robot. I keep getting errors saying expected constructor, destructor etc when trying to recall functions, my code is attached and as you can see it wont compile due to these errors, any ideas as to how i can solve it

Any help with this would be awesome as im currently very confused!

Kind Regards

micromouseNEW.ino (11.4 KB)

Do an auto-format (CTRL-T) and you will be told why it's not working.

I go the reply autoformat cancelled - too many curly braces

Have a look at these 3 lines:

144 191 255

Tell me what is wrong with them :)

Once fixed, then do CTRL-T again.

Then post the auto formatted code for us to look at.

p.s. it is absolutely vital that code be correctly formatted. It isn't difficult to keep lines correctly indented, but not doing so makes debugging almost impossible, makes the code harder for both you and us to understand, and makes learning more difficult. At the very least if you past new code in and it isn't aligned, use CTRL-T to indent automatically.

Another thing - I think you may need to learn about variable scope.

cheers guys, turns out I just had quite a lot of formatting to fix. Compiles now which is awesome. I have had to define variables in each of my void functions which is what i assume you meant when you said about variable scope and is certainly something I shall now look into. The code is attached if you fancy a look…

micromouseNEW.ino (11.9 KB)

Although the code compiles it won’t work. For instance, what is this line doing ?

  while(analogRead(leftinches>2 && rightinches>2))

Which pin is analogRead using in this code ?

void straight(){
  long leftduration, leftinches, fronttduration, 
  frontinches, rightduration, rightinches;
  if( analogRead(leftinches)<2){

When variables are declared inside a function their value is only known to that function. Once the function ends the variable ceases to exist so you cannot create a variable in a function and use its value later outside of the function. So, all of the variables such as leftInches that you create in the readSensors() function are only available in that function even if they have the same name as a variable eleswehere. So, when you do this


    if(leftinches>2 || rightinches>2){ //  if left OR right inches > 2

you are not testing the value of the leftInches variable created in the readSensors() function but the value of another variable with the same name that you just declared in the lefthandwall() function.

To summarise, each time you declare a variable you can only use its value in the function in which it was declared. You should also note that when a variable is declared but not given a value it is no good testing its value as you do below because you don’t know what it is.

void lefthandwall(){

  long leftduration, leftinches, fronttduration, 
  frontinches, rightduration, rightinches;

  if( leftinches>2 && rightinches>2){

So, what to do ?
One easy way out is to declare all your variables as global at the start of the program. They will then be available to all functions throughout the program. Remove all the other variable declarations otherwise your functions will use the local versions and not the global ones. This is not the best programming practice but it will get you going.

That's why I was saying you need to look into variable scope :P Variables named the same but defined in different functions are different variables.