Arduino using Processing won't start.

Hi everyone.

Alright, I am a new to Arduino and for one of our school project we need to use both Arduino and Processing interacting with each other. The processing part is OK but I am struggling to get the Arduino code working as I want it to be. I am using a continuous rotating servo and an Arduino uno board.

First I'll show you my code on processing

#include <Servo.h> 

Servo myservo;
char val; 

void setup() { 
  myservo.attach(9);
  Serial.begin(9600); 
 if (Serial.available()) { 
 val = Serial.read(); 
   myservo.writeMicroseconds(1500);
 }
 if (val == 'H') {
    myservo.write(90);  
  delay(1000);
  myservo.writeMicroseconds(1500);

 }

void loop() {

}

Now in the code, the myservo.write(90) makes it spin slowly clockwise, and the myservo.writeMicroseconds(1500) stops the servo from rotating.

At first, I tried it by putting everything in the void loop section but it kept spinning all day even with the serv.detach(), but it was working and received data from Processing.
Then I decided to put it in the void setup and it is not receiving data or triggering the servo anymore. I kind of know where is the problem because when I remove the if(val == 'H') { part , the servo does its spin and then stop when I reset it or upload the code.

So to summarize, I have no idea why it is not working, so If you guys could tell me where and what did I do wrong, or if you have another way to do it, I would be glad to hear it.

Many thanks !

Are you using the Processing language on your PC to control your Arduino or by 'processing' do you mean something else?

The code you posted looks like Arduino. I suggest that you move the serial receive and action part into loop() and when you do move the servo, set val to something besides H so that val triggers the servo move only once instead of over and over.

Thank you for your answer.

Yes by processing I mean the software Processing. I am meant to use it to activate the servo through Arduino.

I don't understand what you mean by set val to something besides H though. When I tried to put serial receive and action part into loop() it was just looping indefinitely. Perhaps you could give me an example of how you would do it?

When I tried to put serial receive and action part into loop() it was just looping indefinitely. Perhaps you could give me an example of how you would do it?

You've got the problem. You show us how you did it. Use Tools + Auto Format before you post any more abominable looking code.

eb8978:
Thank you for your answer.

Yes by processing I mean the software Processing. I am meant to use it to activate the servo through Arduino.

I don't understand what you mean by set val to something besides H though. When I tried to put serial receive and action part into loop() it was just looping indefinitely. Perhaps you could give me an example of how you would do it?

It's supposed to loop indefinitely. XD
That is how over time new inputs from the PC will be noticed and acted upon. That is how real time code works.

What your program does wrong however is not turning the switch (val) off (not 'H') when it moves the servo.

This way below waits for Serial to have an 'H' and acts on it only one time per 'H' received:

if ( Serial.available() )
{
val = Serial.read();
}

if ( val == 'H' )
{
move the servo
val = 0; // so next time through loop this runs ONLY if another 'H' is in Serial
}

If you leave val == 'H' then yes it will keep moving the servo because your code makes it so.

Servo test code that might be useful in determining desired writemicroseconds values for your servo.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

Thank you GoForSmoke. Indeed adding the line val = 0 stop the servo from spinning indefinitely. You're a lifesaver ! My code is properly working now :slight_smile:

Just please tell me that you understand WHY!