0

I am using the following code to create a snake-like affect (3 LED's wide) moving through 24 LED's using 3 shift registers:

 for(int i = 0; i < 27; i++){
    setRegisterPin(i, HIGH);
    writeRegisters();
    if(i >= 3){
     setRegisterPin(i-LEDLength, LOW);
     writeRegisters(); 
    } 
    delay(100);
  }//end for

The problem with this is that the snake has to completely end before it starts again, instead of looking like one continuous snake going in circles.

So when the three LED's get to the end I want it to immediately begin to light the beginning again. If 2 are lit at the end then 1 should be lit at the start, etc. Like this pattern:

๐Ÿ˜Ž ๐Ÿ˜Ž ๐Ÿ˜Ž ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ

๐Ÿ˜Ž ๐Ÿ˜Ž ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜Ž

๐Ÿ˜Ž ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜Ž ๐Ÿ˜Ž

๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜Ž ๐Ÿ˜Ž ๐Ÿ˜Ž

๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜ถ ๐Ÿ˜Ž ๐Ÿ˜Ž ๐Ÿ˜Ž ๐Ÿ˜ถ

enter image description here

4

You mean something like this? (untested):

void loop() {
  // use static or global variables to remember positions (these are initialized only once):
  static uint8_t hi_pos = 0;  // where HIGH should be set
  static uint8_t lo_pos = 21; // where LOW should be set

  setRegisterPin(hi_pos, HIGH); // turn on led in head
  setRegisterPin(lo_pos, LOW);  // turn off led in tail
  writeRegisters();             // update registers

  // ensure it won't overflow:
  if (++hi_pos == 24) hi_pos = 0;
  if (++lo_pos == 24) lo_pos = 0;

  // or:  hi_pos = (hi_pos+1) % 24;  // but divisions are slower on AVR

  delay(100);
}

It will go in sequence:

ON  OFF
 0   21
 1   22
 2   23
 3    0
 4    1
...
22   19
23   20
0    21
...

forever

  • wow, I'm not sure how you did that but it works. Can you explain the code further please? – Michael Rader Sep 6 '16 at 6:04

Your Answer

By clicking โ€œPost Your Answerโ€, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.