randomizing case want selection

hi guys, im using case wants in a program and i was wondering if its possible to define a random case want selection from preset choices,

so say you have

typedef enum
  {
   initialState,
   wantstate1,
   wantstate2,
   wantstate3,
   wantstate4,
   wantstate5,
   wantstate6,
   wantstate7,
   wantstate8,
   wantstate9,
   wantstate10,
   wantstate11,
   wantstate12,
   wantstate13,
   wantstate14,
   wantstate15,
   wantstate16,
   wantstate17,
   wantstate18,
   wantstate19,
   wantstate20,
   wantstate21,
   wantstate22,
   wantstate23,
    } states;

and in the case you have this

case wantstate4:
         digitalWrite (eyemotor, HIGH);
         state = wantstate5;
         timeInThisState = random (5000 - 8000);
         break;

but for state = wantstate5, can you define it to select a random state from the choices of state 1, 5, 12, 15, 20 for example?

if that doesnt make sense i appologise, i would just like to randomise where the code returns to in the final case wantstate in an animation project but certain case wantstates need to be excluded from the random selection

can you define it to select a random state from the choices of state 1, 5, 12, 15, 20 for example?

Yes

Put the valid state numbers in an array and use the random() function to generate a random number and use that as the index to the array

In your enum, you define:

  • initialstate to 0
  • wantstate1 to 1
  • wantstate2 to 2
  • etc.

If you want a random choice in {1, 5, 10, 12, 15}, just create an array with these values and pick a random element

int myRandomStates[5] = {wantstate1, wantstate5, wantstate10, wantstate12, wantstate15};
int randomPickedState = myRandomStates[random(5)];

Or move the valid states to the start of the enum so that they have values of say 0 to 4 and use random() to select a number between 0 and 4 equivalent to the valid states

thank you guys, i will have a play with adding this feature to the code, its to animate an owl (bubo) so being able to have it randomly track back to certain functions and so on would add that “realism” of an actual owl to it. i’ve already randomized timings of features by defining random times in states so it should work rather nicely.

lesept:
In your enum, you define:

  • initialstate to 0
  • wantstate1 to 1
  • wantstate2 to 2
  • etc.

If you want a random choice in {1, 5, 10, 12, 15}, just create an array with these values and pick a random element

int myRandomStates[5] = {wantstate1, wantstate5, wantstate10, wantstate12, wantstate15};

int randomPickedState = myRandomStates[random(5)];

so i have identified 10 usable states, where you have myRandomStates[random(5)];

i assume i change the 5 to 10?

i assume i change the 5 to 10?

One great things about the Arduino is that it is so easy to try things for yourself

thats not particularly helpful, especially when your dealing with randomising functions, to test it, you would have to breadboard the hardware, set it going and wait to see how it functions and if any are left out, which given the length of code, how long in time it runs for in each function could take a long time.

therefore it is logical ask someone with more knowledge on the subject if it should be changed, especially when the code in question is for a project your working in with your 13 year old brother.

however!!! you are right, arduino is very easy to make changes in and experiment with things, even adding in new features, removing unwanted ones and generally nice to play with. but in this instance, a simple yes or no would be appreciated.

lesept:
In your enum, you define:

  • initialstate to 0
  • wantstate1 to 1
  • wantstate2 to 2
  • etc.

If you want a random choice in {1, 5, 10, 12, 15}, just create an array with these values and pick a random element

int myRandomStates[5] = {wantstate1, wantstate5, wantstate10, wantstate12, wantstate15};

int randomPickedState = myRandomStates[random(5)];

okay so i’ve worked in the majority if your recommended method, im having difficulty understanding what you mean with defining states “wantstate1 to 1”
heres the entire code i’ve assembled, its not the cleanest and im sure there are more efficient ways of doing what im doing but for my perpouses this works perfectly as the code was designed to be easily adjustable with timings and so on, so i’d rather have a longer code with easier time manipulation than a shorter code with harder manipulations, its also been written so i can easily explain the main functions of the code to a 13 year old kid haha.

can anyone see any major issues that would prevent the code from running and how do i go about the defining states a bit more detailed please? (thats the part im struggling with)

i should say, im not getting any error messages, but i have had that before, loaded up the code and it not ran because i forgot to add one singular line in. i would breadboard it and test it but im waiting for all the hardware to arrive and because of the easter holidays everythings delayed, i’d like to ensure the code looks good and functionable before loading it up really.

#include <Servo.h>
#include <LedFader.h>

