Analog signal to enable DC motor for set time

I am currently working on a prosthetic hand and I need a normal brushed DC motor to be able to spin for a set amount of time at a set speed. I am using arduino uno and L293d H-bridge. The control of the motor is based on a signal acquired from an sEMG sensor. I am new to working with arduino and would appreciate some help.

When I run the code, I receive these error messages:

move_motor_for_set_time:49:3: error: ‘oldState’ does not name a type
oldState = EMGsig;
^~~~~~~~
move_motor_for_set_time:50:3: error: expected unqualified-id before ‘if’
if (digitalRead(Pin1 == HIGH))
^~
move_motor_for_set_time:61:3: error: ‘Serial’ does not name a type
Serial.print(EMGsig);Serial.print(" EMG reading, with voltage: “);Serial.println(voltage);
^~~~~~
move_motor_for_set_time:61:24: error: ‘Serial’ does not name a type
Serial.print(EMGsig);Serial.print(” EMG reading, with voltage: “);Serial.println(voltage);
^~~~~~
move_motor_for_set_time:61:69: error: ‘Serial’ does not name a type
Serial.print(EMGsig);Serial.print(” EMG reading, with voltage: ");Serial.println(voltage);
^~~~~~
move_motor_for_set_time:63:3: error: ‘motorSpeed’ does not name a type
motorSpeed = analogRead(potPin) / 4;
^~~~~~~~~~
move_motor_for_set_time:65:1: error: expected declaration before ‘}’ token
}
^
exit status 1
‘oldState’ does not name a type


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;
  
}```

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

You forgot to do an auto-format before posting. If you did that you might have noticed that the line “oldState = EMGsig;” is outside the loop() function, either because you are missing a ‘{’ or have an extra ‘}’.

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))
1 Like

The code now uploads, but the motor still doesn’t run. The serial monitor verifies that the EMG signal is exceeding the threshold. Any suggestions?

Did you leave in this mistake?
if (digitalRead(Pin1 == HIGH))

The paren is in the wrong place:
if (digitalRead(Pin1) == HIGH)

made a fix to the if statements and got it working perfectly. here is my code in case anyone can use it in the future`

//const int sensorPin = 2; // the number of the sensor pin
//const int motorPin = 13; // the number of the motor pin

const int Pin1 = 8; // connected to pin 7 on the H-bridge
const int Pin2 = 9; // connected to pin 2 on the H-bridge
const int enablePin = 13; // 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 = 60; // Move the motor when EMG signal is above this threshold. Remember it ranges 0–1023.
int oldState;

unsigned long startTime;

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

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

digitalWrite(enablePin, LOW);

//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 > EMGsig)
{
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(enablePin, motorSpeed);

  }  
  startTime = millis();
} 

oldState = EMGsig;
if (digitalRead(Pin1 == HIGH))
{
if (millis() - startTime >= 5000UL)
{
digitalWrite(Pin1, LOW);
digitalWrite(enablePin, LOW);

}

}

delay(1000);
motorSpeed = analogRead(potPin) / 4;
Serial.print(EMGsig);Serial.print(" EMG Signal; “);Serial.print(voltage); Serial.print(” Voltage; “); Serial.print(motorSpeed); Serial.println(” Motor Speed; ");

}`