Max -> Arduino -> Servo motors

Karina Kalantajevska's icon

Hello,

I'm doing project where I want Servos to move, for 20° each side, when webcam recognize person (detect face).

I have program in Max Mps which recognize the face and I made output, when face recognized it's 1 when where no faces 0.

I have problem with connection to Arduino (sending output from Max to Arduino) and with code for servos.

Can you please help me with it? Or give some tips where to search for information:)

Thank you

Source Audio's icon

post your available hardware infos, OS etc

Karina Kalantajevska's icon

Hello, thank you for your reply

Do you mean... Arduino uno and I'm working on macOs Catalina, Intel HD graphics, Micro Servo 9g

Source Audio's icon

In arduino you can use servo library.

it depends how you want to control motors.

option 1 is to send On / Off switch values from Max,

and to have fixed sweep in arduino code.

or option 2 to send position values from max...

in max use serial object to send values to arduino

post more details if you need more help

Karina Kalantajevska's icon

thanks

can u explain more about option 1

here what I'm using:

#include <Servo.h>

Servo myservo1;

Servo myservo2;

int pos1 = 0;

int pos2 = 0;

String inputString = ""; // a String to hold incoming data

bool stringComplete = false; // whether the string is complete

bool ones;


void setup() {

// initialize serial:

Serial.begin(115200); //115200

// reserve 200 bytes for the inputString:

inputString.reserve(200);

myservo1.attach(9);

myservo2.attach(10);

}


void loop() {

if (!ones){

Serial.println("please input x an y servo angle ...");

ones=1;

}

// if there's any serial available, read it:

while (Serial.available() > 0) {

Serial.println("xxx");

// look for the next valid integer in the incoming serial stream:

pos1 = Serial.parseInt();

// do it again:

Serial.println(pos1);

pos2 = Serial.parseInt();


// look for the newline. That's the end of your

//sentence:

if (Serial.read() == '\n') {

//constrain the values to 0 - 180 : servo range

pos1 = constrain(pos1, 0, 180);

pos2 =constrain(pos2, 0, 180);

myservo1.write(pos1);

myservo2.write(pos2);

}

}

}

Untitled2.maxpat
Max Patch

Source Audio's icon

To start with ...

you set baud rate in arduino to 115200 but in max to 9600 ?

why do you need to send xxx back to max ?

ardiono code you posted is too complicated for what it does.

It expects 2 values = degrees for each motor.

I don't see any trace of sending data to arduino in your max patch ???

If you want to go that way,

send from max pairs of numbers in wished interval + new line (ASCII 10)

Arduino Code:

#include <Servo.h>

Servo myservo1; Servo myservo2; int pos1 = 0; int pos2 = 0;

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

myservo1.attach(9); myservo2.attach(10);}

void loop() {

while (Serial.available() > 0) {pos1 = Serial.parseInt();

pos2 = Serial.parseInt();

if (Serial.read() == '\n') {myservo1.write(pos1);myservo2.write(pos2);}}}

max patch (only sending values)

this will go up/down 0 - 40.

you can use anything to offset, limit or whatever that 2 values

Source Audio's icon

sorry - forgot to answer about option 1 ...

you create sweeps in arduino, and switch them on or off

from max.

but make sure that sweeps don't use delay as timer between steps,

because that would block receiving of serial messages.

If you ate interested, check stuff like blinking without delay,

non blocking delays etc.

I won't loose time to write code if you use the original one ...