I have a Makeblock xy plotter that I am trying to reprogram using Arduino code and Processing. I want Processing to send (via the serial port) an integer from 0-179 to actuate the servo; send 180, 181, and 182 to move the x-axis stepper one step back, one step forward, and to the home position, respectively; and 190, 191, and 192 to do the same with a y-axis stepper.
The servo is responding perfectly for values from 0-179. However, for all values Processing sends (once every 1 second) to the serial port from 180 to 3327, the x-axis stepper moves continuously in the negative direction. Above 3327 there is no movement. I am stumped.
I'm relatively new to this, so my approach may be flawed. If you have suggestions on that I'm all ears, but also want to be able to learn from this problem along the way. Thanks in advance for any ideas!
#include "MeOrion.h"
#include <SoftwareSerial.h>
MeStepper stepperX(PORT_1);
MeStepper stepperY(PORT_2);
MePort port(PORT_7); //Servo
Servo myservo1; // create servo object to control a servo
int16_t servo1pin = port.pin2(); //attaches the servo on PORT_3 SLOT2 to the servo object
int val = 0; // Data received from the serial port (Learning Note: when datatype is changed to char, anything over 127 doesn't map correctly...)
int width = 1; // Number of steps for 1 unit of desired plotter movement
void setup() {
myservo1.attach(servo1pin); // attaches the servo on servopin1
Serial.begin(9600); // Start serial communication at 9600 bps
stepperX.setMaxSpeed(1000);
stepperX.setAcceleration(20000);
stepperY.setMaxSpeed(1000);
stepperY.setAcceleration(20000);
}
void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if(val < 180){
myservo1.write(val); // Set the servo position
delay(15); // Wait for the servo to get there
} else if (val=180){
stepperX.move(-width); //move one step in negative x direction
stepperX.run();
} else if (val=181){
stepperX.move(width); //move one step in positive x direction
stepperX.run();
} else if (val=182){
stepperX.moveTo(0); //move to x direction home
stepperX.run();
} else if (val=190){
stepperY.move(-width); //move one step in negative x direction
stepperY.run();
} else if (val=191){
stepperY.move(width); //move one step in positive x direction
stepperY.run();
} else if (val=192){
stepperY.moveTo(0); //move to x direction home
stepperY.run();
} else{
//If unrecognized input, do nothing
}
}
PlotterControlFullServoRange_.ino (1.84 KB)