Help with servo + push button

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.

Or, try a more accepted debounce method.

Servos typically only rotate 180 degrees (if that)

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.

Hi! Based on the
https://howtomechatronics.com/projects/diy-vending-machine-arduino-based-mechatronics-project/

I bought the
https://www.amazon.com/KOOKYE-360-Continuous-Rotation-Helicopter/dp/B019TOJPO4/ref=as_li_ss_tl?ie=UTF8&qid=1533401271&sr=8-6&keywords=360+servo&linkCode=sl1&tag=howto045-20&linkId=f233ac4595246b84ce893c3850059cc4

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.

The thing is, a servo can be sent to a given "angle" (within a given limited range, usually around 180 degrees), roughly repeatable.

A "continuous rotation" "servo" ( not really a servo at all) cannot. The best you can hope for is that you can stop it rotating.

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 
    }

This is currently my code now, from the website I shared, but it doesn't work. Nothing happens when I click the button.

#include <Servo.h>
 

#define button1 13
#define button2 12
#define button3 11
#define button4 10

int buttonPressed;

Servo servo1, servo2, servo3, servo4;  


void setup() 
{ 
  
  servo1.attach(4);
  servo2.attach(5);
  servo3.attach(6);
  servo4.attach(7);

  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);

} 
 
 
void loop() {
  while (true) {
    if (digitalRead(button1) == LOW) {
      buttonPressed = 1;
      break;
    }
    if (digitalRead(button2) == LOW) {
      buttonPressed = 2;
      break;
    }
    if (digitalRead(button3) == LOW) {
      buttonPressed = 3;
      break;
    }
    if (digitalRead(button4) == LOW) {
      buttonPressed = 4;
      break;
    }
  }
  switch (buttonPressed) {
    case 1:
      // Rotate the helical coil, discharge the selected item
      servo1.writeMicroseconds(2000); // rotate
      delay(950);
      servo1.writeMicroseconds(1500);  // stop
      break;
      
     case 2:
      servo2.writeMicroseconds(2000); // rotate
      delay(950);
      servo2.writeMicroseconds(1500);  // stop
      break;

      case 3:
      // Rotate the helix, push the selected item
      servo3.writeMicroseconds(2000); // rotate
      delay(950);
      servo3.writeMicroseconds(1500);  // stop
      break;

      case 4:
      // Rotate the helix, push the selected item
      servo4.writeMicroseconds(2000); // rotate
      delay(950);
      servo4.writeMicroseconds(1500);  // stop
      break;
  }
 }

Add

Serial.begin(115200);

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.

HTH

a7

The while(true) loop does not include the switch case. You are stuck in the while loop(). There is no need for it. Let the main loop do the looping.

How are you powering the servos?

  1. 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.

I was mistaken about the while() loop because I didn't notice the use of the break.

I think the while() loop is important to your structure so that you don't repeat the cases in the switch case block.

Use the Serial print statements as suggested to confirm what is happening in the code and where the flow is going.

If "nothing happens" can you answer my question about the powering of the servos.

Have you run any simple test code where you just try and run the servo or servos?

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.

#include <Servo.h>


#define button1 13
#define button2 12
#define button3 11
#define button4 10

int buttonPressed;

Servo servo1, servo2, servo3, servo4;


void setup()
{
  Serial.begin(115200);
  servo1.attach(4);
  servo2.attach(5);
  servo3.attach(6);
  servo4.attach(7);

  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);
}


void loop()
{
  while (true)
  {
    if (digitalRead(button1) == LOW)
    {
      buttonPressed = 1;
      Serial.println(1);
      break;
    }
    if (digitalRead(button2) == LOW)
    {
      buttonPressed = 2;
      Serial.println(2);
      break;
    }
    if (digitalRead(button3) == LOW)
    {
      buttonPressed = 3;
      Serial.println(3);
      break;
    }
    if (digitalRead(button4) == LOW)
    {
      buttonPressed = 4;
      Serial.println(4);
      break;
    }
  }
  switch (buttonPressed)
  {
    case 1:
      // Rotate the helical coil, discharge the selected item
      Serial.println("case1");
      servo1.writeMicroseconds(2000);  // rotate
      delay(950);
      servo1.writeMicroseconds(1500);  // stop
      break;

    case 2:
      Serial.println("case2");
      servo2.writeMicroseconds(2000);  // rotate
      delay(950);
      servo2.writeMicroseconds(1500);  // stop
      break;

    case 3:
      // Rotate the helix, push the selected item
      Serial.println("case3");
      servo3.writeMicroseconds(2000);  // rotate
      delay(950);
      servo3.writeMicroseconds(1500);  // stop
      break;

    case 4:
      // Rotate the helix, push the selected item
      Serial.println("case4");
      servo4.writeMicroseconds(2000);  // rotate
      delay(950);
      servo4.writeMicroseconds(1500);  // stop
      break;
  }
}
17:25:53.828 -> 1
17:25:53.828 -> case1
17:25:55.727 -> 2
17:25:55.727 -> case2
17:25:57.611 -> 3
17:25:57.611 -> case3
17:26:00.660 -> 4
17:26:00.660 -> case4
1 Like

It worked​:sob::sob:. Thank you so much. Honestly crying tears of joy.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.