Hi,
i want to control the speed of dc motor in arduino with a processing sketch.
i already tried every i can think of but something is still missing and i ask your help must be something pretty simple…maybe im not using the right aproach but i think like this its possible to work.
both codes are here below with some coments,hope someone can detect where the problem is…thank you very much!!
arduino code: (where i think problem is)
notes:
-when in processing value is 0 it doesnt move and with multimeter reading is 0 (so serial comunication is working)
-than with any value i send in processing i only can measure 0.50v in pin 9 and ear noise from motor if i force spin it starts and goes up to 1.8 v but no more.
so aparently serial comunication is working but basically i cant understand why im not being able to get analogWrite from 0 to 5v…
int leitura;
int vel;
const int motor=9;
void setup()
{
Serial.begin(9600);
pinMode(motor,OUTPUT);
}
void loop()
{
if(Serial.available())
{
leitura=Serial.read();
vel=map(leitura,0,255,0,1023);
if(leitura>0)
{
analogWrite(motor,vel);
delay(1);
}
else
{
analogWrite(motor,LOW);
}
}
}
processing code:
notes:
-code is a mess!! im just trying every step to have a fully rc car interface with processing,next step is puting everything working in the same code.
-just pay atention to motor part please
import processing.serial.*;
Serial myPort;
int a=0;
int b=1;
int l1=240;
int l2=250;
int x=105;
int x1;
int y=355;
int y1;
int vel;
void setup()
{
size(400, 400);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
//led test------------------------
/*
a=a-b;
a=constrain(a,0,1);
if (a==1)
{
rectMode(CORNER);
background(255);
fill(50);
rect(50, 50, 100, 100);
myPort.write(l1);
println(l1);
}
else if (a==0)
{
background(255);
fill(125);
rect(50, 50, 100, 100);
myPort.write(l2);
println(l2);
}
//servo---------------------
rectMode(CORNER);
stroke(0);
fill(0);
rect(100,190,190,20);
stroke(0);
fill(255);
rect(x,190,10,20);
if(mouseX>97 && mouseX<292 && mouseY > 189 && mouseY< 211 && mousePressed)
{
x=mouseX;
x=constrain(x,100,280);
x1=x-100;
println(x1);
myPort.write(x1);
}
*/
//motor--------------------------
rectMode(CORNER);
stroke(0);
fill(0);
rect(300, 100, 20, 265);
stroke(0);
fill(255);
rect(300, y, 20, 10);
if (mouseX>299 && mouseX<321 && mouseY > 97 && mouseY< 368 && mousePressed)
{
y=mouseY;
y=constrain(y, 100, 355);
y1=abs(y-355);
}
println(y1);
myPort.write(y1);
}
/*
void mousePressed()
{
if (mouseX>49 && mouseX<151 && mouseY>49 && mouseY<151)
{
b=b*-1;
}
}
*/
both y and y1 are int variables. below you can see how the sketch works
thank you!!cumps