// pin assignments
const byte eyemotor = 1; 
const byte redled = 10;  //pwm
const byte blueled = 9;  //pwm
Servo headservo;
Servo legservo;
Servo wingservo;
int solenoid = 2;

// Faders                pin           min  max  millis    on?    stop?
LedFader redledFader      (redled,      0,   40,  1000,   false,  true);
LedFader blueledFader     (blueled,     0,   40,  1000,   false,  true);

typedef enum
  {
   initialState,
   wantstate1,  //use for state ransomiser
   wantstate2,
   wantstate3,
   wantstate4,  //use for state ransomiser
   wantstate5,
   wantstate6,  //use for state ransomiser
   wantstate7,
   wantstate8,
   wantstate9,
   wantstate10,
   wantstate11,  //use for state ransomiser
   wantstate12,  //use for state ransomiser
   wantstate13,
   wantstate14,
   wantstate15,
   wantstate16,
   wantstate17,  //use for state ransomiser
   wantstate18,
   wantstate19,
   wantstate20,
   wantstate21,  //use for state ransomiser
   wantstate22,
   wantstate23,  //use for state ransomiser
           } states;


states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 5;
int pos = 0;
int myRandomStates[8] = {wantstate1, wantstate4, wantstate6, wantstate12, wantstate11, wantstate17, wantstate21, wantstate23};
int randomPickedState = myRandomStates[random(8)];

void setup ()
  {
  pinMode (solenoid, OUTPUT);
  pinMode (eyemotor, OUTPUT);
  headservo.attach(3);  //pwm
  wingservo.attach(5);  //pwm
  legservo.attach(6);   //pwm
  redledFader.begin ();
  blueledFader.begin ();
  }
        
void doStateChange ()
  {
  lastStateChange = millis ();
  timeInThisState = 5;

  switch (state)
   {
    case initialState:
         state = wantstate1;
         break;

    case wantstate1:
         redledFader.off ();
         blueledFader.on ();
         state = randomPickedState;
         timeInThisState = 1000;
         break;
         
    case wantstate2:
         for (pos = 0; pos <= 70; pos +=1) {
         headservo.write(pos);}
         state = wantstate3;
         timeInThisState = random (1000-3000);
         break;

    case wantstate3:
         for (pos = 70; pos >= -70; pos -=1) {
         headservo.write(pos);}
         state = wantstate4;
         timeInThisState = random (1000 - 3000);
         break;
             
    case wantstate4:
         for (pos = -70; pos <= 0; pos +=1){
         headservo.write(pos);}
         state = randomPickedState;
         timeInThisState = 300;
         break;

    case wantstate5:
         digitalWrite (eyemotor, HIGH);
         state = wantstate6;
         timeInThisState = random (5000 - 8000);
         break;

    case wantstate6:
         digitalWrite (eyemotor, LOW);
         state = randomPickedState;
         timeInThisState = 1000;
         break;
         
    case wantstate7:
         digitalWrite (solenoid, HIGH);
         state = wantstate8;
         timeInThisState = 300;
         break;

    case wantstate8:
         digitalWrite (solenoid, LOW);
         state = wantstate9;
         timeInThisState = random (1000-3000);
         break;

   case wantstate9:
         digitalWrite (solenoid, HIGH);
         state = wantstate10;
         timeInThisState = 300;
         break;

   case wantstate10:
         digitalWrite (solenoid, LOW);
         state = randomPickedState;
         timeInThisState = random (1000-3000);
         break;

   case wantstate11:
         digitalWrite (solenoid, HIGH);
         state = wantstate12;
         timeInThisState = 300;
         break;

   case wantstate12:
         digitalWrite (solenoid, LOW);
         state = randomPickedState;
         timeInThisState = random (8000-30000);
         break;

   case wantstate13:
        for (pos = 0; pos <= 30; pos +=1) {
         wingservo.write(pos);}
         state = wantstate14;
         timeInThisState = 300;
         break;

   case wantstate14:
        for (pos = 30; pos >= 0; pos -=1) {
        wingservo.write(pos);}
        state = wantstate15;
        timeInThisState = 300;
        break;
 
  case wantstate15:
        for (pos = 0; pos <= 30; pos +=1) {
         wingservo.write(pos);}
         state = wantstate16;
         timeInThisState = 300;
         break;

   case wantstate16:
        for (pos = 30; pos >= 0; pos -=1) {
        wingservo.write(pos);}
        state = randomPickedState;
        timeInThisState = 300;
        break;

   case wantstate17:
        for (pos = 0; pos <= 30; pos +=1) {
         wingservo.write(pos);}
         state = wantstate18;
         timeInThisState = 300;
         break;

   case wantstate18:
        for (pos = 30; pos >= 0; pos -=1) {
        wingservo.write(pos);}
        state = wantstate19;
        timeInThisState = 300;
        break;

   case wantstate19:
        for (pos = 0; pos <= 20; pos +=1) {
        legservo.write(pos);}
        state = wantstate20;
        timeInThisState = 300;
        break;

   case wantstate20:
        for (pos = 20; pos >=0; pos -=1) {
        legservo.write(pos);}
        state = randomPickedState;
        timeInThisState = random (2000-5000);
        break;

   case wantstate21:
        for (pos = 0; pos <= 20; pos +=1) {
        legservo.write(pos);}
        state = wantstate22;
        timeInThisState = 300;
        break;

   case wantstate22:
        for (pos = 20; pos >=0; pos -=1) {
        legservo.write(pos);}
        state = wantstate23;
        timeInThisState = random (20000-120000);
        break;

  case wantstate23:
         blueledFader.off ();
         redledFader.on ();
         state = randomPickedState;
         timeInThisState = 1000;
         break;

    }
  }

