Arduino with Matlab. Need some assistance.

1 view (last 30 days)
Aliff
Aliff on 4 Mar 2013
Hi there, this is my first time trying to run Arduino with Matlab. Also I'm not that good with Matlab. So, I got this Arduino code to try and run 2 DC motors:
int motor_left[] = {
4, 5, 10};
int motor_right[] = {
2, 3, 9};
int ledPin = 13;
void setup()
{
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);
}
}
void loop()
{
drive_forward();
delay(500);
motor_stop();
drive_backward();
delay(500);
motor_stop();
}
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);
}
Now when I try to run this code in Matlab, it causes some error like this:
Error: File: motor.m Line: 9 Column: 3
Illegal use of reserved keyword "for".
So maybe I thought that I need to translate the Arduino code into Matlab code.
This is the one that I used in Matlab:
a=arduino == ('COM14')
int motor_left[] == { 4, 5, 10};
int motor_right[] == { 2, 3, 9};
int ledPin = 13;
void setup()
{int i;
for(i = 0; 1i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
pinMode(ledPin, OUTPUT);}
}
void loop()
{ drive_forward();
delay(500);
motor_stop();
drive_backward();
delay(500);
motor_stop();
}
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);
}

Answers (1)

Walter Roberson
Walter Roberson on 4 Mar 2013
The code you have is not MATLAB code, it is C code. You cannot run it in MATLAB. It needs to be compiled by a C compiler that is appropriate for the Arduino and loaded into the device.

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!