Communicating with Processing IDE

I am working with a stepper motor. I have my Arduino code:

/*

*/

#include<AccelStepper.h>
// setting, steppin, dirpin
AccelStepper stepper(1, 2, 3);

// set up temp values
int _speed;
int _running;
bool fb;

void setup() {
  Serial.begin(115200);
  stepper.setMaxSpeed(10000);
  stepper.setAcceleration(10000);
  stepper.setSpeed(1000);
  //initially set _speed as the current speed
  _speed = stepper.speed();
}

void loop() {
  if (Serial.available() > 0) {
    //read value from serial port then save it to serial_read
    String serial_read = Serial.readStringUntil('\n');
    //parse serial read into a function and command
    String function = serial_read.substring(0, serial_read.indexOf(" "));
    String command = serial_read.substring(serial_read.indexOf(" ") + 1);
    int function = Serial.read();
    // test for the different functions
    // then get the command and execute
    if (function == "stop") {
      _running = 0;
      stepper.stop();
      stepper.setSpeed(0);
    } else if (function == "speed") {
      if (fb) {
        _speed = abs(command.toInt()) * -1;
      } else {
        _speed = abs(command.toInt());
      }
    } else if (function == "moveto") {
      _running = 1;
      stepper.moveTo(command.toInt());
    } else if (function == "move") {
      _running = 1;
      stepper.move(command.toInt());
    } else if (function == "runforward") {
      _running = 2;
      fb = false;
      _speed = abs(_speed);
      stepper.setSpeed(abs(stepper.speed()));
    } else if (function == "runforward") {
      _running = 2;
      fb = true;
      _speed = abs(_speed) * -1;
    } else if (function == "getspeed") {
      stepper.setSpeed(_speed);
      Serial.println(abs(stepper.speed()));
    } else if (function == "getposition") {
      Serial.println(stepper.currentPosition());
    }



  }
  if (_running == 1) {
    stepper.run();
  } else if (_running == 2) {
    stepper.setSpeed(_speed);
    stepper.runSpeed();
  }
}

It works just fine with the built in Arduino serial monitor but when I try to send data from Processing instead nothing happens. My Processing code:

import processing.serial.*;
Serial Arduino;

void setup() {
  size(600, 600);

  Arduino = new Serial(this, "COM4", 115200);
}

void draw() {
  background(0);

  Arduino.write("runforward");
}

Thank you for your time.

You are trying to use the same port on your Arduino twice. Once for receiving stuff from processing and twice when you try and use serial print.

Shut down the serial monitor, and read the data from processing and light an LED when you receive anything.

Then work on doing something useful with the data you get from processing that doesn't involve serial print.

Moved your post to a better / correct forum section.

So I need to create a different instance of the serial monitor?

No. You get one or the other if using only the one serial port. Processing effectively highjacks the port preventing the Arduino serial monitor from connecting. So if you are just troubleshooting the motor in serial monitor, typing in you commands there, you will see the results of this as you wanted to there, or you could read those values in using Processing and see them there as in the SerialCallResponse example in Processing.

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