EMG Sensor to control Servo motor

I want to start a continuous servo if the value of emg sensor goes above 600, stop the servo when the value is less than 600 and start the servo in reverse direction when the value goes above 600 for the second time. I want this to continue. Forward on third and reverse on fourth and so on. Can anyone please guide me on how I can implement this in arduino code?
Thank You
Edit:
Sorry I should have uploaded my code.
Here it is

#include <Servo.h>
Servo myservo;
#define led 9
int val = 90;
int a=0;
// the setup routine runs once when you press reset:
void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 pinMode(led, OUTPUT); //set pin as output , red led
analogWrite(led,val);
myservo.attach(9);
}

// the loop routine runs over and over again forever:
void loop() {
 // read the input on analog pin 0:
 int sensorValue = analogRead(A0);
 // print out the value you read:
 Serial.println(sensorValue);
 delay(1);        // delay in between reads for stability
if(sensorValue>600 && a==0){
 myservo.write(80);
 a=1;
 }
 else {myservo.write(90);}
 if(sensorValue>600 && a==1)
 {
   myservo.write(100);
   a=0;
   }
}

The problem is that the motor rotates only in 1 direction. I want it to rotate in forward and reverse direction alternatively whenever the value of EMG goes over 600 and stops the motor when the value is less than 600.

What have you tried? This forum helps people who have tried and are having issues. Read the posting guidelines at the top of the forum. Post your code. Post any errors you are getting. State what you expect vs. what is happening. If you want people to write code for you, go over to the Gigs and Collaborations forum and hire a programmer to do it for you. Expect to pay $$$.

The code is only executing the second if command for some reason. Whenever the value goes over 600 myservo.write(100) executes. Can anyone please find a fault in the program logic.
Thank you

Do you really mean, "stop the motor", or do you mean, "set the servo to 90"? You have to explain the 80,90,100 servo parameters so we know what they are supposed to do.

aarg:
Do you really mean, "stop the motor", or do you mean, "set the servo to 90"? You have to explain the 80,90,100 servo parameters so we know what they are supposed to do.

By stop I mean set servo to 90, forward is setting the servo to 80 and reverse is setting the servo to 100

sgr41:
The problem is that the motor rotates only in 1 direction. I want it to rotate in forward and reverse direction alternatively whenever the value of EMG goes over 600 and stops the motor when the value is less than 600.

How do you know how fast the servo is moving? If you want it to alternate back and forth, you need to introduce some timing delay so the servo has a chance to reach say, 100, before it goes back to 80 and so on. I think you need to really sit yourself down and work out a detailed plan before you continue to program. If you have a really detailed picture of how things are supposed to behave, the programming part often comes relatively easily.

aarg:
How do you know how fast the servo is moving? If you want it to alternate back and forth, you need to introduce some timing delay so the servo has a chance to reach say, 100, before it goes back to 80 and so on. I think you need to really sit yourself down and work out a detailed plan before you continue to program. If you have a really detailed picture of how things are supposed to behave, the programming part often comes relatively easily.

Let's consider that i am using a switch to control a continuous servo motor. I want the motor to start rotating in forward direction i.e. myservo.write(100) when I press the switch , when I release the switch the motor should stop i.e. myservo.write(90) , then when I press the switch again the motor should rotate in backward direction i.e. myservo.write(80), there is no need for delay as the motor is not rotating forward and backwards simultaneously.
I hope you understood what I want to say.

Okay, are we just considering something as a switch, or is it an actual switch?

So, as I understand it... you want the servo to be 90 normally (in the idle state). If you push the button, it should rotate in some direction until the switch is released. When the switch is released, the servo should stop (90 again), set the direction to opposite, and wait for another button press.

Have I got it?

Can you explain why you are using an analog value to read a switch?

If you are just using a button, then check out using a toggle function to switch between the rotating directions each time the button is depressed. You might also look at setting sequentially rotating flags to determine what is the next action of the servo.

aarg:
Okay, are we just considering something as a switch, or is it an actual switch?

So, as I understand it... you want the servo to be 90 normally (in the idle state). If you push the button, it should rotate in some direction until the switch is released. When the switch is released, the servo should stop (90 again), set the direction to opposite, and wait for another button press.

Have I got it?

Can you explain why you are using an analog value to read a switch?

Yes , exactly but instead of the button it is a sensor..so the sensor value going over 600 is analogous to the switch being pressed.

zoomkat:
If you are just using a button, then check out using a toggle function to switch between the rotating directions each time the button is depressed. You might also look at setting sequentially rotating flags to determine what is the next action of the servo.

No, I just said button to explain it,I am going to use either an emg sensor or a force sensitive resistor, in both cases i want the motor to start when the value is more than 600.

holdingpattern:
Posting a working sketch, mainly because I saw this:

//  https://forum.arduino.cc/index.php?topic=668593

// 6 mar 2020

// check if pot has just gone over a threshold
int threshold = 600;
int potVal;
int potValPrev;
byte potPin = 0;
bool directionFlag;

#include <Servo.h>
Servo myservo;

void setup()
{
  Serial.begin(9600);
  Serial.println(“setup() starting…”);
  Serial.println(“check if pot has just gone over a threshold, control servo”);
  myservo.attach(9);
  Serial.println(“setup() done…”);
  delay(1000);
}

void loop()
{
  potVal = analogRead(potPin);
  //Serial.println(potVal);
  if (potValPrev < 600 && potVal > 600)
  {
    Serial.print("direction flag changed to ");
    directionFlag = !directionFlag;
    Serial.println(directionFlag);
    if (directionFlag)
    {
      //make servo go one way
      Serial.println(“servo 50”);
      myservo.write(50);
    }
    else
    {
      //make servo go other way
      Serial.println(“servo 130”);
      myservo.write(130);
    }
  }

if (potVal < 600)
  {
    //make servo stop
    Serial.println(“servo 90”);
    myservo.write(90);
  }
  potValPrev = potVal;
}




No charge ;)

~~$$$~~

Thank You very much. @blh64 was right I should have posted my code . I will learn more if I try to improvise my code. :stuck_out_tongue:

holdingpattern:
Improvise? I'm hoping that's a typo :wink: