Toggling two servos controlled by EMG - help with code

Hey everyone, I tried watching every YT vid on the toggling of the pushbutton, in Arduino. I read through this forum seeking an answer, but no luck.

It's worth mentioning that I'm an absolute beginner and I've been stuck with it for a couple of weeks.

This is my problem: I manage to control my two servos with EMG signals from my muscles.

The thing I can't figure out is how to create a START/STOP feature in the code. When pressed to START, servos react to EMG. When pressed STOP they go in a certain position.

Below is code I was able to patch up. I know I'm missing buttonState and all, but don't know what to fit where.

#include <Servo.h>
#define BTN 4

Servo myServo;
Servo myServo1;

int EMG, EMG1; // Create a variable called EMG
int ServoPin = 6;
int ServoPin1 = 2;
int angle, angle1; // Create a variable called ServoPin

void setup() {
myServo.attach(ServoPin);
myServo1.attach(ServoPin1);
pinMode(BTN, INPUT);
}

void loop() {

if () {
myServo.write(80);
myServo1.write(50);
}

else if (){
EMG = analogRead(A0); // Read from Analog port 0 and copy the value to EMG
angle = map(EMG, 300, 800, 70, 100);
myServo.write(angle);

EMG1 = analogRead(A1);
angle1 = map(EMG1, 200, 700, 40, 80);
myServo1.write(angle1);

}

}

Any help is incredibly appreciated!!!

Hello,
try this untestet sketch.

#include <Servo.h>
#define BTN 4
enum {Stage1, Stage2};
struct BUTTON {
  byte pin;
  byte pinmode;
  bool state;
  int stage;
  unsigned long stamp;
  unsigned long debounce;
} button {BTN, INPUT, 0, Stage1, 20, 20};
Servo myServo;
Servo myServo1;

int EMG, EMG1; // Create a variable called EMG
int ServoPin = 6;
int ServoPin1 = 2;
int angle, angle1; // Create a variable called ServoPin

void setup() {
  myServo.attach(ServoPin);
  myServo1.attach(ServoPin1);
  pinMode(button.pin, button.pinmode);
}

void loop() {

  if (millis() - button.stamp >= button.debounce) {
    button.stamp = millis();
    bool state = digitalRead(button.pin);
    if (button.state != state) {
      button.state = state;
      if (state) {
        button.stage++;
        button.stage = button.stage % 2;
      }
    }
  }

  switch (button.stage) {
    case Stage1:
      myServo.write(80);
      myServo1.write(50);
      break;
      
    case Stage2:
      EMG = analogRead(A0); // Read from Analog port 0 and copy the value to EMG
      angle = map(EMG, 300, 800, 70, 100);
      myServo.write(angle);
      EMG1 = analogRead(A1);
      angle1 = map(EMG1, 200, 700, 40, 80);
      myServo1.write(angle1);
      break;
  }
}

Hey @paulpaulson, I can't thank you enough mate! You're F AWESOME! I can't stress enough how much I was banging my head to get this to work out.

I've attached a drive link of how it acts and what does it look like.

Is there any possible way to smooth out those servos?

Hello
this may help to filter the raw sensor data:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.