The encoder motor does not stop

Sorry to bother you, it was a very obvious problem.

Have you tried to set limits...

  if (stateA == stateB) {
    encoder1Pos--;
    if encoder1Pos < 15 // if less than limit
      encoder1Pos = 15; // remain at limit
  } else {
    encoder1Pos++;
    if encoder1Pos > 500 // if greater than limit
      encoder1Pos = 500; // remain at limit
  }

What did you observe in post #2? I wonder if setting both limits was needed. Can you test it and modify it for your needs?

Hello everyone! I used an encoder motor and used an emg sensor to control it. When there is a long compression, the motors rotate forward and stop when the encoder value exceeds 500, if there is a short compression, the motors rotate back to the encoder value of 15. I have motors, its encoder, emg signal, everything works fine, there is one problem, after exceeding the value of the encoder, the motor does not stop, can you please help?

#include <ELEMYO.h>
#define CSpin 10        // chip select pin
#define sensorInPin A1  // analog input pin that the sensor is attached to

int signalReference = 524;  // reference of signal, 2.5 V for MYO, MYO-kit, BPM, BPM-kit
//int signalReference = 369;  // reference of signal, 1.8 V for MH-BPS101 and MH-BPS102
long timer = 0;  // time length of EMG signal

const int motorPin1 = 7;  
const int motorPin2 = 6; 

const int encoderPin1A = 2;
const int encoderPin1B = 17;
const int sleepPin1 = 29;  

volatile long encoder1Pos = 0;
unsigned long EMGStartTime = 0;  
bool motorRunning = false;     

ELEMYO MyoSensor(CSpin);  // create ELEMYO object to work with signal
void setup() {
  Serial.begin(115200);         // initialize serial communications at 115200 bps
  MyoSensor.gain(x2);           // initial value of gain
  pinMode(sensorInPin, INPUT);  // initialize sensorInPin

  Serial.println("Type 1 - short muscle contraction");
  Serial.println("Type 2 - long muscle contraction");
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(encoderPin1A, INPUT_PULLUP);
  pinMode(encoderPin1B, INPUT_PULLUP);
  pinMode(sleepPin1, OUTPUT);
  digitalWrite(sleepPin1, HIGH);  
  attachInterrupt(digitalPinToInterrupt(encoderPin1A), updateEncoder1, CHANGE);
  moveMotorBackward1();
  delay(1500);
  stopMotor1();

  encoder1Pos = 0;  // Сброс энкодера
}

void loop() {
  Serial.println(encoder1Pos);
  int signalValue = analogRead(sensorInPin);              // read the analog in value:
  signalValue = MyoSensor.BandStop(signalValue, 50, 4);   // notch 50 Hz filter with band window 4 Hz.
  signalValue = MyoSensor.BandStop(signalValue, 100, 6);  // notch 100 Hz (one of 50 Hz mode) filter with band window 6 Hz

  int signalValueMA = MyoSensor.movingAverage(signalValue, signalReference, 0.92);  // moving average transformation with 0.92 smoothing constant

  // start timer if EMG amplitude is greater than the given threshold and it is new start of EMG signal
  if (signalValueMA >= 100 && timer == 0)
    timer = millis();

  // if EMG amplitude is bellow than the given threshold
  if (signalValueMA < 100 && timer != 0) {
    if ((millis() - timer) < 500)  // if EMG time length less then 0.5 c
    {
      Serial.println("Type 1");
      if (encoder1Pos > 15) {
        moveMotorBackward1();
      } else {
        stopMotor1();
      }
    } else  // if EMG time length more then 0.5 c
    {
      Serial.println("Type 2");
      if (encoder1Pos < 500) {
        moveMotorForward1();
      } else {
        stopMotor1();
      }
    }
    timer = 0;
  }

  delay(1);  // wait before the next loop
}
void updateEncoder1() {
  int stateA = digitalRead(encoderPin1A);
  int stateB = digitalRead(encoderPin1B);

  if (stateA == stateB) {
    encoder1Pos--;
  } else {
    encoder1Pos++;
  }
}

void stopMotor1() {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
}

void moveMotorBackward1() {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
}

void moveMotorForward1() {
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
}

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