Hello! I am working on my senior thesis- hence the name- and am struggling to get my code to work so that if I press the button the servo moves 180 and stops. When I press it again it will rotate another 180. Right now my button works when I press it but it won't stop. It keeps going and when I press it again it starts over. So the idea is there but it just continues rotating.
Also any suggestions on how I could get this to work for 4 different buttons and servos. I am super new to Arduino and making a super simple vending machine that just uses the buttons - no coin detector or anything, just a free vending machine where you press the button and you get the item.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
const int ServoPin = 9;
const int ButtonPin = 8;
void setup()
{
pinMode(ButtonPin, INPUT_PULLUP);
myservo.attach(ServoPin); // attaches the servo on pin 9 to the servo object
}
void loop()
{
if (digitalRead(ButtonPin) == LOW) {
for(int pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
Start with "debouncing" the button (a button press creates a thousand little presses). A crude way is to place a delay(50) inside the conditional braces, immediately after (digitalReadButtonPin)==LOW to let the other "bounces" time-out.
Can you please provide some documentation on your Servo. It appears that it may be a continuous rotation servo rather than a servo which moves to a specified position.
Based on his video and code it listed I buy this. I’m really just trying to have the button and servo work in the same fashion nothing I didn’t do anything else that he did.
Sorry realized the last part was not English. I am not doing anything else that the original is. I tried to use his code and take out the parts I didn’t need but that didnt work. I might not have been doing it right.
The tutorial indeed uses a continuous rotation servo and has this explanation of the code in the tutorual.
Right after that we rotate the continuous rotation motor for 950 milliseconds so that the helical coil make a full cycle.
// Rotate the helical coil, discharge the selected item
servo1.writeMicroseconds(2000); // rotate
delay(950);
servo1.writeMicroseconds(1500); // stop
Code language: Arduino (arduino)
Note here that these values can sometimes vary and depends on the motor itself.
You have used servo code that is written for a positional servo.
for(int pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
to your setup() function and place Serial.print() and Serial.println() calls at variuous places in the code to check the values of key variables and confirm that they are properly informing the flow of your code.
I don't think I've ever gotten something working and been sure it is doing what I want the way I want it to without such feedback.
Open and adjust the serial monitor baud rate to match the vakue in the call to the begin() method.
115200 is high speed and will work very well with minimal other effect on you code.
Thank you for your help cattledog I appreciate is so much you have no idea and 2. I am a a newb at arduino so I tried to remove the while loop but i got a bunch of errors can you show me which parts I need to remove.
Also I am not a CS or engineering major, I am a design major who wanted to make something conceptual and cool but realized I lack the knowledge for this portion.
This code is showing that the coding and logic are correct. If you can duplicate the correct print outs with your buttons, I would focus on the hardware for the servos.