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.