Servo Control 90 degrees left and right and return to 0

Hi all,

Completely new to programming and could do with some help.

I want to program an Arduino Uno with a servo to do the following:

  1. Always return to 0 position.
  2. If sensor 1 (switch for now) detects something it rotates the servo 90 degrees left then returns home
  3. If sensor 2 (switch for now) detects something it rotates the servo 90 degrees right then returns home

Any help would be appreciated.

Thanks in advance.

Have you experimented with the servo library examples?

digialRead(pin) reads the value of a switch or sensor.

Normally a servo is centred by write(90), write(0) goes all the way one way, write(180) goes the other way. You'll have to work out for yourself which way is left and which is right for your particular servo setup.

That plus the Button, Sweep and Knob examples in the IDE should give you enough to get you going. If you have trouble come back here and post your best effort and describe what it is doing wrong.

Steve

The servo.write() function takes values from 0 to 180 so your ‘0’ position will be 90. Your ‘90° Left’ position will be 0 and you ‘90° Right’ position will be 180.

This should get you started:

#include <Servo.h>


const byte TheServoPin = 2;
const byte Sensor1Pin = 3;
const byte Sensor2Pin = 4;


const byte LeftPosition = 0;
const byte CenterPosition = 90;
const byte RightPosition = 180;


Servo TheServo;


void setup()
{
  TheServo.write(CenterPosition); // Yes, you can set a default position before assigning a pin
  TheServo.attach(TheServoPin);


  pinMode(Sensor1Pin, INPUT_PULLUP);  // Open Circuit reads HIGH. Connect to Ground for LOW
  pinMode(Sensor2Pin, INPUT_PULLUP);  // Open Circuit reads HIGH. Connect to Ground for LOW
}


void loop()
{
  // If sensor 1 (switch for now) detects something it rotates the servo 90 degrees left then returns home
  if (digitalRead(Sensor1Pin) == LOW)
  {
    TheServo.write(LeftPosition);
    delay(100);  // Give it time to arrive
    TheServo.write(CenterPosition);
  }


  // If sensor 2 (switch for now) detects something it rotates the servo 90 degrees right then returns home
  if (digitalRead(Sensor2Pin) == LOW)
  {
    TheServo.write(RightPosition);
    delay(100);  // Give it time to arrive
    TheServo.write(CenterPosition);
  }
}

Hi all, Thanks for your prompt replies and help. This is where I am (code below)

Got it working. Code below.

One more question, instead of starting another thread.

So now I want to replace my switches for two sensors. One to detect metallic objects and one to detect any object. Im thinking of using an inductive proximity sensor (non arduino) and an ultrasonic sensor (arduino).

I presume I can do this?? Any reason I can’t?

Thanks again.

Code:

#include <Servo.h>

Servo myservo1; //servo1

int pos1 = 0; //variable for number of degrees for servo1

int buttonPin1 = 4; //activating pin 2 for Push Button
int buttonState1 = 0; //variable for Push Button
int buttonPin2 = 2; //activating pin 2 for Push Button
int buttonState2 = 0; //variable for Push Button
void setup() {
Serial.begin(9600); //activating Serial Monitor
myservo1.attach(9); //activating pin 9 for servo1

pinMode(buttonPin1 , INPUT); //setting Push Button as an input
pinMode(buttonPin2 , INPUT);
}
void loop() {
buttonState1 = digitalRead(buttonPin1); //reading buttonPin and storing it as buttonState.
Serial.println(buttonState1); //telling computer to print buttonState.
if (buttonState1 == HIGH){ //if button is pressed…
delay (1000);

myservo1.write(0); //setting servo1
delay(5000); //wait
myservo1.write(80); //setting servo1
delay(500); //wait
}

{

buttonState2 = digitalRead(buttonPin2); //reading buttonPin and storing it as buttonState.
Serial.println(buttonState2); //telling computer to print buttonState.
if (buttonState2 == HIGH){ //if button is pressed…

myservo1.write(180); //setting servo1
delay(500); //

myservo1.write(80); //

}

}

That can't be the right code because it won't compile. Your curly brackets must be misplaced. Please edit your post, and put the correct code inside code tags to make it forum compatible.

Thanks for the reply. It most definitely works.

No the code you've posted doesn't work because you have missed out the final } so it doesn't even compile.

As for replacing switches with sensors then if they have digital outputs so are HIGH (on) and LOW (off) like a switch then it would be very easy. But an ultrasonic (ranging) sensor isn't like that so that makes it more complicated but not impossible.

Steve

This is how you SHOULD have posted it (after a bit of clean-up):

#include <Servo.h>
Servo myservo1;  //servo1

const int buttonPin1 = 4; //activating pin 2(?) for Push Button
const int buttonPin2 = 2; //activating pin 2 for Push Button
const int ServoPin = 9;


void setup()
{
  Serial.begin(9600); //activating Serial Monitor
  
  myservo1.attach(ServoPin); //activating pin 9 for servo1


  pinMode(buttonPin1 , INPUT); //setting Push Button as an input
  pinMode(buttonPin2 , INPUT);
}


void loop()
{
  int buttonState1 = digitalRead(buttonPin1); //reading buttonPin and storing it as buttonState.
  Serial.println(buttonState1); //telling computer to print buttonState.
  if (buttonState1 == HIGH)  //if button is pressed...
  {
    delay (1000);
    myservo1.write(0); //setting servo1
    delay(5000); //wait
    myservo1.write(80); //setting servo1
    delay(500); //wait
  }


  int buttonState2 = digitalRead(buttonPin2); //reading buttonPin and storing it as buttonState.
  Serial.println(buttonState2); //telling computer to print buttonState.
  if (buttonState2 == HIGH)  //if button is pressed...
  {
    myservo1.write(180); //setting servo1
    delay(500); //
    myservo1.write(80); //
  }
}

Guys, this is literally the first full piece of code I have ever written. So apologies if I haven't followed the correct procedure as I don't know what the correct procedure is.

Thanks for tidying up. But the code definitely worked. Let me do some more research on the sensors that I plan to use, I'll write some more code and post back here when I get that far (IF I get that far....). Again, thanks for the help.

Arduino_New123:
Thanks for the reply. It most definitely works.

If someone with a much larger post count than you tells you that the code doesn’t compile, it is probably because they are correct.

If code is incomplete, it cannot compile…but WE don’t know how incomplete your code might be.

Arduino_New123:
Thanks for tidying up. But the code definitely worked.

The code you had in your IDE might have worked. But the code you posted in here definitely did not. To prove this all you need to do is copy exactly what you posted into your IDE and try to compile it.

Accuracy and attention to detail are important if you want to do any useful programming.

Good luck with the sensors.

Steve