A strange glitch

So, here is the code I am currently using...

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);
  Serial.begin (9600);


}

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(500);

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

      break;





  }

}
void myDelay(int delayTime) {
  if (digitalRead(select) == LOW)
    Serial.println ("Scene Change");
  {
    scene++;
    if (scene > maxScene) {
      scene = 0;
    }

  }
  delay(delayTime);
}

I wrote this, and tested it using an Uno and a breadboard. I had everything working flawlessly.

Well, on my final product, I am using a Nano. The code uploads fine, but once it starts, it glitches out. It runs case 0 one time, then goes to case 2, and will not change from there. I have tested my connections with a meter, and everything is good there.

I added in the Serial lines in hopes I could see when the button is pressed.

}
void myDelay(int delayTime) {
  if (digitalRead(select) == LOW)
    Serial.println ("Scene Change");
  {
    scene++;
    if (scene > maxScene) {
      scene = 0;
    }

  }
  delay(delayTime);
}

When it is set to High, I get no feedback on the serial monitor. When it is set to low, I get constant feedback.

My only thought is that I am powering it off of my laptop right now, and the supply voltage may not be enough.

Anyone see something that I dont?

I had everything working flawlessly.

What does that mean?

(If it helps, imagine for a moment that I have no idea what your code is supposed to do. This shouldn't tax your imagination, because I don't)

When I powered the Uno on, it would start running the first scene. Every time I pressed the button, the scene would change to the next one.

  if (digitalRead(select) == LOW)
    Serial.println ("Scene Change");
  {
    scene++;

    }

There's only one conditional action there, but the braces suggest to me that you intended something else.

AWOL:
There's only one conditional action there, but the braces suggest to me that you intended something else.

Im not sure I understand. I added the Serial.println just a few minutes ago, and wasn't sure where I should put it. I tried a few different places in that section, but I didn't seem to get any different result.

OK, here's what the code does:
If the pin reads LOW, then print something.
Whether or not the pin was LOW, increment scene.
Is that what you intended?

I intended for the scene to increment by 1, and for the line to print when the switch, which is a NO momentary switch, is activated.

Is it time for some careful thought and a rewrite?

(For me, it's time for bed)

AWOL:
Is it time for some careful thought and a rewrite?

(For me, it's time for bed)

I guess.

Does digitalRead == LOW when you push the button??

And you expect that each time you push the button, it'll increment scene? I'm not sure about that. Maybe if you hold down the button you'll notice something, if it's not getting into those braces in myDelay()

Also, that if statement in myDelay() is wrong when you've added the Serial.println:

void myDelay(int delayTime) {
  if (digitalRead(select) == LOW)
    Serial.println ("Scene Change");
  {
    scene++;
    if (scene > maxScene) {
      scene = 0;
    }

  }
  delay(delayTime);
}

should be:

void myDelay(int delayTime) {
  if (digitalRead(select) == LOW)
  {
    Serial.println ("Scene Change");
    scene++;

    if (scene > maxScene) {
      scene = 0;
    }
    Serial.print("Scene:");
    Serial.println(scene);
  }
  delay(delayTime);
}

(I added some extra bits for debugging purposes).

It might be useful to show us the Serial output :slight_smile:

Edit: You're sure this worked as intended on an Uno?? :\

Bilz:
]

(I added some extra bits for debugging purposes).

It might be useful to show us the Serial output :slight_smile:

Edit: You're sure this worked as intended on an Uno?? :\

When I use your code, I get the same result. One cycle of case 0, then loops of case 2. I get constant feedback in the serial monitor, even when checking the switch with a meter to see that it is not closed.

I had it working, because I was getting help in another thread. I went back and copied the code from there, and I can not get it to work again.

I am not a programmer. But, Im learning. Everything I have done has come from pieces of other examples, both here and on the internet.

Pin 13 is different on Nano than UNO, try another pin for "select".

edgemoron:
Pin 13 is different on Nano than UNO, try another pin for "select".

I was reading about this earlier. Thank you.

I tried rewriting the entire code, using a different method.

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);
  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);
  Serial.begin (9600);
}
void loop() {

  int switchVal = digitalRead(select);
  if (switchVal == HIGH)
  {
    delay(500);
    Serial.println ("Scene Change");
    scene++;

    if (scene == 4) {
      scene = 0;
    }
    Serial.print("Scene");
    Serial.println(scene);
  }
  else

    switch (scene) {
      case 0:



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

      case 2:
        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);

        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);
        delay(500);

        digitalWrite(R, LOW);
        digitalWrite(I, LOW);
        digitalWrite(C, LOW);
        digitalWrite(K, LOW);
        digitalWrite(Y, LOW);
        delay(500);

        break;





    }
}

I tried using this, with pin 12 as the input, and still could not get it to properly change the scenes.

Please explain how your switch is wired.

What values are printed for "scene" when you push the switch?

Hint: the more information you provide, the better others can help.

jremington:
Please explain how your switch is wired.

What values are printed for "scene" when you push the switch?

Hint: the more information you provide, the better others can help.

My switch is wired as a NO momentary switch between pin 12 and ground.

The scene serial lines were added earlier in this thread, by Bilz. In the serial monitor, they show which scene has been selected.

Progress!

I moved the button from 13 to 12, and tweaked a few lines of the code. It now changes the scenes, but the button functionality is a bit weird. In order for the scenes to change, you have to be holding down the button at the end of the scene sequence. Sometimes, this causes the scenes to skip around, if you hold down the button too long.

How would I go about having the scene change be an instant interrupt when the button is pressed?

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 = 4;
int scene = 0;


void setup() {
  pinMode(select, INPUT);
  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);
  Serial.begin (9600);
}
void loop() {

  int switchVal = digitalRead(select);
  if (switchVal == LOW)
  {

    Serial.println ("Scene Change");
    delay(500);
    scene++;

    if (scene == 5) {
      scene = 0;
    }
    Serial.print("Scene");
    Serial.println(scene);
  }

  else

    switch (scene) {
      case 0:



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

      case 2:
        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);

        break;

      case 3:


        digitalWrite(R, HIGH);
        digitalWrite(I, HIGH);
        digitalWrite(C, HIGH);
        digitalWrite(K, HIGH);
        digitalWrite(Y, HIGH);
        delay(1000);

        digitalWrite(R, LOW);
        digitalWrite(I, LOW);
        digitalWrite(C, LOW);
        digitalWrite(K, LOW);
        digitalWrite(Y, LOW);
        delay(1000);

        digitalWrite(D, HIGH);
        digitalWrite(A, HIGH);
        digitalWrite(L, HIGH);
        digitalWrite(E, HIGH);
        delay(1000);

        digitalWrite(D, LOW);
        digitalWrite(A, LOW);
        digitalWrite(L, LOW);
        digitalWrite(E, LOW);
        delay(1000);
        break;


      case 4:

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


    }
}

How would I go about having the scene change be an instant interrupt when the button is pressed?

Rewrite your code and remove all the calls to delay()

AWOL:
Rewrite your code and remove all the calls to delay()

That's how I had it originally, but I was told that it was "rubbish". So, here we are.