void loop ()
  {
   if (millis () - lastStateChange >= timeInThisState)
     doStateChange ();
      redledFader.update ();
      blueledFader.update ();
  }

to test it, you would have to breadboard the hardware, set it going and wait to see how it functions and if any are left out, which given the length of code, how long in time it runs for in each function could take a long time.

No, you simply write a small test sketch like this

const byte test[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.println(test[random(10)]);
  delay(100);
}

Then check that the output has the required range, ie 10 to 19 in this case

ok so i got a servo arrive today, so i linked that up with all the servo outputs connected to that servo as only one servo command is activated at any given time there wouldnt be any cross signal, for all the digitalwrite pins and 2 leds i connected leds in the motor/solenoid places.

when i uploaded the code, i get nothing happen. i havent been able to do the define part and need help there i think

i linked that up with all the servo outputs connected to that servo

Please explain exactly what you mean by this

Please post the full sketch that you are testing

You should never connect pins configured as output together.

secretreeve:

#include <Servo.h>

#include <LedFader.h>

// pin assignments
const byte eyemotor = 1;
const byte redled = 10;  //pwm
const byte blueled = 9;  //pwm
Servo headservo;
Servo legservo;
Servo wingservo;
int solenoid = 2;

// Faders                pin           min  max  millis    on?    stop?
LedFader redledFader      (redled,      0,   40,  1000,   false,  true);
LedFader blueledFader     (blueled,     0,   40,  1000,   false,  true);

typedef enum
 {
  initialState,
  wantstate1,  //use for state ransomiser
  wantstate2,
  wantstate3,
  wantstate4,  //use for state ransomiser
  wantstate5,
  wantstate6,  //use for state ransomiser
  wantstate7,
  wantstate8,
  wantstate9,
  wantstate10,
  wantstate11,  //use for state ransomiser
  wantstate12,  //use for state ransomiser
  wantstate13,
  wantstate14,
  wantstate15,
  wantstate16,
  wantstate17,  //use for state ransomiser
  wantstate18,
  wantstate19,
  wantstate20,
  wantstate21,  //use for state ransomiser
  wantstate22,
  wantstate23,  //use for state ransomiser
          } states;

states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 5;
int pos = 0;
int myRandomStates[8] = {wantstate1, wantstate4, wantstate6, wantstate12, wantstate11, wantstate17, wantstate21, wantstate23};
int randomPickedState = myRandomStates[random(8)];

void setup ()
 {
 pinMode (solenoid, OUTPUT);
 pinMode (eyemotor, OUTPUT);
 headservo.attach(3);  //pwm
 wingservo.attach(5);  //pwm
 legservo.attach(6);   //pwm
 redledFader.begin ();
 blueledFader.begin ();
 }
       
