I have created an Android app using the MIT App Inventor and am trying to control the speed of the motor using a slider within the app. The range is set from 0 to 255.
Watching the results in the Serial Monitor shows that information is being sent to the Arduino, but instead of, for example, showing 186, it shows it as three separate lines of information; 1, then 8, then 6.
I have attached images to show the monitor and how I set up the app in MAI.
I assume I am probably sending the information over in the wrong format.
Any help would be greatly appreciated.
Also, forgive my horrible syntax, I am still learning
#include <AccelStepper.h>
#include <SoftwareSerial.h>
AccelStepper stepper(AccelStepper::FULL2WIRE, 8, 9);
// Define our three input button pins
#define button1 3
#define button2 2
int spd = 1000; // The current speed in steps/second
int sign = 0; // Either 1, 0 or -1
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(5000);
stepper.setSpeed(5000);
// Set up the three button inputs, with pullups
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
Serial.println("APP STARTED");
}
void loop()
{
char c;
if(Serial.available()) {
c = Serial.read();
if (c == 'f') { // forward
sign = 1;
}
if (c == 'r') { // reverse
sign = -1;
}
if (c == 's') { // stop
sign = 0;
}
if (c == '1') { // super slow
spd = 100;
}
if (c == '2') { // slow
spd = 1000;
}
if (c == '3') { // medium
spd = 2000;
}
if (c == '4') { // fast
spd = 3000;
}
if (c == '5') { // faster
spd = 4000;
}
if (c == '6') { // superfast
spd = 5000;
}
if ((c != 'f') && (c != 'r') && (c != 's') && (c != '1') && (c != '2') && (c != '3') && (c != '4') && (c != '5') && (c != '6')){
spd = c; //* 19.6;
}
Serial.println(c);
}
// when the slider hits the button, it sends it back in the reverse direction
if (digitalRead(button1) == 0) {
sign = 1;
}
// when the slider hits the button, it sends it back in the reverse direction
else if (digitalRead(button2) == 0) {
sign = -1;
}
stepper.setSpeed(sign * spd);
stepper.runSpeed();
}