1

I am currently building a prosthetic hand using an Arduino MEGA, an Advancer Tech Myoware sensor, and 3 TB6612FNG Dual H-bridge breakouts. I have the EMG sensor sending an analog value to the Arduino and want to use that value to open or close the hand.

The problem I have is that the motors cannot turn more than 1 second without tightening too much and breaking the hand, so I want to place a limit on the while loop telling them to move that will restrict their movement under the specified conditions to one second. Also, when the analog voltage is no longer present, I need the motors to reverse their movement so the hand releases.

Here is the code I have where move(motor #, speed, direction) is the function to move the hand (1 = forward, 2 = backward).

void loop(){

  int EMGv = analogRead(EMG);

  while(EMGv >= 900){
    move(0, 255, 1); //motor 1, full speed, left
    move(1, 255, 1); //motor 2, full speed, left
    move(2, 255, 1); //motor 3, full speed, left
    move(3, 255, 1); //motor 4, full speed, left
    move(4, 255, 1); //motor 5, full speed, left  

  }

To reiterate. I need the motors to move while the EMG value is over 900, BUT not move more than 1 second, even if the value is 900 for 5 seconds. I then need the motors to move one second backwards when the value dips below 900.

Thanks!

2

You can use millis() feature

unsigned long starttime;

void loop(){

  int EMGv = analogRead(EMG);
  starttime=millis();
  while(EMGv >= 900){
    if (millis()-starttime < 1000) //limit on 1000ms
    {
    move(0, 255, 1); //motor 1, full speed, left
    move(1, 255, 1); //motor 2, full speed, left
    move(2, 255, 1); //motor 3, full speed, left
    move(3, 255, 1); //motor 4, full speed, left
    move(4, 255, 1); //motor 5, full speed, left  
    } else {/*turn off motor here*/}
  }
  • Thank you! Can you explain what the millis funtion does? – Zach Eisner Nov 24 '16 at 18:32
  • Also is there a way to reverse this movement as soon as the value dipps below 900 – Zach Eisner Nov 24 '16 at 18:40
  • Sorry for the questions. One more thing. This code only allows the motors to turn once. How do I reset the function after the value dips below 900 so it can be activated again when the value rises for a second time? – Zach Eisner Nov 24 '16 at 19:48
  • For short, millis is the time on your Arduino since it start. More information can be found on arduino.cc/en/Reference/Millis. As for reversing the movement, you can do some trial with the algorithm by yourself. The function will reset if the EMG value drops under 900, so it will break the while-loops – dhimaspw Nov 28 '16 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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