1

Here is my objective:

If the value of EMGv is at or above 900, start moving the motors left. The motors should continue to move left for a maximum of one second, provided EMGv stays at or above 900. If during this time EMGv goes below 900, the motors should move right for one second. Then the motors should stop. At any time after this should EMGv go above 900 again, the process should repeat.

In simpler terms, the project is a robotic hand, wired to an EMG circuit. When the EMG signal gets above 900 the hand should close by the motors all turning left for one second and when the signal goes below 900 the hand should open by the motors turning right for one second.

Here is my loop atm when move(motor #, speed, direction (1 for left, 0 for right)) is the function to move the motors and stop() stops all motors

void loop(){

  unsigned long starttime; //time limit for hand movement
  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 {
      stop();
    }
  }
}
  • Perhaps you can edit this question to incorporate some examples and the specification you wrote for a later question. – James Waldby - jwpat7 Feb 3 '17 at 17:49
1

I suspect you would do well to first write (in English) the exact set of rules you want it to follow -- don't worry about the code to begin with - get it clear in your head what you want the code to do. (If this is already clear to you, then just write it :-) )

Here's what I think you might mean -- although it's not exactly what you wrote.

If EMG is at or above 900, start moving the motors left. The motors should continue to move left for a maximum of one second, provided EMG stays at or above 900. If during this time EMG goes below 900, the motors should move right for one second. Then the motors should stop.

Once you have that, turning it into code looks well within your ability, judging by the code you've posted. Here's my attempt - note that it'll only be correct if I have correctly stated your desired rules. (And if I've correctly guessed how the move API works - I guess it starts the motor moving, that you need to cancel it to stop it moving. Please say if this is wrong.)

I've used LEFT and RIGHT instead of 1 and whatever, for clarity.

void loop(){
  // If EMG is at or above 900
  int EMGv = analogRead(EMG);
  if (EMGv >= 900) 
  {
    unsigned long starttime = millis();

    // Start the motors moving left
    move(0, 255, LEFT); //motor 1, full speed, left
    ...and the other motors...

    // Continue for a maximum of one second, and while EMG stays at or above 900
    while ( (millis()-starttime < 1000) && ((EMGv = analogRead(EMG)) >= 900)) {
      // Just wait
      delay(1);
    }

    // If during this time EMG goes below one second...
    if (EMGv < 900) {
      // ...the motors should move right for one second
      move(0, 255, RIGHT); //motor 1, full speed, right
      ...and the other motors...
    }

    // Then the motors should stop.
    move(0, 0, LEFT); //motor 1, stop, left
    ...and the other motors...
  }

// More stuff here, otherwise it will immediately go round the loop again and start moving again
}
  • If you want to know what exactly he is trying to do, look at his previous question. – gre_gor Nov 25 '16 at 14:32
  • I did, and I get the gist of it, but I still wasn't confident I understood exactly what was wanted. – Mark Smith Nov 25 '16 at 15:03
  • Thank you for your help! Ive uploaded this code and what it seems to do at the moment is turn the motors forward when the value is above 900 but it does not limit that to one second nor does it reverse them when the value falls below 900. I'm gonna work on debugging but if you have any idea why please let me know! Thanks. – Zach Eisner Nov 25 '16 at 18:45
  • My suspicion is that I incorrectly guessed how to switch the motors off. My guess was move(0,0,LEFT);. Is that correct? Also, I just fixed a bug which would have prevented the reverse from working. – Mark Smith Nov 25 '16 at 18:59
  • ok ill try it thank you. I suspect an easy way to run the reverse would be: If EMG goes from above 900 to below 900, run reverse for one second. Im just not sure what the Arduino code for that is. – Zach Eisner Nov 25 '16 at 19:26

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.