Go Down

Topic: Servo operated by push button (Read 7 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.

Go Up