Controling a Servo using Processing & Arduino

Good day,

Im a bit of a novice and for a school project we are required to make a servo move using Processing and using port.write('value'); to send data from processing to arduino ide.

Would anyone be able to share some insight into what is wrong with my code ?

#include <Servo.h>

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

Servo myservo;


char val;

void setup() {
 
  Serial.begin(9600);
  myservo.attach(9);
  
 
}

void loop() {
  if (Serial.available()) {
    // read the incoming byte:
    val = Serial.read();
  }

    if (val == '1') { // 1 = value that is comming from processing
  
    myservo.write(100);
    delay(1000);
  
  }
  else if (val == '2' ){
   
    myservo.detach()
    
  }
}

My processing code is simple,

void mousePressed() {
  if(mouseX > 500){
  port.write('1');
  print("yes");
  }
  else if (mouseX < 500){
    port.write('2');
    print("no");
  }
  
}

Just sending a 1 or 2 when the mouse is pressed at a certain position on the Canvas. My port assignment and Com is all correct i just dont understand why Arduino is not reading the incoming bytes.

Thanks for your help !

Fixed .

Did you test your Arduino code using the serial monitor?

Why do you think Arduino doesn't read incoming bytes? To be sure, do as @anon73444976 advised you, but I think it might be more convenient to use the LCD display (you have #include <LiquidCrystal_I2C.h> in the program). For me, Arduino does what you program it - nothing. Oh, yes, when the Arduino first gets 1 it will move the servo motor to position 100 and that's it. That's what your program does. What you want Arduino to do is another matter and you have not explained.

What does it do that you don't expect? Or what doesn't it do that you expect it to do?

The tricky bit here is you can not run the arduino serial monitor and processing at the same time, since you are connecting to the serial monitor. I appreciate the feedback.

Im trying to make a the 360 servo rotate, i attached a screw to it, and im trying the have the screw turn x degrees, stop, turn the otherway, etc etc, thanks for the response

The suggestion is that you dispense with processing for now so you have a single piece of code to debug.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.