Servo wont work with digital read

My servo is currently working ok. It is turning 90 degrees and back every 5 secs. However my digital read does not seem to be working. I want the servo to turn only if pin 8, 10 or 11 are set high. but is turning regardless.

#include <Servo.h> 
Servo myservo;                               // creates servo object to control a servo
int pos=0;                                   // variable to store the servo position

void setup() {
  pinMode(8,INPUT);                          // sets digital pin 8 as an input       
  
  pinMode(10,INPUT);                         // sets digital pin 10 as an input 

  pinMode(11,INPUT);                         // sets digital pin 11 as an input  
  
  myservo.attach(9);                         // attaches the servo on pin 9 to the servo object
  

}

void loop() {



  if (digitalRead(8)== HIGH)                 // checks to see if the current state of digital pin 8 is high

  {
    for(pos = 0; pos < 90; pos += 1)         // goes from 0 degrees to 90 degrees 
    {                                        // in steps of 1 degree 
      myservo.write(pos);                    // tell servo to go to position in variable 'pos' 
      delay(5);                              // waits 5ms for the servo to reach the position 
    } 

    delay(5000);                             // delay 5sec

    for(pos = 90; pos>=1; pos-=1)            // goes from 90 degrees to 0 degrees 
    {                                        // in steps of 1 degree 
      myservo.write(pos);                    // tell servo to go to position in variable 'pos' 
      delay(5);                              // waits 5ms for the servo to reach the position 
    } 

    delay(5000);                             // delay 5sec

  }
  }

  


  else if (digitalRead(10)== HIGH)           // checks to see if the current state of digital pin 8 is high
  {
    for(pos = 0; pos < 90; pos += 1)         // goes from 0 degrees to 90 degrees 
    {                                        // in steps of 1 degree 
      myservo.write(pos);                    // tell servo to go to position in variable 'pos' 
      delay(5);                              // waits 5ms for the servo to reach the position 
    } 

    delay(5000);                             // delay 5sec

    for(pos = 90; pos>=1; pos-=1)            // goes from 90 degrees to 0 degrees 
    {                                        // in steps of 1 degree 
      myservo.write(pos);                    // tell servo to go to position in variable 'pos' 
      delay(5);                              // waits 5ms for the servo to reach the position 
    } 

    delay(5000);                             // delay 5sec


  }

 

  else if (digitalRead(11)== HIGH)           // checks to see if the current state of digital pin 8 is high

  {
    for(pos = 0; pos < 90; pos += 1)         // goes from 0 degrees to 90 degrees 
    {                                        // in steps of 1 degree 
      myservo.write(pos);                    // tell servo to go to position in variable 'pos' 
      delay(5);                              // waits 5ms for the servo to reach the position 
    } 

    delay(5000);                             // delay 5sec

    for(pos = 90; pos>=1; pos-=1)            // goes from 90 degrees to 0 degrees 
    {                                        // in steps of 1 degree 
      myservo.write(pos);                    // tell servo to go to position in variable 'pos' 
      delay(5);                              // waits 5ms for the servo to reach the position 
    } 

    delay(5000);                             // delay 5sec


  }

Moderator edit: Fixed code tags.

What is connected to the digital pins?

(Nice try with the code tags - close, but no cigar)

Connected to a GPIO pin on a 18F PIC

(Ha first time )

Grounds connected?

for(pos = 0; pos < 90; pos += 1)         // goes from 0 degrees to 90 degrees

No, it doesn’t.
“pos” ends up as 90, but that’s not where the servo is.

for(pos = 90; pos>=1; pos-=1)            // goes from 90 degrees to 0 degrees

Ditto.
“pos” ends up as zero, but that isn’t where the servo is.

Switches wired correctly to prevent 'floating input', via external pull down resistors? Best way to wire switches is to enable internal pullup resistor and wire switch between input pin and ground, reading LOW then = switch pressed.

Just keep your finger on the button for 15/20 seconds after all you only read that often. Then see blink with out delay.

