Servo Not Responding on toggle switch

Hey guys, I need some help. My servomotor moves when I am adjusting the potentiometer. But it doesn't move(left, right middle) when I use the toggle switch. What's wrong in my code?

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

//Toggle Switch
int toggleA = 5;
int toggleB = 10;

//Toggle Switch Status
int toggleAstatus;
int toggleBstatus;


//Center position (1500usec)
const int center = 1500;


//angle added for 600us
int x = 60;


void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object


  //Serial Monitor
  Serial.begin(9600);
  Serial.println("=====Servo Test======");


  //Toggle Switch pinMode
  pinMode(toggleA, INPUT_PULLUP);
  pinMode(toggleB, INPUT_PULLUP);

  //Servo to center position
  myservo.writeMicroseconds(center);
  Serial.println("Servo to center position");



}

void loop() {
  toggleswitch();
}







void toggleswitch()
{
  //Toggle Switch Status Reads HIGH/LOW
  toggleAstatus = digitalRead(toggleA);
  toggleBstatus = digitalRead(toggleB);


  //Toggle Switch Positioning Control of Servo
  //(LEFT, CENTER, RIGHT)
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value


  if (val >= 20 && val <= 80, toggleAstatus == HIGH && toggleBstatus == HIGH)
  {
    Serial.println("Servo Adjusted to:");
    Serial.println(val);
    myservo.write(val);
  }
  if (val >= 20 && val <= 80, toggleAstatus == LOW)
  {
    Serial.println("Servo adjusted to:");
    Serial.println(val + x);
    myservo.write(val + x);
  }
  if (val >= 20 && val <= 80, toggleBstatus == LOW)
  {
    Serial.println("Servo adjusted to:");
    Serial.println(val - x);
    myservo.write(val - x);
  }
}

  if (val >= 20 && val <= 80, toggleAstatus Comma?

I tried to change it to

if (val >= 20 && val <= 80 &&(toggleAstatus == HIGH && toggleBstatus == HIGH))
  {
    Serial.println("Servo Adjusted to:");
    Serial.println(val);
    myservo.write(val);
  }
  if (val >= 20 && val <= 80 &&(toggleAstatus == LOW))
  {
    Serial.println("Servo adjusted to:");
    Serial.println(val + x);
    myservo.write(val + x);
  }
  if (val >= 20 && val <= 80 &&(toggleBstatus == LOW))
  {
    Serial.println("Servo adjusted to:");
    Serial.println(val - x);
    myservo.write(val - x);
  }

but still doesn't work.

I'm just a newbie and don't have much knowledge about programming.

What do your debug prints tell you?

My code uploads fine, it works with my potentio when I'm adjusting the position but my servo doesn't move everytime I used the toggle switch and the serial monitor prints that the servo moves to certain position.

Are you saying that the debug prints indicate that servo should be moving, but it isn't?

Have you checked the wiring, and that you have an adequate power supply?

Yes, I'm using another DC supply 6V(rating of servo), just like I what told earlier, the servo moves when I'm adjusting it with my potentiometer. Then when I tried to use the toggle switch, it doesn't move.

z00mj:
my servo doesn't move everytime I used the toggle switch

Does that mean it moves sometimes?

Yes, the servo moves when I'm adjusting its position with the use of potentiometer, but it doesn't even twitch with the toggle switch.

I tried to change the values of my code and the comma inside the if statement, but still not working.

 myservo.write(val);                  // sets the servo position according to the scaled value


  if (val >= 20 && val <= 80, toggleAstatus == HIGH && toggleBstatus == HIGH)
  {
    Serial.println("Servo Adjusted to:");
    Serial.println(val);
    myservo.write(val);

What is the interval between those two servo writes?

Is that enough time for the servo to move?

If that is the problem, how do I write that condition? Because I want to have a value >= 20 but not <= 80.

How do you write what condition?

The first move is unconditional.

Still doesn't move sir.

  if (val >= 20 &&(toggleAstatus == HIGH && toggleBstatus == HIGH))
  {
    Serial.println("Servo Adjusted to:");
    Serial.println(val);
    myservo.write(val);
  }
  if (val >= 20 &&(toggleAstatus == LOW))
  {
    Serial.println("Servo adjusted to:");
    Serial.println(val + x);
    myservo.write(val + x);
  }
  if (val >= 20 &&(toggleBstatus == LOW))
  {
    Serial.println("Servo adjusted to:");
    Serial.println(val - x);
    myservo.write(val - x);
  }

What can I do?

Context-less snippets help no-one.

Okay

z00mj:
Okay

You wasted five minutes to post that? :o

No. But still thanks for the help. :slight_smile:

You've obviously made some changes so please post the current version of the complete code.

So the pot moves the servo to a position set by val, maybe 30. When you set the switch so toggleAstatus is LOW serial monitor correctly says servo is at 90 but the servo doesn't move from 30. Is that what you are seeing?

One immediate problem with your logic, if toggleBstatus is LOW you subtract 60 from val. 30-60 = -30 and that is an invalid value because servo.write() only takes 0 to 180 not negative values. What are you actually trying to make happen?

Steve

Noobie Here, I've been trying to do this thing since last week. But still I cannot get the right code for this.

This is how I want this to work. I want to control the angle of my servo using the potentiometer. After that, I want to use the toggle switch to rotate the servo(left,right and middle).

What happened was I gave my desired angle by the use of potentio, but everytime I move the toggle switch, my servo doesn't respond.

I post this 2 times already, and this is my third time. Badly needed help.

This is my code

/*
  Testing and Measuring the Servo
  Setting the Pulse Width of Servo using Potentiometer
  Controlling the Rotation of Servo (CW/CCW)
  Measure the current of Servo
  Measuring the angle of Servo with given PW (600usec & 400usec)
  Test the speed of Servo (14ms & 20ms)
  Values presented in a TFT
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

//Toggle Switch
int toggleA = 5;
int toggleB = 10;

//Toggle Switch Status
int toggleAstatus;
int toggleBstatus;


//Center position (1500usec)
const int center = 1500;


//angle added for 600us
int x = 600;


void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object


  //Serial Monitor
  Serial.begin(9600);
  Serial.println("=====Servo Test======");


  //Toggle Switch pinMode
  pinMode(toggleA, INPUT_PULLUP);
  pinMode(toggleB, INPUT_PULLUP);

  //Servo to center position
  myservo.writeMicroseconds(center);
  Serial.println("Servo to center position");



}

void loop() {
  toggleswitch();
}





void toggleswitch()
{
  //Toggle Switch Status Reads HIGH/LOW
  toggleAstatus = digitalRead(toggleA);
  toggleBstatus = digitalRead(toggleB);




   //Toggle Switch Positioning Control of Servo
  //(LEFT, CENTER, RIGHT)
  val = map(analogRead(potpin), 0, 1023, 280, 2720);     // scale it to use it with the servo (value between 280(60°), 2720(120°))
  Serial.println("Position");
  Serial.println(val);

  if (val > 1200 && val < 1800)
  {
    if (toggleAstatus == HIGH && toggleBstatus == HIGH)
    {
      Serial.println("Servo Adjusted to:");
      Serial.println(val);
      myservo.writeMicroseconds(val);
    }
    if (toggleAstatus == LOW)
    {
      Serial.println("Servo adjusted to:");
      Serial.println(val + x);
      myservo.writeMicroseconds(val + x);
    }
    if (toggleBstatus == LOW)
    {
      Serial.println("Servo adjusted to:");
      Serial.println(abs(val - x));
      myservo.writeMicroseconds(abs(val - x));
    }
  }
  else if (val < 1199)
  {
    if (toggleAstatus == HIGH && toggleBstatus == HIGH)
    {
      Serial.println("Servo Adjusted to:");
      Serial.println(val);
      myservo.writeMicroseconds(val);
    }
    if (toggleAstatus == LOW)
    {
      Serial.println("Servo adjusted to:");
      Serial.println(val + x);
      myservo.writeMicroseconds(val + x);
    }
    if (toggleBstatus == LOW)
    {
      Serial.println("Servo can't adjust to that value");
    }
  }
  else if (val > 1801)
  {
    if (toggleAstatus == HIGH && toggleBstatus == HIGH)
    {
      Serial.println("Servo Adjusted to:");
      Serial.println(val);
      myservo.writeMicroseconds(val);
    }
    if (toggleAstatus == LOW)
    {
      Serial.println("Servo can't adjust to that value");
    }
    if (toggleBstatus == LOW)
    {
      Serial.println("Servo adjusted to:");
      Serial.println(abs(val - x));
      myservo.writeMicroseconds(abs(val - x));
    }
  }
}

This is my code now, but still my servo doesn't respond to the toggle switch. I'm having a feeling that it is because the potentiometer holds the position of my servo but I'm not sure about it. And still don't know what to do.

/*
  Testing and Measuring the Servo
  Setting the Pulse Width of Servo using Potentiometer
  Controlling the Rotation of Servo (CW/CCW)
  Measure the current of Servo
  Measuring the angle of Servo with given PW (600usec & 400usec)
  Test the speed of Servo (14ms & 20ms)
  Values presented in a TFT
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

//Toggle Switch
int toggleA = 5;
int toggleB = 10;

//Toggle Switch Status
int toggleAstatus;
int toggleBstatus;


//Center position (1500usec)
const int center = 1500;


//angle added for 600us
int x = 600;


void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object


  //Serial Monitor
  Serial.begin(9600);
  Serial.println("=====Servo Test======");


  //Toggle Switch pinMode
  pinMode(toggleA, INPUT_PULLUP);
  pinMode(toggleB, INPUT_PULLUP);

  //Servo to center position
  myservo.writeMicroseconds(center);
  Serial.println("Servo to center position");



}

void loop() {
  toggleswitch();
}





void toggleswitch()
{
  //Toggle Switch Status Reads HIGH/LOW
  toggleAstatus = digitalRead(toggleA);
  toggleBstatus = digitalRead(toggleB);




   //Toggle Switch Positioning Control of Servo
  //(LEFT, CENTER, RIGHT)
  val = map(analogRead(potpin), 0, 1023, 280, 2720);     // scale it to use it with the servo (value between 280(60°), 2720(120°))
  myservo.writeMicroseconds(val);
  Serial.println("Position");
  Serial.println(val);

  if (val > 1200 && val < 1800)
  {
    if (toggleAstatus == HIGH && toggleBstatus == HIGH)
    {
      Serial.println("Servo Adjusted to:");
      Serial.println(val);
      myservo.writeMicroseconds(val);
    }
    if (toggleAstatus == LOW)
    {
      Serial.println("Servo adjusted to:");
      Serial.println(val + x);
      myservo.writeMicroseconds(val + x);
    }
    if (toggleBstatus == LOW)
    {
      Serial.println("Servo adjusted to:");
      Serial.println(abs(val - x));
      myservo.writeMicroseconds(abs(val - x));
    }
  }
  else if (val < 1199)
  {
    if (toggleAstatus == HIGH && toggleBstatus == HIGH)
    {
      Serial.println("Servo Adjusted to:");
      Serial.println(val);
      myservo.writeMicroseconds(val);
    }
    if (toggleAstatus == LOW)
    {
      Serial.println("Servo adjusted to:");
      Serial.println(val + x);
      myservo.writeMicroseconds(val + x);
    }
    if (toggleBstatus == LOW)
    {
      Serial.println("Servo can't adjust to that value");
    }
  }
  else if (val > 1801)
  {
    if (toggleAstatus == HIGH && toggleBstatus == HIGH)
    {
      Serial.println("Servo Adjusted to:");
      Serial.println(val);
      myservo.writeMicroseconds(val);
    }
    if (toggleAstatus == LOW)
    {
      Serial.println("Servo can't adjust to that value");
    }
    if (toggleBstatus == LOW)
    {
      Serial.println("Servo adjusted to:");
      Serial.println(abs(val - x));
      myservo.writeMicroseconds(abs(val - x));
    }
  }
}