Re: Mini McQueens rig
Posted: Fri Nov 28, 2014 5:12 am
looks an ok speed to me.
All people seeking membership must contact admin after registering to be validated.
https://www.full-bore.co.uk/
Plenty quick enough.Demonic69 wrote:Do you think it's quick enough David? I was going to use the micro servo but it's really twitchy and seems to hold the interrupt open
Code: Select all
/*
Trial of coding for Mini McQueens smallbore target rig
*/
#include <RCSwitch.h>
#include <Servo.h>
Servo myservo1; // create servo object to control a 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
int runs;
int flag;
int previousflag = flag;
int randommillis;
int MAX_RAND = 10; //Maximum random generated number
int showdelay = 3200; //How long each target shows for, adjust to suit
RCSwitch mySwitch = RCSwitch();
const int buzzerpin = 11;
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
pinMode(buzzerpin, OUTPUT);
myservo1.attach(8); // attaches the servo on pin 8 to the servo object
/*myservo2.attach(8);// greyed out until more servos attached
myservo3.attach(8);
myservo4.attach(8);
myservo5.attach(8);
myservo6.attach(8);
myservo7.attach(8);
myservo8.attach(8);
myservo9.attach(8);*/
}
void loop() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
Serial.println(value);
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );
switch (value) {
case 20565:
Serial.println("A"); // Prints which button was selected from the remote. useful for further functions
mcqueens(); // runs the C0f
break;
case 20564:
Serial.println("B");
myservo1.write(180);
break;
case 5205:
Serial.println("C");
myservo1.write(90);
break;
case 5204:
Serial.println("D");
myservo1.write(90);
break;
}
}
mySwitch.resetAvailable();
}
}
void mcqueens(){
beep(2000); //Beep to let shooter know it's about to begin
for (runs = 0;runs < 10; runs++){
Serial.print("//////////////////// Run Number ");
Serial.println(runs);
do { // Random routine, stop same flag being shown twice in succession
flag = random(MAX_RAND);
} while (flag == previousflag);
previousflag = flag; //save the currently selected flag
//flag = random(9); // Normal random routine, same flag can be chosen twice in succession
Serial.print("Servo: "); //DEBUG print random number from 0 to MAX_RAND
Serial.println(flag);
randommillis = random(10000); // random milliseconds for delay between flags, up to 10 seconds
Serial.print("Delay: ");
Serial.println(randommillis);
delay(randommillis);
switch (flag) { // Switch/Case statements to cover all servos
case 0:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 1:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 2:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 3:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 4:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 5:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 6:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 7:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 8:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
case 9:
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" Out");
myservo1.write(1);
delay(showdelay);
myservo1.write(90);
Serial.print("Servo: ");
Serial.print(flag);
Serial.println(" In");
break;
}
}
beep(1000);
beep(1000);
beep(1000);
Serial.println("*************Done!********************");
}
void beep(int delayms){
analogWrite(buzzerpin, 100); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // How long to beep for
analogWrite(buzzerpin, 0); // 0 turns it off
delay(500);
}
Strange that the array version didn't work. Did you try something like the code below?Demonic69 wrote:I could only think of using case statements to manage the servos as they have to be named as far as I can see.
Code: Select all
/*
Trial of coding for Mini McQueens smallbore target rig
*/
#include <RCSwitch.h>
#include <Servo.h>
#define NUM_SERVOS 9
//int pos = 0;// variable to store the servo position
int runs;
int flag;
int previousflag = flag;
int randommillis;
int showdelay = 3200; //How long each target shows for, adjust to suit
RCSwitch mySwitch = RCSwitch();
const int buzzerpin = 11;
Servo ServoArray[NUM_SERVOS];
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
pinMode(buzzerpin, OUTPUT);
int ServoPinArray[NUM_SERVOS] = { 8, 12, 13, 14, 15, 16, 17, 18, 19 }; // Servo pins
for (int ServoNum = 0; ServoNum < NUM_SERVOS; ServoNum++)
ServoArray[ServoNum].attach(ServoPinArray[ServoNum]);
}
void loop() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
Serial.println(value);
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );
switch (value) {
case 20565:
Serial.println("A"); // Prints which button was selected from the remote. useful for further functions
mcqueens(); // runs the C0f
break;
case 20564:
Serial.println("B");
ServoArray[0].write(180);
break;
case 5205:
Serial.println("C");
ServoArray[0].write(90);
break;
case 5204:
Serial.println("D");
ServoArray[0].write(90);
break;
}
}
mySwitch.resetAvailable();
}
}
void mcqueens(){
beep(2000); //Beep to let shooter know it's about to begin
for (runs = 0;runs < 10; runs++){
Serial.print("//////////////////// Run Number ");
Serial.println(runs);
do { // Random routine, stop same flag being shown twice in succession
flag = random(NUM_SERVOS);
} while (flag == previousflag);
previousflag = flag; //save the currently selected flag
//flag = random(NUM_SERVOS); // Normal random routine, same flag can be chosen twice in succession
Serial.print("Servo: "); //DEBUG print random number from 0 to MAX_RAND
Serial.println(flag);
randommillis = random(10000); // random milliseconds for delay between flags, up to 10 seconds
Serial.print("Delay: ");
Serial.println(randommillis);
delay(randommillis);
ServoArray[flag].write(1);
delay(showdelay);
ServoArray[flag].write(90);
}
beep(1000);
beep(1000);
beep(1000);
Serial.println("*************Done!********************");
}
void beep(int delayms){
analogWrite(buzzerpin, 100); // Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms); // How long to beep for
analogWrite(buzzerpin, 0); // 0 turns it off
delay(500);
}
I tried something similar but it didn't work, I had an array of variable names. I'll run through what you've put and see if that works better.rox wrote: Strange that the array version didn't work. Did you try something like the code below?
I had a look at seeding but it didn't make sense to me. Does it just stop repeats? We want repeats really.rox wrote: Don't forget to seed the random number generator.
I was aiming for 9 yes, I've probably left some dodgy code in while playing about with things.rox wrote: Note that your 'case' approach has 10 cases, and 10 possible random numbers, but it looks like you intended there to be 9.
That was intentionalrox wrote: Each case currently calls the same servo.