Have only one servo random at a time

I find writing code very, very difficult. Please bear with me. Here I just want one servo to move at a time, rarely, if ever two. Is it an easy fix?

#include <Servo.h>
Servo kite;
Servo kite2;
Servo kite3;
void setup() {
    kite.attach(4);
      kite2.attach(5);
        kite3.attach(6);
  }
void loop() {
  unsigned long startDelay = random(500, 1500);  // wait to start 0.5 to 1.5 second

  unsigned long pauseDelay = random(1000, 2000); // pause between 1 and 2 seconds
 int servoCount = 1 + random(3); // 1-3 servos to use this time. (I have deleted this line experimenting and it appears to do nothing. This, and bool, I cannot get my head around. I am really bad at this. 
 
 bool enabled1 = random(100) > 50;
  bool enabled2 = random(100) > 50;
  bool enabled3 = random(100) > 50;
 

  delay(startDelay);

  for (int pos = 0; pos <= 30; pos += 3)////30 was 100
  {
    if ( enabled1 )kite.write(pos);
    if ( enabled2 ) kite2.write(pos);
    if ( enabled3 ) kite3.write(pos);
      delay(15);                       // waits 15ms for the servo to reach the position
  }

  delay(pauseDelay);

  for (int pos = 30; pos >= 0; pos -= 3) {
    if ( enabled1 ) kite.write(pos);
    if ( enabled2 ) kite2.write(pos);
    if ( enabled3 ) kite3.write(pos);
   //// if ( enabled4 ) myservo4.write(pos);
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

Hello
Take some time and analyze these lines of code for yourself.

Did you get an error report from the compiler?

1 Like

Hi Paulpaulson, thanks for your reply. I did not get an error code. It is working but sometimes all three servos go, or two, or one. I have tried to understand what Boolean means, it is a variable for true or false. I do not understand it here. I have no idea what those lines mean. I tried Googling it but got nowhere.
I copied this from a previous post and altered some things myself. Just to try to get the timing right. I am happy there.
I might just leave it. I am curious though.
Thanks anyway.

Thought I would try another way but obviously out of my depth. Oh well, the original works okay.

#include <Servo.h>
long randNumber;
Servo kite;
Servo kite2;
Servo kite3;
void setup() 
{ Serial.begin(9600); randomSeed(analogRead(0)); // nothing connected to 0 so read sees noise }
    kite.attach(4);
      kite2.attach(5);
        kite3.attach(6);
           randomSeed(analogRead(0));
}
void loop() {
  unsigned long startDelay = random(500, 1500);  // wait to start 0.5 to 1 second
  unsigned long pauseDelay = random(1000, 2000); // pause between 1 and 1.5 seconds
   delay(startDelay);
  for (int pos = 0; pos <= 30; pos += 3)////30 was 100
  {
    randNumber=random(4);
    Serial.println(randNumber);
  if ( randNumber=1 )kite.write(pos);
  if ( randNumber=2 ) kite2.write(pos);
    if ( randNumber=3) kite3.write(pos);
      delay(25);                       // waits 15ms for the servo to reach the position
  }
  delay(pauseDelay);
  for (int pos = 30; pos >= 0; pos -= 3) {
   if ( randNumber=1 ) kite.write(pos);
    if ( randNumber=2 ) kite2.write(pos);
    if ( randNumber=3 ) kite3.write(pos);
      delay(25);                       // waits 15ms for the servo to reach the position
  }
}

Working quite well. Thank you! Wish I could understand it better though. Almost all of my coding is trial and error. I just cannot get it into my head.

 bool enabled1 = random(5)> 1;
  bool enabled2 = random(3) >1;
  bool enabled3 = random(4) > 1;

Try and use Arduino Reference - Arduino Reference and look up "random".

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