serial communication between matlab and arduino

i am trying to serially communicate arduino and matlab. i have to receive signals from matlab. based on the received signals arduino has to run the motors using the motor shield. the receiving led is glowing whenever the matlab sends the signal but i am unable to read those signals in the arduino. i am attaching my arduino code to read the siganls. i am also including the matlab code to transfer the signals.

tsshield.ino (1.11 KB)

Code from Original Post so others don’t have to download it

#include <SD.h>

#include <Wire.h>

#include <AFMotor.h>

#include <SoftwareSerial.h>

 int s;
 AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm

void setup() {

  motor.setSpeed(200);     // set the speed to 200/255

void loop(){

  case 3:Serial.print("3"); break;
  case 0: Serial.println("0");
 ;      // turn it on going forward;      // turn it on going forward
  case 1: Serial.println("1");;     // the other way;     // the other way
  case 2:Serial.print("2");;      // stopped;      // stopped
  delay(1000);;      // stopped;      // stopped
  default : break;


Have a look at the examples in Serial Input Basics for simple reliable ways to receive data. There is also a parse example.

You should also separate the code for receiving data from the code for controlling the motors so that the different parts can be tested on their own. The receiving code should store the data in variables and the motor code should do its stuff based on whatever happens to be in the variables.


Hmm, your code does not compile because you have a 'case 3:' outside a switch.

Please post a fixed code so we can understand what your doing.