We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to control servo by sending values from processing to arduino but whatever value i send within 0 to 180 degree it rotates only 90 degree,after first time 90 degree rotation it stood still...even if i run the code again then also......
Arduino Code:
String data;
#include <Servo.h>`> `
Servo myservo;
int i = 0;
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
if(Serial.available())
{
char c = Serial.read();
if(c == '\n')
{
parseCommand(data);
data = "";
}
else
{
data += c;
}
}
}
void parseCommand(String data1)
{
String X;
X = data1;
int pos = X.toInt();
for(i = 0; i<=pos; i += 1)
{
myservo.write(i);
}
Serial.println("X:"+X);
}
if i send values from the serial monitor of arduino then it works perfectly.
Processing code:
import processing.serial.*;
Serial myPort;
void setup(){
size(300, 300);
myPort = new Serial(this, "COM4", 9600);
}
void draw(){
int c = 45;
myPort.write(c);
delay(3000);
}**
this is the processing code..i'm changing the c value every time i run the code but it rotates 90.for one time only if its positon is 0 deg or less than 90 deg.
please help me to figure the problem out....
Answers
Maybe use a better subject line for your post.
Definitely format your code - edit the post, highlight the code, press ctrl-o. People can't fix what they can't read.
Hi priyabratabasu, I think on the Serial Monitor you type 45 Enter, and it sends 3 chars, 4,5,new-line. From your Procssing sketch 'write' sends 1 char ascii 45. Look at the help for Serial.write. Try sending a String '45\n' as a test.