Hello all I have been working on a target system a mini mcqueens. My aim is to have 9 servos moving randomly but showing 10 targets it will have a 4 button rf fob to start, stop, all targets to 90, all targets to 0, I have seen them but nobody will give up their coding unless I pay them so I have brought everything and giving it a go myself, I have the uno 3 and a few cheap mini test servos and fob on order.
So far after lots of failures I have managed to put together the code to get them moving randomly which is probably nothing to most people but it made me happy. I am now stuck as I cannot find any reference to my next piece of code to add in, how will I get 9 servos to show 10 targets and stop, you will see only have 7 servos in the code at the moment as that's all I have got for testing.
many thanks for any help you may give.
#include <Servo.h> // Servo library.
Servo myservo1; // Servo name object to control servo.
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
Servo myservo7;
int pos = 0; // variable to store the servo position
long servonumber;
void setup() {
myservo1.attach(2); // attaches the servo on pin 2 the servo object
myservo2.attach(3); // attaches the servo on pin 3 the servo object
myservo3.attach(4); // attaches the servo on pin 4 the servo object
myservo4.attach(5); // attaches the servo on pin 5 the servo object
myservo5.attach(6); // attaches the servo on pin 6 the servo object
myservo6.attach(7); // attaches the servo on pin 7 the servo object
myservo7.attach(8); // attaches the servo on pin 8 the servo object
}
void loop() {
servonumber = random(1, 7);
if (servonumber == 1){
myservo1.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo1.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 2){
myservo2.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo2.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
} else if (servonumber == 3){
myservo3.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo3.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
} else if (servonumber == 4){
myservo4.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo4.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
} else if (servonumber == 5){
myservo5.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo5.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
} else if (servonumber == 6){
myservo6.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo6.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
} else if (servonumber == 7){
myservo7.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo7.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}
}
@Heli-man-2007 might have meant that each servo needs to show its target 10 times or just randomly pick 10 servos in the list of available Servos (or something else)?
but you might get the same one multiple times and some never.
if you want full coverage, I would shuffle the array (simple algorithm, look for "Fisher–Yates shuffle C++"), then go through the array until the right count (and if you have less motors than target count to show, then repeat the process to shuffle again etc)
Hi please bare with me as I am what you might say getting on in life and teaching myself, so is this code something that I can incorporate into my code or should I scrap what I have done and start again, do you think what I have done is unnecessary
there is an array of numbers (could be pins attached to a servo)
there is a function taking one pin randomly
there is a for loop calling that function 10 times
the point is that you need to think this through in terms of what type of "randomness" do you want. if you need to go through all the targets, then it's not pure random.
As it is, your code comes very close to doing what you want I think.
All it doesn’t do is stop after N servos have randomly offered their target. So count the the targets as each is raised, and at the top of the loop stop if you’ve raised N.
As for the ten servos, I never did but you can hang 3 more off the UNO. I mention power, but if you e gotten this far it seems like you must be powering you servos adequately.
Then it is time to get a bit more specific about the timing and function of your program.
Also @J-M-L has used an array in the demo. Your code is fine but arrays should probably be on your short list of very good things to know about and exploit. Since forever, nothing new and shiny about it.
Hi thanks for your input I am powering the servos with their own supply as my understanding is that it would probably fry the uno. It is good to hear that my code looks fine so far, I have got the times correct I believe they will have 10 seconds out of view 10000 and 3 seconds in view 3000 which are the times set out in shooting competitions, I may have to reverse some servos depending on the way they are mounted and the direction the target flips which I can do ok, there are actually nine servos that have to show ten targets between them I know that random will not guarantee all separate targets but apparently does not matter I believe the reason to use nine is to remove the chance of knowing where your last target would show so if my code is okay so far I would like to keep it if possible and add additional code that will give me ten targets with the nine servos then stop. Then when my rf fob turns up I will try to get that working on my own first as I really want to understand how it works so will research that and give it a go.
Hello A7 I have been looking up how to make things work that you mentioned.
I was reading that stop does not actually stop a servo but you can use detach and I looked for count and targets but I still have not worked out how to turn what I read into code that will do what I want. Your explanation stop after N (10) servos have randomly offered their target. So count the targets as each is raised, and at the top of the loop stop if you’ve raised N (10) this is exactly what I want to do please can you tell me how to write this in code it has stumped me so far.
On the link the program is for 5 servos. When the program ends, Arduino's LED starts flashing. The next sequence is started by pressing the reset button and the Arduino is restarted. I watched the video and the choice of targets is random and a repeat is possible. It is up to you to edit the program according to your requirements.
#include <Servo.h>
Servo myservo[5];
byte ps [5] = {4,5,6,7,8};
void setup ()
{
for ( byte n = 0; n < 5; n ++ ) myservo [n].attach ( ps [n] );
randomSeed(A2);
for ( byte S = 0; S < 6; S ++ )
{
byte sn = random ( 5 );
delay ( 10000 );
myservo [sn].write ( 0 );
delay ( 3000 );
myservo [sn].write ( 90 );
}
pinMode ( 13, OUTPUT );
}
void loop ()
{
delay (400);
digitalWrite (13, !digitalRead (13));
}
Hello Flashko Merry Christmas to you, below is my complete sketch I have written it is working fine for random servos, the part I cannot work out is with 9 servos connected to targets they have to display 10 targets randomly then stop.
I have been reading about a count function and stop which I believe is actually a detach function I could well be wrong. So in my sketch below I need to add code that will make the loop count 10 targets then end or stop the loop. so this is where I am stuck I don't know enough but trying I was very happy to get this far by myself I am sure it needs a count function of some sort in the loop but I have read pages and pages and not found anything like what I am trying to do but must be possible, I would be very grateful if you can solve my problem many thanks.
#include <Servo.h> // Servo library.
Servo myservo1; // Servo name object to control servo.
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
Servo myservo7;
Servo myservo8;
Servo myservo9;
int pos = 0; // variable to store the servo position
long servonumber;
void setup() {
myservo1.attach(2); // attaches the servo on pin 2 the servo object
myservo2.attach(3); // attaches the servo on pin 3 the servo object
myservo3.attach(4); // attaches the servo on pin 4 the servo object
myservo4.attach(5); // attaches the servo on pin 5 the servo object
myservo5.attach(6) // attaches the servo on pin 6 the servo object
myservo6.attach(7) // attaches the servo on pin 7 the servo object
myservo7.attach(8) // attaches the servo on pin 8 the servo object
myservo8.attach(9) // attaches the servo on pin 9 the servo object
myservo9.attach(10) // attaches the servo on pin 10 the servo object
}
void loop() {
servonumber = random(1, 9);
if (servonumber == 1){
myservo1.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo1.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 2){
myservo2.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo2.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 3){
myservo3.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo3.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 4){
myservo4.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo4.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 5){
myservo5.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo5.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 6){
myservo6.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo6.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 7){
myservo7.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo7.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 8){
myservo8.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo8.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}else if (servonumber == 9){
myservo9.write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 seconds
myservo9.write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
}
}
Hello UKHeliBob going by your name a fellow RC Heli addict Happy Christmas to you, I am amazed how much code is removed by using the arrays.
When my friend has made the box's there will be enough room for a 12v motorcycle battery which will have 5.5v regulator for the servos and another for the Arduino uno3. I have just received an RF 433mhz 4 button remote by paralax that I will play with over xmas I read about radiohead as being the way to go so have downloaded that library. I have also ordered a servo power board/driver just in case I need some of the pins used for the servos and have to make changes to how things connect, my only worry is it will mess up what has been accomplished do you have any advice. I would also like to make a build video for YouTube when this is all finished to share for other people to build their own target systems for themselves or their clubs I'm guessing its ok to share anything I use or learn from the forum thanks very much for your help.
Not too much if at all, that’s the beauty of software. At worst some edits and cut and paste… the more you understand about how it is functioning now, the easier that kind of hacking will be.
As for the servo power, I just mention the existence of step-down or “buck” regulators, which are preferred now to linear regulators esp. for anything drawing some current. More efficient, less hot.
Thanks a7 I will take a look I am more at home with electrical things and love gadgets I have just tried out Wokwi with the full sketch altered and working its great I had never seen it before I have to admit didn't realise before it was a link then the light came on ha. I was trying to find an RF 4 button tx and rx but not on the Wokwi list, I will do some more reading and try out radiohead library I also see now why the arrays are important and how they can clean things up I want to get stuck in but children come first, also a big thanks to you and merry christmas.
Hi UKHeliBob I hope you had a great Christmas and new year, I have a couple of questions if you don't mind about the sketch you wrote I noticed in void setup your entry is for (int sNum = 0; sNum < 9; sNum++) and in the loop int sNum = random(0, 9); should they be 0 and 8 as there is only 9 servos or is it done like this to give the 10 targets required. I have also had a go to make it work with a servo driver as not thinking ahead I used all the pins needed for a RF 4 button remote.
At the moment it will not compile so obviously I have not done it correctly can you see where the problem is or what I have missed out thanks for any help
#include "HCPCA9685.h" // servo board library
#include <Wire.h>
#include <RadioHead.h> // RF library
#include <RH_ASK.h>
#include <Servo.h> // Servo library.
#define I2CAdd (0x40); // servo board id
/* Create an instance of the library */
HCPCA9685 HCPCA9685 I2CAdd;
const byte servoPins[] = {0 , 1 , 2, 3 , 4, 5, 6, 7, 8,}; // 9 servo outputs
Servo servos[9];
void setup()
{
/* Initialise the library and set it to 'servo mode' */
HCPCA9685.Init(SERVO_MODE);
/* Wake the device up */
HCPCA9685.Sleep(false);
{
for (int sNum = 0; sNum < 9; sNum++)
{
servos[sNum].attach(servoPins[sNum]);
}
}
void loop()
{
static byte count = 0;
int sNum = random(0, 9); // pick a servo
servos[sNum].write(0); // Make servo go to 0 degrees target showing
delay(3000); // Hold for 3 second
servos[sNum].write(90); // Make servo go to 90 degrees target hidden
delay(10000); // Hold for 10 seconds
count++;
if (count == 10)
{
delay(10000);
count = 0; // reset the counter
}
}