Servo Control

Hello everyone, I have a quick question. What I am trying to do is make a servo go right if the right most button is pushed and go left if the left most button is pushed. Nothing is happening when I push the buttons. What is wrong with my code! (Or wiring…)

#include <Servo.h>

int ran=0;
int SPS=0;
Servo myservo;

void setup()
{
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  Servo.attach(10);
}

void loop()
{
  myservo.write(SPS);
  for(ran=0; ran++;)
  {
    if(digitalRead(8) == HIGH);
    {
      SPS++;
      delay(20);
    }
    if(SPS == 180)
    {
      break;
    }
    if(digitalRead(8) == LOW)
    {
      break;
    }
  }
  for(ran=0; ran++;)
  {
    if(digitalRead(9) == HIGH)
    {
      SPS--;
      delay(20);
    }
    if(SPS == 0)
    {
      break;
    }
    if(digitalRead(9) == LOW)
    {
      break;
    }
  }
}

Wiring:
Imgur

Thank you!!!

sigilwig444: What is wrong with my code! (Or wiring...)

Or both!

As for your wiring, your switches need either a pull-up/pull-down resistor attached or the internal pull-ups enabled. As far as your code, I don't see any point in having that for loop and you seem to be over-complicating the code with it.

Arrch:

sigilwig444: What is wrong with my code! (Or wiring...)

Or both!

As for your wiring, your switches need either a pull-up/pull-down resistor attached or the internal pull-ups enabled. As far as your code, I don't see any point in having that for loop and you seem to be over-complicating the code with it.

Like this??? http://instagram.com/p/dAP1uxKMYM/

sigilwig444: Like this??? http://instagram.com/p/dAP1uxKMYM/

Yes, that will work. Why people don't simply things and use the internal pull-up resistors, though, I don't know.

I'm more C# than C/C++, but shouldn't Servo.attach(10); be myservo.attach(10); in your setup? And I believe you're missing the condition in your for loops, it should be for(ran=0; ran [somecondition]; ran++;) But I agree with Arrch that you're over complicating things with them.

Basic two button servo test code.

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

Ok, so I actually got this working great. I used a pull up resistor but it didn't work, so I removed my internal pull up activator lines of code in my setup and it worked great! Then, that night the arduino fell off a ledge onto the couch. It only fell about 2' but my code was no longer working... The next day I moved my input wires from digital to analog pins. Then I used the serial monitor to read the value coming from the buttons. When the buttons were not pressed it was at 998, when the button was pressed it was reading 1000. So I used this:

if(analogRead(0) > 999)
{
  count++;
  delay(10);
}

And that worked fine!!! Was damage done to the board??? What happened??? It's also not quite so responsive now...

Was the arduino powered up with external wires connected to it at the time it fell? If so there is a possibility some wires got tangled and caused a short circuit.

Another slim possibility (if it was powered up) is that the soft material of the couch prevented it from getting a flow of cooling air and it overheated.

If it wasn't powered up I'd be surprised if any damage occurred (even if it fell a short distance onto a hard surface) unless (again slim possibility) the atmega chip became dislodged from its socket. Try pushing it in (with power off) and after checking that all the pins are lined up correctly.

But you might have been unlucky :(

...R