Go Down

Topic: Servo operated by push button (Read 7660 times) previous topic - next topic

Peter9DO

Hello everyone! So, I'm working on a little project and I cant figure out how to set up the programing on the Arduino, so I was hoping that someone would be able to give me a little help.

I have a servo, that I want to be at position = 0 while the push button is in the LOW position. When I hit the push button in the HIGH position I need the servo to sweep to position = 180 and then back to position = 0, ending the cycle, until the push button is hit again.

I have the servo working with no problem and I set up a push button with no problem but the specific task I need it to do when the button is hit is not working. I'm sure it has to do with programing the push button a little different than how I have it already.

If anyone can help me with the code on this that would be GREAT! I'm fairly new to the Arduino sketches so please bare with me, I have about 40 hours invested into figuring out a solution to this, so I feel like I gave it a good run, before asking for help. I was looking forward to doing this one on my own. But I would like to see my project in motion rather than being stuck on something that I'm sure is fairly simple, but I'm not experienced enough with code to figure it out.

Thank you for any help you guys are willing to give! 

-Pete
What if Burt Reynolds, really changed his name to Turd Ferguson?

wildbill


Peter9DO

As soon as I get home I will, I took the sketch for the push button hold and the sketch for servos and tried to combine them.... It's all types of messed up, lol! As I said I have very little experience with code. I appricate your willingness to help.
What if Burt Reynolds, really changed his name to Turd Ferguson?

Peter9DO

#3
Jun 03, 2012, 03:47 am Last Edit: Jun 04, 2012, 12:38 am by CrossRoads Reason: 1
Okay, well here it is... I didn't know how to combine the two of them properly... I'm still working on it, any guidance you can give would be very helpful. Also, if it's not to much to ask, I'm really not looking for someone to just do it for me, but if you can explain it so I can understand it a little more that would be awesome! Again, thank you for any help you can give!

Code: [Select]

#include <Servo.h>

Servo myservo;  

int pos = 0;
int pushButton = 2;
int LED = 13;

int state = HIGH;
int readibg;
int previous = LOW;

long time = 0;
long debounce =200;


void setup()
{
 {
   pinMode(pushButton, INPUT);
   pinMode(LED, OUTPUT);
 }
 
 {
 myservo.attach(9);  
 }{
 Serial.begin(9600);
 pinMode(pushButton,INPUT);
 }
}
 
void loop()
{
 
{
 reading = digitalRead(pushButton);

 
 if (reading == HIGH && previous == LOW && millis() - time > debounce) {
   if (state == HIGH)
     state = LOW;
   else
     state = HIGH;

   time = millis();    
 }

 digitalWrite(outPin, state);

 previous = reading;
}
 {
   int buttonState = digitalRead (pushButton);
   Serial.println(buttonState);                            
 }
 
 
 for(pos = 0; pos < 180; pos +=1)  
 {                                  
   myservo.write(pos);              
                       
 }
 for(pos = 180; pos >= 1; pos -=1)      
 {                                
    myservo.write(pos);                                   }

}
}
What if Burt Reynolds, really changed his name to Turd Ferguson?

wildbill

Four things:
1. Use code tags to post your code - the # button above lets you do this
2. You have a lot of superfluous curly braces - the code will be a lot clearer without them
3. Put a delay in each of your for loops - the arduino is fast. Your servo is being told to go to 180 degrees and back before it has a chance to move at all.
4. The commands to move the servo are not dependent on the state of the button.

Fix number 3 first - then you should at least be able to confirm that your servo wiring is correct as it should then move.

Peter9DO

Well, let me ask you a question in regards to the delays, I took them out for a reason. If you can tell me if this is correct. When you put a delay in a sketch the entire sketch takes a delay, so if the Arduino is doing a read operation at the dsame time a delay from a say a write operation, it will miss the command to read because of the delay.

I was trying to get 2 LEDs to blink but alternating them and I had to take the delay out for that same reason, that's why I tried not to use the delay.

So, if this is true, how would I go about pausing the operation without using the delay function?
What if Burt Reynolds, really changed his name to Turd Ferguson?

Peter9DO

#6
Jun 03, 2012, 11:59 pm Last Edit: Jun 04, 2012, 12:39 am by CrossRoads Reason: 1
How about this one? I'm getting an error in the script but I think I'm on the right track.... I need to get that delay function out of it, because in time I'm going to have more functions. Can you give me an idea as to what I would need to change to make it work and even make it work without the delay?
Code: [Select]

#include <Servo.h>

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

int button = 0;
int val;

void setup()
{
 myservo.attach(9); /* attaches servo to pin 9*/
}

void loop()
{
 val = analogRead(button);
 val = map(val, 0, 1023, 0);
myservo.write(val);
delay(15);
}
(code tags added by moderator)
What if Burt Reynolds, really changed his name to Turd Ferguson?

Peter9DO

#7
Jun 04, 2012, 04:02 pm Last Edit: Jun 05, 2012, 03:32 am by CrossRoads Reason: 1
With some hard work last night and a nice size knot on my head from banging it into the desk. I came out victorious from the first part of my battle with this project! I now have the servo being operated by the push button. Now there may be a different way to do it, but this is what I have done and it's working!

(Add code tags next post please!)
Code: [Select]

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
               // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
int button = 2;  // The button will be on Pin 7


void setup()
{
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
digitalWrite (button, LOW);
}

void loop()
{
 
   if (digitalRead(button) == LOW)

 for(pos = 0; pos < 90; pos += 90)  // goes from 0 degrees to 90 degrees
 {                                  // in steps of  degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
                         // waits 1s for the servo to reach the position
 }
 if (digitalRead(button) == HIGH)
 
 for(pos = 90; pos>=90; pos-=90)     // goes from 90 degrees to 0 degrees
 {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
 delay(50);                             // waits 50ms for the servo to reach the position
                         
What if Burt Reynolds, really changed his name to Turd Ferguson?

Peter9DO

I have it all working now, it wasn't as hard as I was making it out to be... It took me long enough to figure it out however, I have it working now and I have learned from my mistakes!
What if Burt Reynolds, really changed his name to Turd Ferguson?

khelms1493

Great code. Exactly what i needed for my trigger on my skeet thrower. Great job. Forgot a couple things on the end of the code though : )....
Code: [Select]
           
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int pos = 0;    // variable to store the servo position
int button = 2;  // The button will be on Pin 7


void setup()
{
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
pinMode(pos, OUTPUT);
pinMode(button, INPUT);
digitalWrite (button, LOW);
}

void loop()
{
 
    if (digitalRead(button) == LOW)

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

johny132

can anyone post a picture of the connection of the button to the servo please?? thanks a lot.. i cant seem to get the button to work with the servo..

Atom_6

I'm fairly new to arduino and I was wondering why do you put the "for"  in there?  Could you just make 2 pos's and flip between them when needed.

Thanks,

Go Up