'motor_left' was not declared in this scope

Hi,

Trying to get a small robot to work with an Arduino Uno and a L293D motor driver chip.

Basing our code on this Instructable: http://www.instructables.com/id/Control-your-motors-with-L293D-and-Arduino/?ALLSTEPS

Here is the code we are trying to use:

/*
  Michael's first go at coding Arduino  
  Turns on an LED on for one second, then off for one second, repeatedly.
  Controls L293D motor driver chip.
  22/6/2016
  For Mr T's class
 */


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  Serial.begin(9600);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(500);              // wait for a second

// Setup motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};
int i;

for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}

drive_forward();
delay(1000);                // Delay 1 second
motor_stop();

Serial.println("Drive forward to 1");


drive_backward();
delay(1000);
motor_stop();

Serial.println("Drive back to 2");


turn_left();
delay(1000);
motor_stop();

Serial.println("Drive left to 3");


turn_right();
delay(1000);
motor_stop();

Serial.println("Drive right to 4");


motor_stop();
delay(1000);
motor_stop();

Serial.println("Drive nowhere to 5");
}

// --------------------------------------------------------------------------- Drive


void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}


void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}


void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}


void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}


void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

However we are getting errors:

'motor_left' was not declared in this scope

(See below for full error.)

HELP! :slight_smile: We're stuck! Thank you.
Rob.

Arduino: 1.6.9 (Windows 8.1), Board: "Arduino/Genuino Uno"

C:\Users\Jiaren.TANG\Desktop\ET\Arduino_L293D_control_Ver_1\Arduino_L293D_control_Ver_1.ino: In function 'void motor_stop()':

Arduino_L293D_control_Ver_1:76: error: 'motor_left' was not declared in this scope

 digitalWrite(motor_left[0], LOW);

              ^

Arduino_L293D_control_Ver_1:78: error: 'motor_right' was not declared in this scope

 digitalWrite(motor_right[0], LOW);

              ^

C:\Users\Jiaren.TANG\Desktop\ET\Arduino_L293D_control_Ver_1\Arduino_L293D_control_Ver_1.ino: In function 'void drive_forward()':

Arduino_L293D_control_Ver_1:85: error: 'motor_left' was not declared in this scope

 digitalWrite(motor_left[0], HIGH);

              ^

Arduino_L293D_control_Ver_1:87: error: 'motor_right' was not declared in this scope

 digitalWrite(motor_right[0], HIGH);

              ^

C:\Users\Jiaren.TANG\Desktop\ET\Arduino_L293D_control_Ver_1\Arduino_L293D_control_Ver_1.ino: In function 'void drive_backward()':

Arduino_L293D_control_Ver_1:93: error: 'motor_left' was not declared in this scope

 digitalWrite(motor_left[0], LOW);

              ^

Arduino_L293D_control_Ver_1:95: error: 'motor_right' was not declared in this scope

 digitalWrite(motor_right[0], LOW);

              ^

C:\Users\Jiaren.TANG\Desktop\ET\Arduino_L293D_control_Ver_1\Arduino_L293D_control_Ver_1.ino: In function 'void turn_left()':

Arduino_L293D_control_Ver_1:101: error: 'motor_left' was not declared in this scope

 digitalWrite(motor_left[0], LOW);

              ^

Arduino_L293D_control_Ver_1:103: error: 'motor_right' was not declared in this scope

 digitalWrite(motor_right[0], HIGH);

              ^

C:\Users\Jiaren.TANG\Desktop\ET\Arduino_L293D_control_Ver_1\Arduino_L293D_control_Ver_1.ino: In function 'void turn_right()':

Arduino_L293D_control_Ver_1:109: error: 'motor_left' was not declared in this scope

 digitalWrite(motor_left[0], HIGH);

              ^

Arduino_L293D_control_Ver_1:111: error: 'motor_right' was not declared in this scope

 digitalWrite(motor_right[0], LOW);

              ^

exit status 1
'motor_left' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

You should declare motor_left and motor_right globally, not inside loop().

Excellent! :smiley: Thank you so much! We now have a very happy 'bot running around the place! :smiley:

Here is the code we fixed! :slight_smile:

/*
  Michael's first go at coding Arduino
  
  Turns on an LED on for one second, then off for one second, repeatedly.

  Controls L293D motor driver chip.

  22/6/2016
  For Mr T's class with a little help from aarg on the Arduino Forums. 
 */

// --------------------------------------------------------------------------- Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};

// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(9600);
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
  // Setup motors
  int i;
  for(i = 0; i < 2; i++){
  pinMode(motor_left[i], OUTPUT);
  pinMode(motor_right[i], OUTPUT);
  }
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(500);              // wait for a second
  
  drive_forward();
  delay(1000);                // Delay 1 second
  motor_stop();
  
  Serial.println("Drive forward to 1");
  
  
  drive_backward();
  delay(1000);
  motor_stop();
  
  Serial.println("Drive back to 2");
  
  
  turn_left();
  delay(1000);
  motor_stop();
  
  Serial.println("Drive left to 3");
  
  
  turn_right();
  delay(1000);
  motor_stop();
  
  Serial.println("Drive right to 4");
  
  
  motor_stop();
  delay(1000);
  motor_stop();
  
  Serial.println("Drive nowhere to 5");
}

// --------------------------------------------------------------------------- Drive


void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}


void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}


void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}


void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);
digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}


void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);
digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

It's what we do. :slight_smile: