Analog signal to enable DC motor for set time

Try it like this?

const int Pin1 = 12; // connected to pin 7 on the H-bridge
const int Pin2 = 11; // connected to pin 2 on the H-bridge
const int enablePin = 3;   // connected to pin 1 on the H-bridge
const int potPin = A0;  // connected to the potentiometer's output

int motorSpeed = 0; // speed of the motor
int threshold = 90;  // Move the motor when EMG signal is above this threshold. Remember it ranges 0–1023.
int oldState = 0;

unsigned long startTime;

void setup()
{
  Serial.begin(9600);

  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(enablePin, OUTPUT);


  //pinMode(motorPin, OUTPUT);
  //pinMode(sensorPin, INPUT);
}
//
void loop() {
  //int sensorState = digitalRead(sensorPin);
  int EMGsig = analogRead(A5);
  float voltage = EMGsig * (5.0 / 1023.0);

  if (EMGsig > threshold) {   //voltage must be tuned according to sEMG output
    if (oldState > threshold) {
      Serial.println("Button Pushed");
      digitalWrite(Pin1, HIGH);
      digitalWrite(Pin2, LOW);
      analogWrite(enablePin, motorSpeed);
    }   // turns the motor on
    startTime = millis();
  }
  oldState = EMGsig;
  if (digitalRead(Pin1 == HIGH)) {
    if (millis() - startTime >= 5000UL) {
      Serial.println("Timer Expired");
      digitalWrite(Pin1, LOW);
      digitalWrite(Pin2, HIGH);
      analogWrite(enablePin, 0);
    }
  }
  Serial.print(EMGsig); Serial.print(" EMG reading, with voltage: "); Serial.println(voltage);
  motorSpeed = analogRead(potPin) / 4;
}

-jim lee

1 Like