Ping Sensor Robot Code

This is the code for a robot car with a ping sensor that I'm doing for a school project.
It's won't compile and I don't understand why the error messages it's showing are doing so, can someone help me?

#include <NewPing.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Servo.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int leftsonar;
int rightsonar;
int centersonar;
int forwardsonar;
int spintimer;
int sensvalues[5];

void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Adafruit Motorshield v2 - DC Motor test!");

AFMS.begin();
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(90);
myMotor->setSpeed(150);
myOtherMotor->setSpeed(150);
myMotor->run(FORWARD);
myOtherMotor->run(BACKWARD);
myMotor->run(RELEASE);
myOtherMotor->run(RELEASE);
}

void loop(){
createandsortarray();
forwardsonar = sensvalues[3];
if (forward == 0 || forwardsonar >=15) {
forward();
}
else{
dontmove();
lookaround();
}
}

uint8_t i;

void forward(){
myMotor->run(FORWARD);
myOtherMotor->run(FORWARD);
for (i=0; i<255; i++) {
myMotor->setSpeed(i);
myOtherMotor->setSpeed(i);
running = true

void backup(){
myMotor->run(BACKWARD);
myOtherMotor->run(BACKWARD);
for (i=0; i<255; i++) {
myMotor->setSpeed(i);
myOtherMotor->setSpeed(i);
running = true
}

void dontmove(){
myMotor->setSpeed(0);
myOtherMotor->setSpeed(0);
running = false;
}

void turnright(){
myMotor->run(FORWARD);
myOtherMotor->run(BACKWARD);
for (i=0; i<255; i++) {
myMotor->setSpeed(i);
myOtherMotor->setSpeed(i);

void turnleft(){
myMotor->run(BACKWARD);
myOtherMotor->run(FORWARD);
for (i=0; i<255; i++) {
myMotor->setSpeed(i);
myOtherMotor->setSpeed(i);

void lookaround(){
pingservo.write(180);//right
delay(1000);
createandsortarray();
rightsonar = sensvalues[3];
delay(500);
pingservo.write(0);//left
delay(1000);
createandsortarray();
leftsonar = sensvalues[3];;
delay(500);
pingservo.write(90); //middle
delay(1000);
createandsortarray();
centersonar = sensvalues[3];
delay(500);
if (leftsonar == 0 && rightsonar == 0)
{
spin();
}
else{
if (leftsonar > rightsonar){
turnleft();
}
else if (rightsonar > leftsonar){
turnright();
}
else if (rightsonar <= 10 && leftsonar <= 10 && centersonar <= 10){
backup();
lookaround();
}
else if (centersonar >= 15 || centersonar == 0){
forward();}
}
}

void createandsortarray(){
int i = 0;
while (i <5){
sensvalues = sonar.ping_cm();

  • i++;*
  • }*
  • sort(sensvalues, 5);*
  • }*
    void sort(int a[], int size) {
  • for(int i=0; i<(size-1); i++) {*
  • for(int o=0; o<(size-(i+1)); o++) {*
  • if(a[o] > a[o+1]) {*
  • int t = a[o];*
  • a[o] = a[o+1];*
  • a[o+1] = t;*
  • }*
  • }*
  • }*
    Error Messages:
    Arduino: 1.6.6 (Windows 10), Board: "Arduino/Genuino Uno"
    C:\Users\Michael Gomez\Downloads\MG_Final_Project_Code\MG_Final_Project_Code.ino: In function 'void loop()':
    MG_Final_Project_Code:41: error: 'createandsortarray' was not declared in this scope
  • createandsortarray();*
  • ^*
    MG_Final_Project_Code:47: error: 'dontmove' was not declared in this scope
  • dontmove();*
  • ^*
    MG_Final_Project_Code:48: error: 'lookaround' was not declared in this scope
  • lookaround();*
  • ^*
    C:\Users\Michael Gomez\Downloads\MG_Final_Project_Code\MG_Final_Project_Code.ino: In function 'void forward()':
    MG_Final_Project_Code:60: error: 'running' was not declared in this scope
  • running = true*
  • ^*
    MG_Final_Project_Code:62: error: expected ';' before 'void'
    void backup(){
    ^
    MG_Final_Project_Code:144: error: expected '}' at end of input
  • }*
  • ^*
    MG_Final_Project_Code:144: error: expected '}' at end of input
    exit status 1
    'createandsortarray' was not declared in this scope
  • This report would have more information with*
  • "Show verbose output during compilation"*
  • enabled in File > Preferences.*

You are missing some '}'s, like at the end of forward() and backup().

The error message that you are receiving is misleading because your syntax is incorrect. If you use Tools->Auto Format you will see the message "Too many left curly braces", which could also mean not enough right curly braces.

If you highlight a left curly brace in your code the corresponding right curly brace should highlight. This can help you find your mistakes. For example, highlight the first left curly brace in the function dontmove() and the closing brace will highlight. Do the same with forward() and backward() and you will not see any closing brace highlight, pointing to a problem.

You should not even try to compile the code until Auto Format works without error.

If you continue to have difficulty finding your syntax errors I suggest you create a new empty file and copy in up to the end of your loop() function and compile. Then copy in one function at at time, fixing the syntax as you go along.

You're also missing the mandatory code tags:-

You really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

And besides what's been mentioned, you're also missing semicolons at the end of some lines.
ie

running = true
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
Adafruit_DCMotor *myOtherMotor = AFMS.getMotor(2);

Really? It never occurred to you to give the instances MEANINGFUL names?