Serial Reading and servo selection

Hi there!

im trying to control servos with matlab and arduino uno.

1 servo is no problem. no i want to connect 2 servos and control them via serial port

i want to send something like this:

A
1
2
3
4
5
...
180
B
1
2
3
4
5
...
180

First Servo A is activated then it gets his data
After that Servo B is activated and data is send.

how do i read this in with arduino?

thanks,

---moe

I have done the same previously so what happens actually when you click 1 or 2 or 3 or A..........something like you want within the Serial Port monitor then it sends the ASCII values to the arduino and not the 1 or 2 or 3 or A........... as you expected so programme your code with the ASCII values and not the keyboard value.

for example the ASCII value for 'A' is 65 and 'a' is 97 , so if you want servo to move some angle when you click 'a' programme your code with 97 as this is the value that would be sent to arduino on small 'a' input.

hi, thanks for reply.

well i dont know if you are right.

this is what i send from matlab:

%% Arduino Servo Controller

arduino = serial('COM3','BaudRate',9600); % Set ComPort

fopen(arduino);
pause(0.01) % this is very important! arduino needs a little time to initialise the fopen command, because it resets it!

fwrite(arduino,201)

for i = 0:179  % Runs your servo from 0-179 degree
fwrite(arduino,i);
pause(0.02)
end

fclose(arduino);

delete(arduino)
clear arduino

this is my arduino code:

/*
Matlab-Arduino Servo Controller
*/

#include <Servo.h> 

//DECLARATION--------------------- 
Servo servo1;
Servo servo2;

//SETUP----------------------------
void setup() {
  
  Serial.begin(9600);
    
  servo1.attach(2);
  servo2.attach(3);
   
}

//LOOP--------------------------------
void loop() {

 if( Serial.available() ) 
    {
        int ser = Serial.read();
        
        if(ser == 201)
        {
        servo1.write(ser);
        }
        
        if(ser == 202)
        {
        servo2.write(ser);
        }
        
    }
  
}

I skipped the idea to use A,B, C....
instead i use 201, 202, 203....

when i just send the command 65 ie, my servo turns to 65 degree
so i dont have to send ascii commands.

how did you made the selection, if serial says 201, how do you send the other serial commands following to your servo?

---moe

        if(ser == 201)
        {
        servo1.write(ser);
        }

Can your servo really turn to 201 degrees?

no, it cant.

this is the code to select the servo

Servo1 = 201
Servo2 = 202
...

for example,
i want to send 2 angles to 2 servos, then it should look like this in serial monitor:

201
45
202
90

servo1 should be 45 degree, and servo2 should be 90 degree

so, i think i know how to send from matlab, like the code above

but how do i seperate the different serial inputs with the arduino?

edit:

the big problem is, i cant use the serial monitor, to check what kind of data is send, it always says com3 is already in use.
is there any solution, to use the serial monitor, while sending data with com3 with matlab?

So, if the value read from the serial port is 201, you want to read another value to get the actual angle for servo 1.
If the value read from the serial port is 202, you want to read another value to get the actual angle for servo 2.

Doesn't seem that difficult, to me. Just be sure to not read anything that isn't there, yet. In other words, instead of not reading unless there is one byte or more, as you do now, you need to not read unless there are two or more bytes.

The sender now appears to send a value to define the servo and then a series of values to sweep the servo. That, of course, will need to change, too.

yes, thats what i want to do,

another possibility is to send data and store it in an array with 2 numbers

something like

if(int i; i < 2; i++)
{
reading[i] = Serial.Read()
}

if(reading[0] == 201)
{
 Servo1.write(reading[1])
}


if(reading[0] == 202)
{
 Servo2.write(reading[1])
}


....

but this isnt working right now too....

any ideas?

if(int i; i < 2; i++)

Local variables are not initialized by default. You MUST supply an initial value. Zero is generally a good choice.

you re right....

ok, heres the code... probably not working :frowning:

/*
Matlab-Arduino Servo Controller
*/

#include <Servo.h> 
int reading[2];



//DECLARATION--------------------- 
Servo servo1;
Servo servo2;

//SETUP----------------------------
void setup() {
  
  Serial.begin(9600);
    
  servo1.attach(2);
  servo2.attach(3);
   
}

//LOOP--------------------------------
void loop() {

 
      while (Serial.available()>0) 
      {
    
        for (int i=0; i<2; i++) 
        {
        reading [i] = Serial.read();
        }
    
          if (reading[0] == 201) 
          {
          servo1.write(reading[1]);
          }
          if (reading[0] == 202) 
          {
          servo2.write(reading[1]);
          }

       } 
       
        
    }

if(int i; i < 2; i++)

Local variables are not initialized by default. You MUST supply an initial value. Zero is generally a good choice.

Also, try "for", not "if" :wink:

Also, try "for", not "if"

Sure. Trust me to pick out the small error and miss the really big one.