Controlling multiple motors via PWM

Hi

I was wondering what i should do to control multiple motors independently using MaxMSP.

I have been looking at the Dimmer example in arduino used to dim LEDs, and am trying to essentially double this code so that i can use two sliders in MaxMSP to control each motor.

Any ideas would be greatly appreciated.

Thanks

p.s. The code below is valid but doesn't work, it might give you more of an idea of what i am on about if i haven't stated it clearly.

const int ledPin = 9; // the pin that the LED is attached to
const int ledPin1 = 3;
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
}

void loop() {
byte brightness;
byte spinning;

// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
spinning = Serial.read();
// set the brightness of the LED:
analogWrite(ledPin, brightness);
analogWrite(ledPin1, spinning);
}
}

if (Serial.available()) {
   // read the most recent byte (which will be from 0 to 255):
   brightness = Serial.read();
   spinning = Serial.read();

So, if there is at least one byte available, it's OK to read two bytes?

I don't think so.

Thanks for the reply Paul

I would like to read two bytes simultaneously
I tried to split the values using while for one slider, but to no avail.

So if I come on the arduino forum, I can ask a noob question?
I dont think so. :stuck_out_tongue:

So if I come on the arduino forum, I can ask a noob question?

Of course you can.

The if test needs only a slight modification:

if (Serial.available()[glow] >= 2[/glow]) {
   // read the most recent byte (which will be from 0 to 255):
   brightness = Serial.read();
   spinning = Serial.read();

Cheers Paul, worked a treat. :slight_smile:

For anyone who has the same problem here is the arduino code and max 5 patcher.

{
"patcher" : {
"fileversion" : 1,
"rect" : [ 0.0, 44.0, 771.0, 614.0 ],
"bglocked" : 0,
"defrect" : [ 0.0, 44.0, 771.0, 614.0 ],
"openrect" : [ 0.0, 0.0, 0.0, 0.0 ],
"openinpresentation" : 0,
"default_fontsize" : 12.0,
"default_fontface" : 0,
"default_fontname" : "Arial",
"gridonopen" : 0,
"gridsize" : [ 15.0, 15.0 ],
"gridsnaponopen" : 0,
"toolbarvisible" : 1,
"boxanimatetime" : 200,
"imprint" : 0,
"boxes" : [ {
"box" : {
"maxclass" : "message",
"text" : "port a",
"patching_rect" : [ 279.0, 408.0, 41.0, 18.0 ],
"numinlets" : 2,
"numoutlets" : 1,
"fontname" : "Arial",
"outlettype" : [ "" ],
"fontsize" : 12.0,
"id" : "obj-29"
}

}
, {
"box" : {
"maxclass" : "message",
"text" : "close",
"patching_rect" : [ 280.0, 387.0, 39.0, 18.0 ],
"numinlets" : 2,
"numoutlets" : 1,
"fontname" : "Arial",
"outlettype" : [ "" ],
"fontsize" : 12.0,
"id" : "obj-27"
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "print",
"patching_rect" : [ 235.0, 496.0, 34.0, 20.0 ],
"numinlets" : 1,
"numoutlets" : 0,
"fontname" : "Arial",
"fontsize" : 12.0,
"id" : "obj-26"
}

}
, {
"box" : {
"maxclass" : "toggle",
"patching_rect" : [ 255.0, 351.0, 20.0, 20.0 ],
"numinlets" : 1,
"numoutlets" : 1,
"outlettype" : [ "int" ],
"id" : "obj-23"
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "metro 10",
"patching_rect" : [ 208.0, 386.0, 58.0, 20.0 ],
"numinlets" : 2,
"numoutlets" : 1,
"fontname" : "Arial",
"outlettype" : [ "bang" ],
"fontsize" : 12.0,
"id" : "obj-22"
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "serial a 9600",
"patching_rect" : [ 203.0, 435.0, 79.0, 20.0 ],
"numinlets" : 1,
"numoutlets" : 2,
"fontname" : "Arial",
"outlettype" : [ "int", "" ],
"fontsize" : 12.0,
"id" : "obj-21"
}

}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 410.0, 198.0, 50.0, 19.0 ],
"numinlets" : 1,
"numoutlets" : 2,
"fontname" : "Verdana",
"outlettype" : [ "int", "bang" ],
"fontsize" : 10.0,
"id" : "obj-24"
}

}
, {
"box" : {
"maxclass" : "slider",
"patching_rect" : [ 413.0, 50.0, 20.0, 140.0 ],
"numinlets" : 1,
"bgcolor" : [ 0.94902, 0.94902, 0.94902, 0.0 ],
"numoutlets" : 1,
"outlettype" : [ "" ],
"size" : 256.0,
"id" : "obj-25"
}

}
, {
"box" : {
"maxclass" : "panel",
"patching_rect" : [ 412.0, 51.0, 21.0, 139.0 ],
"numinlets" : 1,
"grad1" : [ 1.0, 1.0, 1.0, 1.0 ],
"numoutlets" : 0,
"mode" : 1,
"grad2" : [ 0.509804, 0.509804, 0.509804, 1.0 ],
"angle" : 270.0,
"id" : "obj-28"
}

}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 330.0, 200.0, 50.0, 19.0 ],
"numinlets" : 1,
"numoutlets" : 2,
"fontname" : "Verdana",
"outlettype" : [ "int", "bang" ],
"fontsize" : 10.0,
"id" : "obj-9"
}

}
, {
"box" : {
"maxclass" : "slider",
"patching_rect" : [ 330.0, 50.0, 20.0, 140.0 ],
"numinlets" : 1,
"bgcolor" : [ 0.94902, 0.94902, 0.94902, 0.0 ],
"numoutlets" : 1,
"outlettype" : [ "" ],
"size" : 256.0,
"id" : "obj-10"
}

}
, {
"box" : {
"maxclass" : "panel",
"patching_rect" : [ 330.0, 50.0, 21.0, 139.0 ],
"numinlets" : 1,
"grad1" : [ 1.0, 1.0, 1.0, 1.0 ],
"numoutlets" : 0,
"mode" : 1,
"grad2" : [ 0.509804, 0.509804, 0.509804, 1.0 ],
"angle" : 270.0,
"id" : "obj-12"
}

}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 264.0, 199.0, 50.0, 19.0 ],
"numinlets" : 1,
"numoutlets" : 2,
"fontname" : "Verdana",
"outlettype" : [ "int", "bang" ],
"fontsize" : 10.0,
"id" : "obj-15"
}

}
, {
"box" : {
"maxclass" : "slider",
"patching_rect" : [ 264.0, 52.0, 20.0, 140.0 ],
"numinlets" : 1,
"bgcolor" : [ 0.94902, 0.94902, 0.94902, 0.0 ],
"numoutlets" : 1,
"outlettype" : [ "" ],
"size" : 256.0,
"id" : "obj-16"
}

}
, {
"box" : {
"maxclass" : "panel",
"patching_rect" : [ 264.0, 49.0, 21.0, 139.0 ],
"numinlets" : 1,
"grad1" : [ 1.0, 1.0, 1.0, 1.0 ],
"numoutlets" : 0,
"mode" : 1,
"grad2" : [ 0.509804, 0.509804, 0.509804, 1.0 ],
"angle" : 270.0,
"id" : "obj-18"
}

}
, {
"box" : {
"maxclass" : "newobj",
"text" : "pak i i i i i",
"patching_rect" : [ 140.0, 244.0, 73.0, 20.0 ],
"numinlets" : 5,
"numoutlets" : 1,
"fontname" : "Arial",
"outlettype" : [ "" ],
"fontsize" : 12.0,
"id" : "obj-7"
}

}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 184.0, 201.0, 50.0, 19.0 ],
"numinlets" : 1,
"numoutlets" : 2,
"fontname" : "Verdana",
"outlettype" : [ "int", "bang" ],
"fontsize" : 10.0,
"id" : "obj-3"
}

}
, {
"box" : {
"maxclass" : "slider",
"patching_rect" : [ 184.0, 55.0, 20.0, 140.0 ],
"numinlets" : 1,
"bgcolor" : [ 0.94902, 0.94902, 0.94902, 0.0 ],
"numoutlets" : 1,
"outlettype" : [ "" ],
"size" : 256.0,
"id" : "obj-4"
}

}
, {
"box" : {
"maxclass" : "panel",
"patching_rect" : [ 184.0, 51.0, 21.0, 139.0 ],
"numinlets" : 1,
"grad1" : [ 1.0, 1.0, 1.0, 1.0 ],
"numoutlets" : 0,
"mode" : 1,
"grad2" : [ 0.509804, 0.509804, 0.509804, 1.0 ],
"angle" : 270.0,
"id" : "obj-5"

}

}
, {
"box" : {
"maxclass" : "number",
"patching_rect" : [ 118.0, 200.0, 50.0, 19.0 ],
"numinlets" : 1,
"numoutlets" : 2,
"fontname" : "Verdana",
"outlettype" : [ "int", "bang" ],
"fontsize" : 10.0,
"id" : "obj-6"
}

}
, {
"box" : {
"maxclass" : "slider",
"patching_rect" : [ 118.0, 50.0, 20.0, 140.0 ],
"numinlets" : 1,
"bgcolor" : [ 0.94902, 0.94902, 0.94902, 0.0 ],
"numoutlets" : 1,
"outlettype" : [ "" ],
"size" : 256.0,
"id" : "obj-1"
}

}
, {
"box" : {
"maxclass" : "panel",
"patching_rect" : [ 118.0, 50.0, 21.0, 139.0 ],
"numinlets" : 1,
"grad1" : [ 1.0, 1.0, 1.0, 1.0 ],
"numoutlets" : 0,
"mode" : 1,
"grad2" : [ 0.509804, 0.509804, 0.509804, 1.0 ],
"angle" : 270.0,
"id" : "obj-8"
}

}
],
"lines" : [ {
"patchline" : {
"source" : [ "obj-29", 0 ],
"destination" : [ "obj-21", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-27", 0 ],
"destination" : [ "obj-21", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-7", 0 ],
"destination" : [ "obj-21", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-21", 0 ],
"destination" : [ "obj-26", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-23", 0 ],
"destination" : [ "obj-22", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-22", 0 ],
"destination" : [ "obj-21", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-4", 0 ],
"destination" : [ "obj-3", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-1", 0 ],
"destination" : [ "obj-6", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-6", 0 ],
"destination" : [ "obj-7", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-3", 0 ],
"destination" : [ "obj-7", 1 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-16", 0 ],
"destination" : [ "obj-15", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-10", 0 ],
"destination" : [ "obj-9", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-25", 0 ],
"destination" : [ "obj-24", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-15", 0 ],
"destination" : [ "obj-7", 2 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-9", 0 ],
"destination" : [ "obj-7", 3 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-24", 0 ],
"destination" : [ "obj-7", 4 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
, {
"patchline" : {
"source" : [ "obj-25", 0 ],
"destination" : [ "obj-28", 0 ],
"hidden" : 0,
"midpoints" : [ ]
}

}
]
}

}

Arduino code

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

void loop()
{

if (Serial.available() > 4)
{

int Motor1= Serial.read();
analogWrite (3, Motor1);

int Motor2 = Serial.read();
analogWrite (5, Motor2);

int Motor3 = Serial.read();
analogWrite (6, Motor3);

int Motor4 = Serial.read();
analogWrite (9,Motor4);

Serial.flush();
Serial.print(255, BYTE);
}

}