Go Down

Topic: LED Scene selection? (Read 685 times) previous topic - next topic

Nancenificent

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.

larryd

#1
Aug 29, 2017, 12:18 am Last Edit: Aug 29, 2017, 12:24 am by larryd
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.
 
.
No technical PMs.
The last thing you did is where you should start looking.

Nancenificent

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.

larryd

#3
Aug 29, 2017, 01:10 am Last Edit: Aug 29, 2017, 01:10 am by larryd
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]



.
No technical PMs.
The last thing you did is where you should start looking.

Nancenificent

Code: [Select]

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.

larryd

#5
Aug 29, 2017, 04:03 am Last Edit: Aug 29, 2017, 04:14 am by larryd
You never 'CALL'  myDelay() therefore the code inside the function never runs and 'scene' never increments.
No technical PMs.
The last thing you did is where you should start looking.

Nancenificent

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

Code: [Select]


int myDelay();



?


larryd

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.

.
No technical PMs.
The last thing you did is where you should start looking.

Nancenificent

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.

Grumpy_Mike

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

Nancenificent

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.

Code: [Select]


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);
}


 

larryd

No technical PMs.
The last thing you did is where you should start looking.

Nancenificent

case2:   ????




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

Thank you!!!

Nancenificent

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?

vinceherman

Code: [Select]
case 0:
That worked, right?
Code: [Select]
case 1:
And that worked?
Code: [Select]
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.

Go Up