0

I want to ask is there a possible code to prioritize a specific part of a code? So I'm doing this project where I want to utilize muscle sensor, servo, and a button on an app over Bluetooth. The problem is since all is running in void() loop, even though I am not using the muscle sensor I can't move the servo with the app, since it reads the muscle sensor first. So is there a possible code to make the muscle sensor stop for a period of time after the button is pressed on the app and the servo is moved?

Here is the code:

void loop() 
 {
  if(Serial.available()>0)
   {
    char data = Serial.read();

      if (data == 'a')
       {
        myservo.write(15);
       }
  }



int value = analogRead(EMG_PIN);

 if(value > THRESHOLD)
  {
   myservo.write(15);
  }

  else{
       myservo.write(140);
      }

So the first code is a bluetooth code where the servo is controlled through a button on an app. The second one is a muscle sensor. As stated above the problem is it will always read the muscle sensor even though the button is pressed from the app.

#include <Servo.h> 

 int increment = 0;

 Servo myservo;

 void setup() {

   myservo.attach(1);

 }

 void loop() 
 {

   Serial.println(increment++);

   if (increment > 9);
    {

     if(Serial.available()>0)
      {
       char data = Serial.read();

       if (data == 'a')
       {
        myservo.write(15);
       }
 }

      else if (increment = 0);

      int value = analogRead(EMG_PIN);

      if(value > THRESHOLD)
       {
        myservo.write(15);
       }

     else{
       myservo.write(140);
      }
  }

closed as too broad by VE7JRO, sempaiscuba, Greenonline, Bra1n, MatsK Mar 29 at 14:57

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Your question is too vague to be answerable. The answer is "Yes, there's a way." Provide your current loop code, and highlight the relevant sections where you read the muscle sensor, and where the servo code doesn't get called. It sounds to me like a simple logic problem with your code but we can't tell without seeing the code. – Duncan C Mar 21 at 15:53
  • 2
    why do you need code to stop the muscle sensor? .... does it have a "stop" function? ....... why don't you simply read it only every10th iteration of loop()? – jsotola Mar 21 at 16:02
  • @DuncanC Ok guys I already edited my post, and putted in my code, i hope it will give you a better hindsight – Brilliant Purnawan Mar 21 at 23:20
  • @jsotola I'm new to this stuff, so I don't really know how to do it :), can you show me an example code from my code above, it would really help thx – Brilliant Purnawan Mar 21 at 23:22
  • @jsotola is it something like that, second code, im still new to thus so a bit confused – Brilliant Purnawan Mar 22 at 0:23
1

You have to think of your program as having two distinc modes of operation:

  • The "fixed servo" mode is entered when you press the button on the app. In this mode the servo is kept at a fixed position. The mode lasts for a predefined delay.

  • The normal (not fixed servo) mode is the default mode. In this mode the servo is positioned according to the reading of the sensor.

So you need a boolean variable to remember whether the "fixed servo" mode is active or not, and the code in loop() should take care of providing the correct behavior depending on the mode, and updating that variable when needed. For example:

void loop()
{
    static bool fixed = false;  // whether we are in "fixed servo" mode
    static uint32_t fixed_started;  // when the mode started
    uint32_t now = millis();

    // On button press, start "fixed servo" mode.
    if (Serial.available() && Serial.read() == 'a') {
        servo.write(SERVO_LEFT);
        fixed = true;
        fixed_started = now;
    }

    // Get out of "fixed" mode after some delay.
    if (fixed && now - fixed_started >= FIXED_DELAY) {
        fixed = false;
    }

    // If not in "fixed" mode, react to the sensor.
    if (!fixed) {
        if (analogRead(EMG_PIN) >= THRESHOLD) {
            servo.write(SERVO_LEFT);
        } else {
            servo.write(SERVO_RIGHT);
        }
    }
}
  • Ahh I see it worked perfectly thx Edgar – Brilliant Purnawan Mar 22 at 9:00
  • 1
    Well done, figuring out the OPs intent from that jumble of code. – Duncan C Mar 22 at 12:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.