I'm trying to use two Bluetooth commands in my code: one is press off a button "1" the servo should do its routine without stop second one press of the a button "2" servo should stop.
What am I getting with my code is button 1 pressed servo is executing the routine only once and then I need to press again. But I want the routine to loop until I'll stop it.
I'm adding here the code:
#include <SoftSerial.h>
#include <TinyPinChange.h>
#include <SoftRcPulseOut.h>
#define SERVO_PIN 0
#define REFRESH_PERIOD_MS 4
#define NOW 1
int pos = 0;
SoftRcPulseOut myservo;
SoftSerial mySerial(2, 3); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
mySerial.begin(9600);
pinMode(greenPin, OUTPUT);
myservo.attach(SERVO_PIN);
}
void loop() // run over and over
{
char c = mySerial.read();
if(c == '1') selfservo();
if (c == '2') selfservo2();
}
ok the selfservo() is:
void selfservo()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for refresh period
SoftRcPulseOut::refresh(NOW); // generates the servo pulse Now
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(REFRESH_PERIOD_MS); // waits 20ms for for refresh period
SoftRcPulseOut::refresh(NOW); // generates the servo pulse Now
}
}
and selfservo2() is:
void selfservo2()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
}
}
if the button IS pressed then move the servo
.... change it toif the button WAS pressed then move the servo
... you do that by "remembering" if the button was pressed – jsotola Sep 14 '19 at 18:06receive data
function from theact on the data
function .... define aflag
variable such asbool move;
... the received data makes themove
variabletrue
orfalse
... do this at the beginning of theloop()
block .... later in the loop() block, perform actions depending on the value ofmove
, not on the value of received data ...... it is basically a simple state machine with two states – jsotola Sep 14 '19 at 18:24