2D Array Passing to Function

Hello,

I’m trying to create a function which will be used to drive multiple motors by changing input parameters. First i created an array called motor to assign motor connections for each motor. Then i created a drive() function to drive the motors according to orientation input. But the problem is that i can not successfully pass multidimensional array to drive function. The code i wrote is given below.

const int numberOfMotors=2;
int motor[numberOfMotors][3]={{22,23,3},{24,25,4}};

void drive(int motorNo,int pwm, char orientation){
	if(orientation=='left'){
		digitalWrite(motor[motorNo][0],HIGH);
		digitalWrite(motor[motorNo][1],LOW);
		analogWrite(motor[motorNo][2],pwm);
	}
	if(orientation=='right'){
		digitalWrite(motor[motorNo][0],LOW);
		digitalWrite(motor[motorNo][1],HIGH);
		analogWrite(motor[motorNo][2],pwm);
	}
	if(orientation=='break'){
		digitalWrite(motor[motorNo][0],HIGH);
		digitalWrite(motor[motorNo][1],HIGH);
		analogWrite(motor[motorNo][2],pwm);
	}
}


void setup(){
	Serial.begin(9600);
	for(int i=0; i<numberOfMotors; i++){
		for(int j=0; j<3; j++){
			pinMode(motor[i][j],OUTPUT);
		}
	}
}

void loop(){
	drive(0,255,'left');
	Serial.println("Turning Left!");
}
char orientation){
	if(orientation=='left')

{cough}

AWOL:

char orientation){
if(orientation=='left')


{cough}

Well, this was not expected… Thank you!

The working code is as shown if someone wants to use a handy dc motor drive function…

const int numberOfMotors=2;
int motor[numberOfMotors][3]={{22,23,3},{24,25,4}};

void drive(int motorNo,int pwm, char orientation){
	if(orientation=='l'){
		digitalWrite(motor[motorNo][0],HIGH);
		digitalWrite(motor[motorNo][1],LOW);
		analogWrite(motor[motorNo][2],pwm);
	}
	if(orientation=='r'){
		digitalWrite(motor[motorNo][0],LOW);
		digitalWrite(motor[motorNo][1],HIGH);
		analogWrite(motor[motorNo][2],pwm);
	}
	if(orientation=='b'){
		digitalWrite(motor[motorNo][0],HIGH);
		digitalWrite(motor[motorNo][1],HIGH);
		analogWrite(motor[motorNo][2],pwm);
	}
}


void setup(){
	Serial.begin(9600);
	for(int i=0; i<numberOfMotors; i++){
		for(int j=0; j<3; j++){
			pinMode(motor[i][j],OUTPUT);
		}
	}
}

void loop(){
	for(int i=0;i<=255;i++){
		drive(0,i,'l');
		delay(25);
	}
	for(int i=255;i>=0;i--){
		drive(0,i,'l');
		delay(25);
	}
	drive(0,255,'b');
	delay(3000);
	for(int i=0;i<=255;i++){
		drive(0,i,'r');
		delay(25);
	}
	for(int i=255;i>=0;i--){
		drive(0,i,'r');
		delay(25);
	}
}

The Arduino is a small device. Just use global variables for the arrays so there is no need to pass them to functions.

...R

There are no arrays being passed, only array elements - the thread title is incorrect.

Variable names should reflect what they hold. The array motor can NOT hold a bunch of motors.

If the name motorPins is more appropriate, please tell me which Arduino you are using that has more than 255 pins. If your Arduino doesn't have more than 255 pins, why does the type need to allow for more than that number of pins? In other words, why is the type int?