void doStateChange ()
 {
 lastStateChange = millis ();
 timeInThisState = 5;

switch (state)
  {
   case initialState:
        state = wantstate1;
        break;

case wantstate1:
        redledFader.off ();
        blueledFader.on ();
        state = randomPickedState;
        timeInThisState = 1000;
        break;
       
   case wantstate2:
        for (pos = 0; pos <= 70; pos +=1) {
        headservo.write(pos);}
        state = wantstate3;
        timeInThisState = random (1000-3000);
        break;

case wantstate3:
        for (pos = 70; pos >= -70; pos -=1) {
        headservo.write(pos);}
        state = wantstate4;
        timeInThisState = random (1000 - 3000);
        break;
           
   case wantstate4:
        for (pos = -70; pos <= 0; pos +=1){
        headservo.write(pos);}
        state = randomPickedState;
        timeInThisState = 300;
        break;

case wantstate5:
        digitalWrite (eyemotor, HIGH);
        state = wantstate6;
        timeInThisState = random (5000 - 8000);
        break;

case wantstate6:
        digitalWrite (eyemotor, LOW);
        state = randomPickedState;
        timeInThisState = 1000;
        break;
       
   case wantstate7:
        digitalWrite (solenoid, HIGH);
        state = wantstate8;
        timeInThisState = 300;
        break;

case wantstate8:
        digitalWrite (solenoid, LOW);
        state = wantstate9;
        timeInThisState = random (1000-3000);
        break;

case wantstate9:
        digitalWrite (solenoid, HIGH);
        state = wantstate10;
        timeInThisState = 300;
        break;

case wantstate10:
        digitalWrite (solenoid, LOW);
        state = randomPickedState;
        timeInThisState = random (1000-3000);
        break;

case wantstate11:
        digitalWrite (solenoid, HIGH);
        state = wantstate12;
        timeInThisState = 300;
        break;

case wantstate12:
        digitalWrite (solenoid, LOW);
        state = randomPickedState;
        timeInThisState = random (8000-30000);
        break;

case wantstate13:
       for (pos = 0; pos <= 30; pos +=1) {
        wingservo.write(pos);}
        state = wantstate14;
        timeInThisState = 300;
        break;

case wantstate14:
       for (pos = 30; pos >= 0; pos -=1) {
       wingservo.write(pos);}
       state = wantstate15;
       timeInThisState = 300;
       break;

case wantstate15:
       for (pos = 0; pos <= 30; pos +=1) {
        wingservo.write(pos);}
        state = wantstate16;
        timeInThisState = 300;
        break;

case wantstate16:
       for (pos = 30; pos >= 0; pos -=1) {
       wingservo.write(pos);}
       state = randomPickedState;
       timeInThisState = 300;
       break;

case wantstate17:
       for (pos = 0; pos <= 30; pos +=1) {
        wingservo.write(pos);}
        state = wantstate18;
        timeInThisState = 300;
        break;

case wantstate18:
       for (pos = 30; pos >= 0; pos -=1) {
       wingservo.write(pos);}
       state = wantstate19;
       timeInThisState = 300;
       break;

case wantstate19:
       for (pos = 0; pos <= 20; pos +=1) {
       legservo.write(pos);}
       state = wantstate20;
       timeInThisState = 300;
       break;

case wantstate20:
       for (pos = 20; pos >=0; pos -=1) {
       legservo.write(pos);}
       state = randomPickedState;
       timeInThisState = random (2000-5000);
       break;

case wantstate21:
       for (pos = 0; pos <= 20; pos +=1) {
       legservo.write(pos);}
       state = wantstate22;
       timeInThisState = 300;
       break;

case wantstate22:
       for (pos = 20; pos >=0; pos -=1) {
       legservo.write(pos);}
       state = wantstate23;
       timeInThisState = random (20000-120000);
       break;

case wantstate23:
        blueledFader.off ();
        redledFader.on ();
        state = randomPickedState;
        timeInThisState = 1000;
        break;

}
 }

void loop ()
 {
  if (millis () - lastStateChange >= timeInThisState)
    doStateChange ();
     redledFader.update ();
     blueledFader.update ();
 }

i mean i instead of placing motors on the breadboard or a solenoid, i have used leds to simulate them in place till they arrive.

const byte redled = 10;  //pwm
const byte blueled = 9;  //pwm
Servo headservo;

Oops.

TheMemberFormerlyKnownAsAWOL:

const byte redled = 10;  //pwm

const byte blueled = 9;  //pwm
Servo headservo;



Oops.

look at void setup, instead of posting "oops" make a useful comment please otherwise how does anyone learn

Oops.
Check the documentation for Servo

(Why should I look at the setup function?)

look at it! look your comments arent helping, they are setup exactly as they would be in the servo example sweep. can you please be more helpful than actually just posting random crap that doesnt help at all please

modified 8 Nov 2013
by Scott Fitzgerald

*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
for (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
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

once more heres the code

Did you read the documentation for Servo?
Did you read what it said about pins 9 & 10?

Please slow down, and think before posting your random thoughts.

Please remember to use code tags when posting random code.