0

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 
  {                                

  } 
} 
  • look at your code ... it says if the button IS pressed then move the servo .... change it to if the button WAS pressed then move the servo ... you do that by "remembering" if the button was pressed – jsotola Sep 14 at 18:06
  • @jsotola tell me more please! how should it look like? – Andy Andy Sep 14 at 18:08
  • separate the receive data function from the act on the data function .... define a flag variable such as bool move; ... the received data makes the move variable true or false ... do this at the beginning of the loop() block .... later in the loop() block, perform actions depending on the value of move, not on the value of received data ...... it is basically a simple state machine with two states – jsotola Sep 14 at 18:24
  • That's precisely how I would have described it: you need a variable that indicates whether or not the servo should be rotating, toggled by the button(s). (Two buttons seems overkill, but there may be a specific UX need for this.) – Dave Newton Sep 15 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.