MATLAB: ArduinoIO package and Arduino Uno Motor Shield R3 server sketch

arduinoelectric_motor_controlmotor shieldpower_electronics_control

I'm trying to use R3 shield with matlab script. I installed the ArduinoIO package but there is no server sketch for Arduino software for this particular shield. Only Adafruit motor shields are included. Where do I get the right sketch for driving R3 with this package?

Best Answer

I figured this out. No sketch was needed, just the right tools. Arduino can read serial ports and matlab write to them. Here's the codes if anybody ever meets the same "problem":
Arduino:
// using example:
// http://social.nkcelectronics.com/discussion/1/arduino-motor-shield-r3-introduction/p1
// Include the Stepper Library
#include <Stepper.h>
// Map our pins to constants to make things easier to keep track of
const int pwmA = 3;
const int pwmB = 11;
const int dirA = 12;
const int dirB = 13;
// The amount of steps for a full revolution of your motor.
const int stepsPerRevolution = 200;
// Initialize the Stepper class
Stepper myStepper(stepsPerRevolution, 12,13);
// Initialize the incoming command byte
char incomingByte = 0;
void setup() {
// Set the RPM of the motor
myStepper.setSpeed(40);
// Turn on pulse width modulation
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
// Start the log
Serial.begin(9600);
}
void loop() {
// Wait for any input from serial
if (Serial.available() > 0) {
// Read the incoming byte
incomingByte = Serial.read();
// Commands for stepper
if ( incomingByte == 'a' ) {
myStepper.step(200);
}
}
}
Matlab:
% using example:
% http://se.mathworks.com/help/instrument/writing-and-reading-data-2.html
pause on;
% initialise serial object
s = serial('COM6');
s.Baudrate=9600;
s.StopBits=1;
s.Terminator='LF';
s.Parity='none';
s.FlowControl='none';
% open for usage, pause for process delay
fopen(s);
pause(2);
% give the serial port a command for arduino to execute
for n = 1:10
fprintf(s,'%c','a'); % using char a for "drive"
pause(2);
end
delete(s)
Related Question