randomizing case want selection

what did it say about 9 and 10? my previous post was the wrong code.

for refresher for anyone else viewing this is the code in question

//bubo

#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[9] = {wantstate1, wantstate4, wantstate5, wantstate7, 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 = 500;
         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 ();
  }
const byte redled = 10;  //pwm
const byte blueled = 9;  //pwm
Servo headservo;

Still, oops

Just because you've now for over a hundred posts, and can post in less than five minutes doesn't mean that you should

okay so no pwm on 9 and 10, fair enough, but would that prevent the remainder of the code working?

cause not even the initial leds are working

TheMemberFormerlyKnownAsAWOL:

const byte redled = 10;  //pwm

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



Still, oops

Just because you've now for over a hundred posts, and can post in less than five minutes doesn't mean that you should

and just cause you have a few thousand doesnt mean you have the right to post derogatory comments that arent helpul to the thread

Clearly "derogatory" means something different in your language.

Good night.
(Report to moderator?
Seriously?
Careful with that - it's a double-edged sword)

all you have done since you came on this thread and most of my other threads i have started over time is post unhelpful and pointless posts in order to rack up your post count.

how about you leave my threads alone unless you legitely want to help

secretreeve:
all you have done since you came on this thread and most of my other threads i have started over time

If you can clearly show that I have posted, helpfully or otherwise, on at least 50% of your other threads, I'll apologise to you.

Otherwise, you'll do the same to me.

i wont appologise to you because i dont have to prove anything to you. i asked a simple question that you have completely ignored. that in itself speaks more than words.

thanks for derailing a thread by posting unhelpful comments that dont inform anyone of anything and being a complete and utter moron. thinking your better than everyone else when frankly your little more than an internet troll getting his rocks off annoying people and preventing them from getting the help they actually need.

i honestly hope to god your not a teacher because you'd make a horrible one. you dont help, you degrade, berate and annoy those you consider below you. honesty, you make me sick

okay so considering i did ask this in another thread but it was derailed unnecessarily im starting this again,

i have been working on some code for an animated owl and i have loaded the code into an arduino uno and bread boarded it substituting the motors and solenoid for led's as im still waiting for those to arrive.

i've not had any responce from the code at all, all the wiring is fine so thats not a problem.

im aware of pwm on 9 and 10 being disabled for servos and that needs changing but the first case state calls for leds to function yet they dont even flicker an inch of life.

heres the code

//bubo

#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[9] = {wantstate1, wantstate4, wantstate5, wantstate7, 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 = 500;
         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 ();
  }

That's really not fair to the people who could answer this here, or the people who already answered you. If you are going to double post, please at least provide a link to the other thread.

Is there a question there?
Is your shift key broken? Your lack of proper punctuation makes it difficult to read your post.
Without a schematic, we would be guessing.

theres no problem with the hwardware, the code itself isnt running

secretreeve:
theres no problem with the hwardware, the code itself isnt running

You have basically said:
I have wired stuff up and it doesn't work.

For anyone to help you they will need to know how you actually wired it up and what is actually happening. Look up the forum guidelines (sticky at top of every section). It tells you how to best form a question, what information to include, how to post a picture and how to use code tags.

At the very least, post a schematic. (Please, NOT a pretty Fritzing picture).

"code isn't running" is beyond vague. How do you know?
You will have to give a proper description of what you expect the code to do and what it actually does; "it's not working" doesn't mean anything.

This is not a suggestion. I'm not a moderator, but double posting is not allowed in this forum (and this thread illustrates why).

okay so everything is wired up as per the code pin assignments. the wiring isnt difficult to do,

i cant give you a schematic because i havent drawn one up as i dont need to.

i get no errors in the compiling or uploading to arduino but nothing actually operates, so the quiestion is can anyone see a problem with the code that would prevent it from working?

aarg:
This is not a suggestion. I'm not a moderator, but double posting is not allowed in this forum (and this thread illustrates why).

ive restarted this thread because one individual derailed the previous.

Please stay calm,everyone...

@secretreeve, you need to read the language reference, it provides many information that can help you.
The same for libraries you use. BTW I couldn't find the LedFader library you use (only a LEDFader which seems different) so I can't help on this. I assume you use it correctly.

Anyways, when you want to create a complex system, complex in the sense that it uses several different components, you should first try to master each single component before to integrate everything. This helps a lot. When you know how to use each single element, it's easier to make the whole thing work.

So you should first try to fade your leds: maybe you'll find why they don't lit up.
Then try the examples of the servo library

I think you use the random function wrongly. When you write for example:

timeInThisState = random (1000 - 3000);

What do you mean exactly? This should provide a negative random number (or maybe some unexpected result) which can't be a time.
Do you want a random number between 1000 and 3000? In this case the syntax is

timeInThisState = random (1000, 3000);

Be aware that this never gives 3000.

I'm not sure to understand what you want to do in the doStateChange function. You consult millis() but never use it. You set a duration but never check it.
If I'm guessing well, you want your animation to last some given time. So instead of

void doStateChange ()
  {
  lastStateChange = millis ();
  timeInThisState = 5;

you should do something like this:

void doStateChange ()
  {
  if (millis() - lastStateChange  < timeInThisState) return;
  lastStateChange = millis ();

Here you effectively change the animation after the duration is passed.
Hope it helps, and cools the local temperature down... :slight_smile:

secretreeve:
ive restarted this thread because one individual derailed the previous.

Only a moderator can do that. You must have broken a forum rule.

I actually answered in the other thread before finding this new one...

You should in any case provide all information that can be relevant: at least

  • what arduino board do you use (pwm are different from a board to another)
  • where are the libraries you use (I couldn't find the ledFader library)
  • how is everything wired (if a led doesnt make light or a servo doesn't move, it may be a connection problem)
  • explain what the code is expected to do, and what you get instead
  • for debugging purpose, use the serial monitor and put printlns inside your switch/case to follow the execution of the code

lesept:
I actually answered in the other thread before finding this new one...

You should in any case provide all information that can be relevant: at least

  • what arduino board do you use (pwm are different from a board to another)
  • where are the libraries you use (I couldn't find the ledFader library)
  • how is everything wired (if a led doesnt make light or a servo doesn't move, it may be a connection problem)
  • explain what the code is expected to do, and what you get instead
  • for debugging purpose, use the serial monitor and put printlns inside your switch/case to follow the execution of the code

i will answer these as best as i can tomorrow, its 1am here and im worked up and fustrated with the other thread so i will try to provide the information, however its hard to explain what the code is expected to do and the code itself is most likelt to explain that in itself.
as for your reply on the other thread, i greatly appreciate that but having difficulty in understanding the define area you mentioned, i attempted it but got errors and i have a sneaking suspision its partly related to that.
one thing i want to do is remove the random element to state want selection and see what happens.