LED Scene selection?

Im trying to figure out how to have a button toggle between different "scenes" in an LED bank.

Example:

When powered on, it runs "Scene 1".

On the first button press, it moves to "Scene 2".

Next press, Scene 3, etc.

I already know how to write the scenes, but how would I go about the scene toggle?

Thank you in advance.

Each time you press the switch you can increment a variable, call it a counter. At some point you can have the variable reset to 0. In your code you check your variable, when it reaches a certain value you run an associated scene.

See if()/else if() and switch/case.

You can also write your code using the 'state machine' concept.

.

Im trying to use the StateChangeDetection example, and Ive got it working (sort of). How would I have it pull the scene instead of just turning a single led on and off?

Im not very good with the programming side of this yet.

Always show us your current compete sketch.
Use CTRL T to format the sketch.
Please use code tags.
Use the </> icon in the posting menu.

[code] Paste sketch here. [/code]

.

int R = 2;
int I = 3;
int C = 4;
int K = 5;
int Y = 6;
int D = 7;
int A = 8;
int L = 9;
int E = 10;

const int select = 13;
const int maxScene = 2;

int scene = 0;


void setup() {
  pinMode(select, INPUT_PULLUP);
  pinMode(R, OUTPUT);
  pinMode(I, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(K, OUTPUT);
  pinMode(Y, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(A, OUTPUT);
  pinMode(L, OUTPUT);
  pinMode(E, OUTPUT);


}

void loop() {
  switch (scene) {
    case 0:

      digitalWrite(R, HIGH);
      delay (500);
      digitalWrite(R, LOW);
      delay (500);
      digitalWrite(I, HIGH);
      delay (500);
      digitalWrite(I, LOW);
      delay (500);
      digitalWrite(C, HIGH);
      delay (500);
      digitalWrite(C, LOW);
      delay (500);
      digitalWrite(K, HIGH);
      delay (500);
      digitalWrite(K, LOW);
      delay (500);
      digitalWrite(Y, HIGH);
      delay (500);
      digitalWrite(Y, LOW);
      delay (500);
      digitalWrite(D, HIGH);
      delay (500);
      digitalWrite(D, LOW);
      delay (500);
      digitalWrite(A, HIGH);
      delay (500);
      digitalWrite(A, LOW);
      delay (500);
      digitalWrite(L, HIGH);
      delay (500);
      digitalWrite(L, LOW);
      delay (500);
      digitalWrite(E, HIGH);
      delay (500);
      digitalWrite(E, LOW);
      delay (500);
      break;

    case 1:
      digitalWrite(R, HIGH);
      delay(100);
      digitalWrite(R, LOW);
      delay(100);
      digitalWrite(I, HIGH);
      delay(100);
      digitalWrite(I, LOW);
      delay(100);
      digitalWrite(C, HIGH);
      delay(100);
      digitalWrite(C, LOW);
      delay(100);
      digitalWrite(K, HIGH);
      delay(100);
      digitalWrite(K, LOW);
      delay(100);
      digitalWrite(Y, HIGH);
      delay(100);
      digitalWrite(Y, LOW);
      delay(100);
      digitalWrite(D, HIGH);
      delay(100);
      digitalWrite(D, LOW);
      delay(100);
      digitalWrite(A, HIGH);
      delay(100);
      digitalWrite(A, LOW);
      delay(100);
      digitalWrite(L, HIGH);
      delay(100);
      digitalWrite(L, LOW);
      delay(100);
      digitalWrite(E, HIGH);
      delay(100);
      digitalWrite(E, LOW);
      delay(100);
      digitalWrite(L, HIGH);
      delay(100);
      digitalWrite(L, LOW);
      delay(100);
      digitalWrite(A, HIGH);
      delay(100);
      digitalWrite(A, LOW);
      delay(100);
      digitalWrite(D, HIGH);
      delay(100);
      digitalWrite(D, LOW);
      delay(100);
      digitalWrite(Y, HIGH);
      delay(100);
      digitalWrite(Y, LOW);
      delay(100);
      digitalWrite(K, HIGH);
      delay(100);
      digitalWrite(K, LOW);
      delay(100);
      digitalWrite(C, HIGH);
      delay(100);
      digitalWrite(C, LOW);
      delay(100);
      digitalWrite(I, HIGH);
      delay(100);
      digitalWrite(I, LOW);
      delay(100);
      digitalWrite(R, HIGH);
      delay(100);
      digitalWrite(R, LOW);
      delay(100);
      break;

case2:
      digitalWrite(R, HIGH);
      digitalWrite(E, HIGH);
      delay(100);
      digitalWrite(R, LOW);
      digitalWrite(E, LOW);
      delay(100);
      digitalWrite(I, HIGH);
      digitalWrite(L, HIGH);
      delay(100);
      digitalWrite(I, LOW);
      digitalWrite(L, LOW);
      delay(100);
      digitalWrite(C, HIGH);
      digitalWrite(A, HIGH);
      delay(100);
      digitalWrite(C, LOW);
      digitalWrite(A, LOW);
      delay(100);
      digitalWrite(K, HIGH);
      digitalWrite(D, HIGH);
      delay(100);
      digitalWrite(K, LOW);
      digitalWrite(D, LOW);
      delay(100);
      digitalWrite(Y, HIGH);
      delay(100);
      digitalWrite(Y, LOW);
      delay(100);
      digitalWrite(K, HIGH);
      digitalWrite(D, HIGH);
      delay(100);
      digitalWrite(K, LOW);
      digitalWrite(D, LOW);
      delay(100);
      digitalWrite(C, HIGH);
      digitalWrite(A, HIGH);
      delay(100);
      digitalWrite(C, LOW);
      digitalWrite(A, LOW);
      delay(100);
      digitalWrite(I, HIGH);
      digitalWrite(L, HIGH);
      delay(100);
      digitalWrite(I, LOW);
      digitalWrite(L, LOW);
      delay(100);
      digitalWrite(R, HIGH);
      digitalWrite(E, HIGH);
      delay(100);
      digitalWrite(R, LOW);
      digitalWrite(E, LOW);
      delay(100);
      break;





  }

}
void myDelay(int delayTime) {
  if (digitalRead(select) == LOW) {
    scene++;
    if (scene > maxScene) {
      scene = 0;
    }
  }
  delay(delayTime);
}

This is where I am with it. I can only get it to run the first pattern, and can not get it to switch to the others.

You never 'CALL' myDelay() therefore the code inside the function never runs and 'scene' never increments.

Im sorry, I dont understand. Do i need to add something like:

int myDelay();

?

You never enter the myDelay() function where the incrementing happens. As a result, 'scene' is always 0

BTW You should learn how to use arrays in your code. You should learn how a 'for' loop works.

.

Thank you for all of your help so far, I really appreciate it.

I am not a programmer, Im a hardware guy. Everything I have done so far has come from google, the forums, and Frankensteining everything together as best I can. I have ordered a book, but it has not come in yet.

I looked up arrays, and that will help with the scenes immensely! I sort of get the idea of the "for loop", but thats going to take a bit more studying.

As for the myDelay, I am totally lost. I understand that I am missing a line of code, and I am assuming that it will go into the setup field. As to what function needs to be applied to that, I have no idea.

Thank you again for your assistance larryd.

Every place where you have "delay( some value )" in your code you replace it with "myDelay ( some value )"

Grumpy_Mike: Every place where you have "delay( some value )" in your code you replace it with "myDelay ( some value )"

This helped a lot, thank you!

So now, when it is powered up, scene 0 runs. On the first press, it switches to scene 1. The next press, all of the LED's go out, and none of the scenes will run until I cycle power the Uno.

int R = 2;
int I = 3;
int C = 4;
int K = 5;
int Y = 6;
int D = 7;
int A = 8;
int L = 9;
int E = 10;

const int select = 13;
const int maxScene = 3;

int scene = 0;


void setup() {
  pinMode(select, INPUT_PULLUP);
  pinMode(R, OUTPUT);
  pinMode(I, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(K, OUTPUT);
  pinMode(Y, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(A, OUTPUT);
  pinMode(L, OUTPUT);
  pinMode(E, OUTPUT);


}

void loop() {
  switch (scene) {
    case 0:

      digitalWrite(R, HIGH);
      myDelay (500);
      digitalWrite(R, LOW);
      myDelay (500);
      digitalWrite(I, HIGH);
      myDelay (500);
      digitalWrite(I, LOW);
      myDelay (500);
      digitalWrite(C, HIGH);
      myDelay (500);
      digitalWrite(C, LOW);
      myDelay (500);
      digitalWrite(K, HIGH);
      myDelay (500);
      digitalWrite(K, LOW);
      myDelay (500);
      digitalWrite(Y, HIGH);
      myDelay (500);
      digitalWrite(Y, LOW);
      myDelay (500);
      digitalWrite(D, HIGH);
      myDelay (500);
      digitalWrite(D, LOW);
      myDelay (500);
      digitalWrite(A, HIGH);
      myDelay (500);
      digitalWrite(A, LOW);
      myDelay (500);
      digitalWrite(L, HIGH);
      myDelay (500);
      digitalWrite(L, LOW);
      myDelay (500);
      digitalWrite(E, HIGH);
      myDelay (500);
      digitalWrite(E, LOW);
      myDelay (500);
      break;

    case 1:
      digitalWrite(R, HIGH);
      myDelay(100);
      digitalWrite(R, LOW);
      myDelay(100);
      digitalWrite(I, HIGH);
      myDelay(100);
      digitalWrite(I, LOW);
      myDelay(100);
      digitalWrite(C, HIGH);
      myDelay(100);
      digitalWrite(C, LOW);
      myDelay(100);
      digitalWrite(K, HIGH);
      myDelay(100);
      digitalWrite(K, LOW);
      myDelay(100);
      digitalWrite(Y, HIGH);
      myDelay(100);
      digitalWrite(Y, LOW);
      myDelay(100);
      digitalWrite(D, HIGH);
      myDelay(100);
      digitalWrite(D, LOW);
      myDelay(100);
      digitalWrite(A, HIGH);
      myDelay(100);
      digitalWrite(A, LOW);
      myDelay(100);
      digitalWrite(L, HIGH);
      myDelay(100);
      digitalWrite(L, LOW);
      myDelay(100);
      digitalWrite(E, HIGH);
      myDelay(100);
      digitalWrite(E, LOW);
      myDelay(100);
      digitalWrite(L, HIGH);
      myDelay(100);
      digitalWrite(L, LOW);
      myDelay(100);
      digitalWrite(A, HIGH);
      myDelay(100);
      digitalWrite(A, LOW);
      myDelay(100);
      digitalWrite(D, HIGH);
      myDelay(100);
      digitalWrite(D, LOW);
      myDelay(100);
      digitalWrite(Y, HIGH);
      myDelay(100);
      digitalWrite(Y, LOW);
      myDelay(100);
      digitalWrite(K, HIGH);
      myDelay(100);
      digitalWrite(K, LOW);
      myDelay(100);
      digitalWrite(C, HIGH);
      myDelay(100);
      digitalWrite(C, LOW);
      myDelay(100);
      digitalWrite(I, HIGH);
      myDelay(100);
      digitalWrite(I, LOW);
      myDelay(100);
      digitalWrite(R, HIGH);
      myDelay(100);
      digitalWrite(R, LOW);
      myDelay(100);
      break;

case2:
      digitalWrite(R, HIGH);
      digitalWrite(E, HIGH);
      myDelay(100);
      digitalWrite(R, LOW);
      digitalWrite(E, LOW);
      myDelay(100);
      digitalWrite(I, HIGH);
      digitalWrite(L, HIGH);
      myDelay(100);
      digitalWrite(I, LOW);
      digitalWrite(L, LOW);
      myDelay(100);
      digitalWrite(C, HIGH);
      digitalWrite(A, HIGH);
      myDelay(100);
      digitalWrite(C, LOW);
      digitalWrite(A, LOW);
      myDelay(100);
      digitalWrite(K, HIGH);
      digitalWrite(D, HIGH);
      myDelay(100);
      digitalWrite(K, LOW);
      digitalWrite(D, LOW);
      myDelay(100);
      digitalWrite(Y, HIGH);
      myDelay(100);
      digitalWrite(Y, LOW);
      myDelay(100);
      digitalWrite(K, HIGH);
      digitalWrite(D, HIGH);
      myDelay(100);
      digitalWrite(K, LOW);
      digitalWrite(D, LOW);
      myDelay(100);
      digitalWrite(C, HIGH);
      digitalWrite(A, HIGH);
      myDelay(100);
      digitalWrite(C, LOW);
      digitalWrite(A, LOW);
      myDelay(100);
      digitalWrite(I, HIGH);
      digitalWrite(L, HIGH);
      myDelay(100);
      digitalWrite(I, LOW);
      digitalWrite(L, LOW);
      myDelay(100);
      digitalWrite(R, HIGH);
      digitalWrite(E, HIGH);
      myDelay(100);
      digitalWrite(R, LOW);
      digitalWrite(E, LOW);
      myDelay(100);
      break;





  }

}
void myDelay(int delayTime) {
  if (digitalRead(select) == LOW) {
    scene++;
    if (scene > maxScene) {
      scene = 0;
    }
  }
  delay(delayTime);
}

case2: ????

larryd: case2: ????

Ugh. I don't know how I missed that.

Thank you!!!

That still didnt fix it. I played around with my maxScene, thinking that had something to do with it.

How does that work exactly? Im using case0, case1, and case2. Would the maxScene be 2 or 3? Does the 0 count there?

case 0:

That worked, right?

case 1:

And that worked?

case2:

But this did not?

So I assume that you did change this to case 2:

Now you are concerned about maxScene values. You had 3 and you had poor results.
Try 2 and let us know how it goes.

int R = 2;
int I = 3;
int C = 4;
int K = 5;
int Y = 6;
int D = 7;
int A = 8;
int L = 9;
int E = 10;

const int select = 12;
const int maxScene = 3;

int scene = 0;


void setup() {
  pinMode(select, INPUT_PULLUP);
  pinMode(R, OUTPUT);
  pinMode(I, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(K, OUTPUT);
  pinMode(Y, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(A, OUTPUT);
  pinMode(L, OUTPUT);
  pinMode(E, OUTPUT);


}

void loop() {
  switch (scene) {
    case 0:
      digitalWrite(R, LOW);
      digitalWrite(I, LOW);
      digitalWrite(C, LOW);
      digitalWrite(K, LOW);
      digitalWrite(Y, LOW);
      digitalWrite(D, LOW);
      digitalWrite(A, LOW);
      digitalWrite(L, LOW);
      digitalWrite(E, LOW);
      delay(50);


      digitalWrite(R, HIGH);
      myDelay (100);
      digitalWrite(R, LOW);
      myDelay (100);
      digitalWrite(I, HIGH);
      myDelay (100);
      digitalWrite(I, LOW);
      myDelay (100);
      digitalWrite(C, HIGH);
      myDelay (100);
      digitalWrite(C, LOW);
      myDelay (100);
      digitalWrite(K, HIGH);
      myDelay (100);
      digitalWrite(K, LOW);
      myDelay (100);
      digitalWrite(Y, HIGH);
      myDelay (100);
      digitalWrite(Y, LOW);
      myDelay (100);
      digitalWrite(D, HIGH);
      myDelay (100);
      digitalWrite(D, LOW);
      myDelay (100);
      digitalWrite(A, HIGH);
      myDelay (100);
      digitalWrite(A, LOW);
      myDelay (100);
      digitalWrite(L, HIGH);
      myDelay (100);
      digitalWrite(L, LOW);
      myDelay (100);
      digitalWrite(E, HIGH);
      myDelay (100);
      digitalWrite(E, LOW);
      myDelay (100);
      break;

    case 1:
      digitalWrite(R, HIGH);
      myDelay(100);
      digitalWrite(R, LOW);
      myDelay(100);
      digitalWrite(I, HIGH);
      myDelay(100);
      digitalWrite(I, LOW);
      myDelay(100);
      digitalWrite(C, HIGH);
      myDelay(100);
      digitalWrite(C, LOW);
      myDelay(100);
      digitalWrite(K, HIGH);
      myDelay(100);
      digitalWrite(K, LOW);
      myDelay(100);
      digitalWrite(Y, HIGH);
      myDelay(100);
      digitalWrite(Y, LOW);
      myDelay(100);
      digitalWrite(D, HIGH);
      myDelay(100);
      digitalWrite(D, LOW);
      myDelay(100);
      digitalWrite(A, HIGH);
      myDelay(100);
      digitalWrite(A, LOW);
      myDelay(100);
      digitalWrite(L, HIGH);
      myDelay(100);
      digitalWrite(L, LOW);
      myDelay(100);
      digitalWrite(E, HIGH);
      myDelay(100);
      digitalWrite(E, LOW);
      myDelay(100);
      digitalWrite(L, HIGH);
      myDelay(100);
      digitalWrite(L, LOW);
      myDelay(100);
      digitalWrite(A, HIGH);
      myDelay(100);
      digitalWrite(A, LOW);
      myDelay(100);
      digitalWrite(D, HIGH);
      myDelay(100);
      digitalWrite(D, LOW);
      myDelay(100);
      digitalWrite(Y, HIGH);
      myDelay(100);
      digitalWrite(Y, LOW);
      myDelay(100);
      digitalWrite(K, HIGH);
      myDelay(100);
      digitalWrite(K, LOW);
      myDelay(100);
      digitalWrite(C, HIGH);
      myDelay(100);
      digitalWrite(C, LOW);
      myDelay(100);
      digitalWrite(I, HIGH);
      myDelay(100);
      digitalWrite(I, LOW);
      myDelay(100);
      break;

    case 2:
      digitalWrite(R, HIGH);
      digitalWrite(E, HIGH);
      myDelay(100);
      digitalWrite(R, LOW);
      digitalWrite(E, LOW);
      myDelay(100);
      digitalWrite(I, HIGH);
      digitalWrite(L, HIGH);
      myDelay(100);
      digitalWrite(I, LOW);
      digitalWrite(L, LOW);
      myDelay(100);
      digitalWrite(C, HIGH);
      digitalWrite(A, HIGH);
      myDelay(100);
      digitalWrite(C, LOW);
      digitalWrite(A, LOW);
      myDelay(100);
      digitalWrite(K, HIGH);
      digitalWrite(D, HIGH);
      myDelay(100);
      digitalWrite(K, LOW);
      digitalWrite(D, LOW);
      myDelay(100);
      digitalWrite(Y, HIGH);
      myDelay(100);
      digitalWrite(Y, LOW);
      myDelay(100);
      digitalWrite(K, HIGH);
      digitalWrite(D, HIGH);
      myDelay(100);
      digitalWrite(K, LOW);
      digitalWrite(D, LOW);
      myDelay(100);
      digitalWrite(C, HIGH);
      digitalWrite(A, HIGH);
      myDelay(100);
      digitalWrite(C, LOW);
      digitalWrite(A, LOW);
      myDelay(100);
      digitalWrite(I, HIGH);
      digitalWrite(L, HIGH);
      myDelay(100);
      digitalWrite(I, LOW);
      digitalWrite(L, LOW);
      myDelay(100);

      break;

    case 3:
      digitalWrite(D, HIGH);
      digitalWrite(A, HIGH);
      digitalWrite(L, HIGH);
      digitalWrite(E, HIGH);

      digitalWrite(R, HIGH);
      digitalWrite(I, HIGH);
      digitalWrite(C, HIGH);
      digitalWrite(K, HIGH);
      digitalWrite(Y, HIGH);
      myDelay(100);

      digitalWrite(R, LOW);
      digitalWrite(I, LOW);
      digitalWrite(C, LOW);
      digitalWrite(K, LOW);
      digitalWrite(Y, LOW);
      myDelay(100);

      break;





  }

}
void myDelay(int delayTime) {
  if (digitalRead(select) == LOW) {
    scene++;
    if (scene > maxScene) {
      scene = 0;
    }
  }
  delay(delayTime);
}

Final product, working as I wanted. I'm sure there is an easier way to do this, but I'm happy with it.

Thank you all for the help.

Your myDelay function is quite frankly rubbish. The idea is to remove ALL delay calls and you still have one in the myDelay function.
That function should hold in a loop until the delay time is finished as measured by the millis timer OR the push button has been pressed.
Do not try any push counting in this functionality.

Hi,

I was also trying the same kind of programming, I am not successful with it. Scene is not switching to different cases.
I tried your above codes with my board as it is, only one case is working other two are not worked.
could you succeed with the above code?