Go Down

Topic: serial communication between matlab and arduino (Read 59 times) previous topic - next topic

haritha94

Apr 06, 2016, 12:20 pm Last Edit: Apr 06, 2016, 12:27 pm by haritha94
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.

Robin2

Code from Original Post so others don't have to download it
Code: [Select]
#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() {

  pinMode(s,INPUT);
  Serial.begin(9600);
  motor.setSpeed(200);     // set the speed to 200/255
  motor2.setSpeed(200);
}


void loop(){

if(Serial.available()){
s=Serial.read();
  break;
  case 3:Serial.print("3"); break;
s=Serial.parseInt();
//s=Stream.read();
Serial.println();
switch(s){
  case 0: Serial.println("0");
           motor.run(FORWARD);      // turn it on going forward
 
 motor2.run(BACKWARD);      // turn it on going forward
  delay(1000);
          break;
  case 1: Serial.println("1");
      motor.run(BACKWARD);     // the other way
 
  motor2.run(FORWARD);     // the other way
  delay(1000);
  break;
  case 2:Serial.print("2");
        motor.run(FORWARD);      // stopped
 
  motor2.run(FORWARD);      // stopped
  delay(1000);
 
  motor.run(RELEASE);      // stopped
 
  motor2.run(RELEASE);      // stopped
  delay(1000);
  default : break;
}
}
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

#2
Apr 06, 2016, 01:06 pm Last Edit: Apr 06, 2016, 01:08 pm by Robin2
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.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sterretje

#3
Apr 06, 2016, 02:02 pm Last Edit: Apr 06, 2016, 02:02 pm by sterretje
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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy