Error coding : two servo motors controlled by button

Hello everyone,
I'm new to programming and I am having trouble with coding a button to control two servo motors for a project. I don't know if I'm doing right or not please I need help!!
My code is based off another one:

#include <Servo.h>

Servo servo1;
Servo servo2;

#define pushButtonPin 2
int pos = 0;
int pos2 = 0;
int buttonPushed =0;

void setup() {

Serial.begin(9600);
servo1.attach(3);
servo2.attach(5);//
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println(" 123 ");
servo1.write(pos2);
servo2.write(pos);

}

void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){

for(pos = 0; pos < 180; pos += 1)
for(pos2 = 0; pos2 < 90; pos2 += 1)
{
buttonPushed =0;
}
}

for(pos = 180; pos>=1; pos-=1)
for(pos2 = 90; pos2>=1; pos2-=1)
{
buttonPushed =0;
}
}

servo1.write (pos2);
servo2.write (pos);
Serial.print("Moved to: ");
Serial.print(pos);
Serial.println(" degree");
delay(50); //
}

}
}
}

when I try to verify it it says "servo1 does not name a type", anyone can tell me why? please I really need help

Please read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum. You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.
code tags new

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) and you will see the problem. You have closed loop() and the rest of the code is outside of a function which is not legal.

Further to what groundFungus said, if you click the mouse next to the opening brace of loop(), you will see a small box appear over this one...

  for (pos = 180; pos >= 1; pos -= 1)
    for (pos2 = 90; pos2 >= 1; pos2 -= 1)
    {
      buttonPushed = 0;
    }
} <<<<<<<<<

... which indicates they're buddies.

Even with the indenting of control-T it's often useful to comment your closing braces to help keep track of them, like this:

} //end of loop

@helenabessa123, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

As the @groundFungus and @anon87005993 mentioned, properly indenting your code using tools -> autoformat will reveal what is wrong. This is what it looks like after an autoformat.

#include <Servo.h>

Servo servo1;
Servo servo2;

#define pushButtonPin 2
int pos = 0;
int pos2 = 0;
int buttonPushed = 0;

void setup() {

  Serial.begin(9600);
  servo1.attach(3);
  servo2.attach(5);//
  pinMode(pushButtonPin, INPUT_PULLUP);
  Serial.println(" 123 ");
  servo1.write(pos2);
  servo2.write(pos);

}

void loop() {
  if (digitalRead(pushButtonPin) == LOW) {
    buttonPushed = 1;
  }
  if ( buttonPushed ) {

    for (pos = 0; pos < 180; pos += 1)
      for (pos2 = 0; pos2 < 90; pos2 += 1)
      {
        buttonPushed = 0;
      }
  }

  for (pos = 180; pos >= 1; pos -= 1)
    for (pos2 = 90; pos2 >= 1; pos2 -= 1)
    {
      buttonPushed = 0;
    }
}

servo1.write (pos2);
servo2.write (pos);
Serial.print("Moved to: ");
Serial.print(pos);
Serial.println(" degree");
delay(50); //
}

}
}
}

Each function (setup() and loop() in this case) start swith a { and ends with a }; a } at the beginning of a line indicates the end of a function. @anon87005993 already indicated where your loop() ends.

So the rest of the code is outside a function which is not allowed.

Further, you should never have more than one } at the beginning of a line at the end of your code. So below indicates a problem.

...
...
delay(50); //
}

}
}
}

How useful your code is, is debatable at this stage. Why would you set buttonPushed 180x90 times to 0? And by the time you reach servo1.Write() and servo2.write(), pos and pos2 are both 0. I suspect that those actually should have been inside the for-loops.

Hello
What is the task of your coding?
Desribe the task in simple words what shall happens and this very simple.

the above loops thru pos2 for each value of pos but only resets buttonPushed to zero without affecting any servo. the 2nd pair of loops does the same

seems like you may have been attempting the following which doesn't return the servos to their original positions

    if (buttonPushed) {
        for(pos = 0; pos < 180; pos += 1)  {
            servo2.write (pos);
            Serial.print("Moved to: ");
            Serial.print(pos);
            Serial.println(" degree");

            for(pos2 = 0; pos2 < 90; pos2 += 1) {
                servo1.write (pos2);
                delay(50);
            }
        }
    }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.