Mark

At the minute I'm not interfacing it with the PIC chip. So in the code I was for example setting pin 8 to high and getting it to work that way first, but it is turning regardless if I have the pin high or not. Just for my own state of mind is the code correct is it def a hardware issue

Just connected simply using servo to arduino

OK, I'll ask the question again - what is connected to the digital pins?

At the minute there is nothing connected to them. Im just waiting for a pin to be set high and then rotating the servo.

kevin306:
However my digital read does not seem to be working.

What exactly do you mean by “not working”?

The way your code is written (using a lot of delay() functions) the lines if (digitalRead() will not be read very often. So it might seem that nothing is happening.

Of course if there is nothing connected to the I/O pins and you are not using the internal pullup resistors there is no knowing what state the Arduino thinks the pins are in. Use, for example, pinMode(8,INPUT_PULLUP); and the pins will be HIGH until some external signal pulls them low.

…R

Maybe I should explain myself better.
I will have three different devices connected to the 3 digital pins on the arduino. A switch will decide which device is selected and powered on.
The GPIO pin from the pic of device 1 will be connect digital (8
The GPIO pin from the pic of device 2 will be connect digital (10)
The GPIO pin from the pic of device 3 will be connect digital (11)

Therefore the arduino will be constantly monitoring these pins and will know when one of the devices is powered up because its pin will read high. Then the servo motor will rotate.
Likewise for device 2 and 3.
I just want it to work first without the three devices and by turning the pins high through software. This is my first time with the arduino so I am a bit rusty.
Is my code incorrect for what I want to do.

I will have three different devices connected to the 3 digital pins on the arduino.

But you don't have them connected now, so the input pins are floating.

So do I just set them to low.

digitalWrite(8,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);

No, you've just done the one thing you DON'T want to do - turn off the pullup resistors.

So to help newbies like us understand; if we don't have a sensor connected to a digital pin that is actually controlling the input; that digital pin might either be high or low, and we have no way of telling which it is, unless we command it via some sensor?

It sounds like in this case one or more of kevin306's input pins are defaulting to HIGH since he's not commanding them.

In this case, if kevin306 just wants to test the servo control logic without actually hooking up sensors, would a possible solution be to connect some simple pushbutton switches to pins 8, 10, and 11?

Also, while the code is waiting through that 5 second delay, will it accept new inputs? Say the pushbutton or eventual sensor goes to HIGH at the 2.5s mark then back to LOW at the 3.0s mark. The code will completely ignore that input, correct?

It sounds like in this case one or more of kevin306's input pins are defaultingfloating to HIGH since he's not commanding them.

Connecting switches won't make any difference UNLESS
a) the internal pullups are enabled OR
b) external pullups are fitted OR
c) external pulldowns are fitted

I have connected a 10K resistor from pin 10 to ground.
That should mean it is in a low state correct?
Is this what you mean, forgive me for my lack of knowledge.
Also I will have to have a switch for this.
Then I will need 3 different switches for the 3 pins wont I?

This discussion seems to have become hopelessly confused. Start again from scratch. This short piece of code should move a servo when the switch is closed.

#include <Servo.h>

Servo myServo;

byte servoPin = 7;
byte switchPin = 8;
byte switchState;

void setup() {
    Serial.begin(9600);
    Serial.println("Starting servo switch test");

     myServo.attach(servoPin);
     myServo.write(40);

     pinMode(switchPin, INPUT_PULLUP);
}

void loop() {
   switchState = digitalRead(switchPin);
   if (switchState == LOW) {
         myServo.write(120);
   }
}

This is very simple - the only way to get the servo to move back is by pressing the reset button.

Because this uses INPUT_PULLUP the I/O pin will normally be HIGH and the switch should be wired to connect it to GND.

If your PICs require a HIGH to represent a switch press then you will need to swap the logic by using INPUT (without PULLUP) and an external pull-down resistor. But get the simple version working first.

…R