Arduino Uno/Matlab Interaction.

Hi, I have created a gui in Matlab that will consists of 2 sliders. These 2 sliders will control 2 servos on the arduino. The values of sliders in matlab are printed in 2 text boxes. Can someone PLEASE help with the code for arduino??(in order for the servos to be controlled via the 2 sliders in the matlab gui)

Hi. I want to connect my Matlab Gui with arduino to control 2 servo motors. My Gui Consists of 2 sliders. The values of these sliders are printed in separate text boxes within the gui(from 0-180). I want the values of these text boxes to control the degrees of servos. Can someone help with the Arduino Sketch for this case?
I can establish serial communication between matlab and arduino.

Any kind of help/ideas appreciated.

Hi, I made a gui in matlab that consists of 2 sliders. these sliders will control the 2 servos connected to arduino. Anyone has an idea on how to to program matlab and arduino?
Thanks in advance

Possible receiving code for the arduino.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

There are examples of more general code for receiving data in serial input basics

...R

@konanhar, please do not cross-post. Similar threads merged.

Thank you VERY VERY MUCH about the arduino code. I sort of get it now. what should be the name of the variable sent from matlab's arduino for each servo?

konanhar:
what should be the name of the variable sent from matlab's arduino for each servo?

If there are (say) 3 servos and you always send 3 numbers in the same order you don't need names to indentify them.

...R

Hello R,

Thanks for getting back to me. I still have troubles sending numbers from matlab to arduino. (i have copied the arduino code as it is and attach the appropriate pins)
Could you give an example of matlab code?

Thanks for being so helpful
I appreciate it!

konanhar:
(i have copied the arduino code as it is and attach the appropriate pins)

You seem to have forgotten the Arduino code.

And, sorry, I don't know Matlab.

...R