correct value is not sent to arduino from processing for servo -where is the error in my code?

edited May 2017 in Arduino

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

  • what to do??

    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.

Sign In or Register to comment.