I am working on a project to wirelessly control a continuous rotation servo...something like a wireless vending machine. The problem I am having is controlling how many revolutions the servo is making. During testing I used a button and had no issues getting the delay to work properly. Now that it's connected to the HC-12 RF module I can't get the delay to control how many rotations the servo makes. The servo I'm using is the FS90R.
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial mySerial(2, 3); // RX, TX
Servo badservo;
int pos = 92;
unsigned long last = millis();
void setup() {
mySerial.begin(9600);
badservo.attach(9);
badservo.write(pos);
}
void loop() {
if(mySerial.available() > 1){
int input = mySerial.parseInt();//read serial input and convert to integer (-32,768 to 32,767)
int servpos = badservo.read();
if(millis() - last > 250){
if(servpos == 92 && input == 1234){
badservo.write(0);
delay(470);
//mySerial.println(8894);
}
else{
badservo.write(pos);
//mySerial.println(8894);
}
}
mySerial.flush();//clear the serial buffer for unwanted inputs
last = millis();
}
delay(20);//delay little for better serial communication
}
With the servo that I'm using has a stop position of 92, meaning when I do servo.write(92); it will stop the servo. Also, the 470 delay was tested with a button and was a single full rotation.
Now, with the HC-12 module, I can't get the servo to rotate less than 2.5 times. Anyone have any idea what I'm doing wrong? The goal is to get the servo to rotate only once when the RF module receives the proper signal.
flush()
either - what you want, is sound event-driven code. – Chris Stratton Jun 12 '17 at 16:43