Connect Servo to pushbutton and get it to randomly stop at a number

Hi,
I am entirely new to Arduino/Tinkercad and have some questions regarding a project I am trying to do. Ho,pefully this post is done correctly :sweat_smile:

The project:
I want the servo to spin randomly between 0-180, then when the push button is pressed I want it to randomly stop on either position 0, 50, 100, or 180, and the LED to light if stopped at 180.

The problem I have is to get the servo to stop at a random number if the push button is pressed. I guess I need to use Random () or randomSeed() but not sure how to do it.

Would appreciate all help possible :slight_smile:

#include <Servo.h>


int pos = random(0,180);

  Servo servo8;

void setup()
{

  servo8.attach(8);

  pinMode (7, OUTPUT);
  pinMode (4, INPUT);
  
   Serial.begin(9600);
}

void loop()
{
  servo8.write(pos);
  delay(250);
  pos=random(0,180);
  delay(5);
}

Would appreciate all help possible! :slight_smile:

One approach would be to have random() generate a number between 1 and 4, then use a series of if statements or switch/case to choose servo positions.

1 Like

Hi,
Thank you! Will look it up and try if I succeed! :slight_smile:

There is an input declared, but, you have to query if the button is pressed. Something like that:

if(digitalRead(4)==HIGH) {
  servo8.write(pos);
}

And then, the advice from @jremington is good as well. Do something like this:

int random_number = random(1,5);
if(random_number==1) {
  servo8.write(50);
}
if(random_number==2) {
  servo8.write(100);
}

And so on...

And then, you need to set up randomSeed() as well, before calling random().

randomSeed(analogRead(0));
1 Like

Really appreciate the help!
Thank you very much :slight_smile:

If you have any questions or problems, feel free to ask us!

Finally solved it because of your help! Thank you, have a nice evening!

1 Like

Some post successful code so as to help others in the future........ :upside_down_face:

1 Like

nothing worse than finding a thread on the topic you are struggling with, only to find it ends with, "never mind guys, I figured it out..."

Notice this forum software has the totally infuriating habit of removing important things from what you write?

Apparently as you have found, it does not do this once you have edited it, but since it also deletes more than one space between words and more than one blank line, I just add spaces and an extra line feed when I quote someone's one-line message. :grin:

The code is almost exactly like model_railroader sent.. :slight_smile:

#include <Servo.h>


int random_number = random(1,6);
int pos = random(0,180);
  Servo servo8;

void setup()
{
 Serial.begin(9600);
  	servo8.attach(8);

  pinMode (7, OUTPUT);
  pinMode (4, INPUT);
  

   randomSeed(analogRead(0));
}

void loop()
{
 servo8.write(pos);
  delay(250);
  pos=random(0,180);
  delay(5);
  

  int random_number = random(1,7);
  if(digitalRead(4)==HIGH) {
if(random_number==1) {
  servo8.write(0);
  digitalWrite(7,HIGH); // LED start to light
    delay (2000);
  digitalWrite(7, LOW); 
}
if(random_number==2) {
  servo8.write(50);
  
}
 if(random_number==3) {
  servo8.write(100);

}   
  if(random_number==4) {
  servo8.write(135);
}
  if(random_number==5) {
  servo8.write(150);
}
  if(random_number==6) {
      servo8.write(180);
    
        digitalWrite(7,HIGH); // LED start to light
          delay (2000);
  digitalWrite(7, LOW); 
    
}
        delay (2000);
}
}

Great, that I was able to help you. You no longer need the declaration of variables random_number and pos at the beginning. One time in void loop() is enough. And you wont longer need the variable pos in general. :wink:

But, maybe I'm just too picky....

1 Like

Please note there is no such thing as a " void loop()". There is a function called " loop()". As a matter of definition it returns no parameter when called so it is first defined as " void". But you always refer to it as " loop()". :face_with_raised_eyebrow:

1 Like

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