Help with Cylon Swipe

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
drumminhands
 
Posts: 36
Joined: Tue Jul 23, 2013 10:02 am

Help with Cylon Swipe

Post by drumminhands »

I could use some help with some code that I have plugged into a Gemma with NeoPixels in series. I'm trying to program the look of a cylon, but in the style of Blink Without Delay. There are two specific issues I'm having:

1) I can get the swipe up and down to happen. I'm struggling to add a pause before swiping up and down again. Right now, the program cylonEye() works without a pause. cylonEye_notWorking() is, well, not working. I've commented a few trouble spots that might be the issue below.

2) The programs cylonUp() and cylonDown() are only one line of code different. There should be a way to combine these into one program, but I'm not sure how.

Can anyone point me in the right direction?

Thanks.

Code: Select all

//NeoPixel LED Digital Strip Cylon Eye v1.10 Created by EternalCore
#include <Adafruit_NeoPixel.h>

//Settings:
#define PIN 1 //The Pin out your Neopixel DIN strip/stick is connected to (Default is 6)
#define numPixelsInStrip 8 //The total amount of pixel's/led's in your connected strip/stick (Default is 60)

long previousMillis = 0;        // will store last time LED was updated
int neoPixelToChange = 0; //track which neoPixel to change
int neoPixel_j = 0; //stores values for program cycles
int fadeDirection = -1;//change sigen to fade up or down
boolean cylonDirection = true; //keeps track of the direction the pixels should swipe
boolean cylonPause = false; //keeps track of the pause inbetween swipes

Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixelsInStrip, PIN, NEO_GRB + NEO_KHZ800); //Standered Strip function

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
void loop() {
  cylonEye(40,500);
}

void cylonEye(uint8_t swipeSpeed, uint8_t pauseTime){
    
    //if needed, change directions
    if (neoPixelToChange > numPixelsInStrip) {
      cylonDirection = false;
    }
    if (neoPixelToChange < 0) {
      cylonDirection = true;
    }
    
    //run the swipe
    if(cylonDirection){
        cylonUp(strip.Color(175,0,0), strip.Color(25,0,0), swipeSpeed); // red
    } else {
        cylonDown(strip.Color(175,0,0), strip.Color(25,0,0), swipeSpeed); // red
    }
}

void cylonEye_notWorking(uint8_t swipeSpeed, uint8_t pauseTime){
  
  if (cylonPause){ //we are on a pause break from swipes
  
    unsigned long currentPauseMillis = millis();
    
    //check to see if we've waited long enough
    if(currentPauseMillis - previousMillis > pauseTime) {  /////////////////////////////// this is not getting called. Why????????????????
      // save the last time you checked the pause 
      previousMillis = currentPauseMillis;  
      cylonPause = false; //end the pause
    }
    
  } else {
    
    //if needed, change directions
    if (neoPixelToChange > numPixelsInStrip) {
      cylonDirection = false;
    }
    if (neoPixelToChange < 0) {
      cylonDirection = true;
      cylonPause = true; //take a break from the swipe
      //turn all pixels off
      for(uint16_t i=0; i<strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(0,0,0));
      }
      strip.show(); 
      previousMillis = millis();
    }
    
    //run the swipe
    if(cylonDirection){
        cylonUp(strip.Color(175,0,0), strip.Color(25,0,0), swipeSpeed); // red
    } else {
        cylonDown(strip.Color(175,0,0), strip.Color(25,0,0), swipeSpeed); // red
    }
  }
}

void cylonUp(uint32_t c0, uint32_t c1, uint8_t wait) {
  
  unsigned long currentMillis = millis();
    
    //neoPixelToChange * wait
  if(currentMillis - previousMillis > wait) {
    
    //turn all pixels off
    for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0,0,0));
    }
    
    // save the last time you changed a NeoPixel 
    previousMillis = currentMillis;  
  
    //change a pixel
    strip.setPixelColor(neoPixelToChange, c0); //primary color
    strip.setPixelColor(neoPixelToChange-1, c1); //secondary color
    strip.setPixelColor(neoPixelToChange+1, c1); //secondary color
    strip.show();
    neoPixelToChange++;
  }
}

void cylonDown(uint32_t c0, uint32_t c1, uint8_t wait) {
  
  unsigned long currentMillis = millis();
    
    //neoPixelToChange * wait
  if(currentMillis - previousMillis > wait) {
    
    //turn all pixels off
    for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0,0,0));
    }
    
    // save the last time you changed a NeoPixel 
    previousMillis = currentMillis;  
  
    //change a pixel
    strip.setPixelColor(neoPixelToChange, c0); //primary color
    strip.setPixelColor(neoPixelToChange-1, c1); //secondary color
    strip.setPixelColor(neoPixelToChange+1, c1); //secondary color
    strip.show();
    neoPixelToChange--; //is there any way to combine this with cylonUp, since this is the only line that is different?
  }
}

// Fill all the dots with one color
void allColor(uint32_t c) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
  }
} 


User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Help with Cylon Swipe

Post by adafruit_support_rick »

Why not just call delay(pauseTime)?

User avatar
drumminhands
 
Posts: 36
Joined: Tue Jul 23, 2013 10:02 am

Re: Help with Cylon Swipe

Post by drumminhands »

Because my next step is to take this into a sketch that has a button attached, changing the pattern displayed in the NeoPixels. That's working well, but requires me to stop using delay.

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Help with Cylon Swipe

Post by adafruit_support_rick »

The problem is that your delay is using previousMillis. Your cylonEye_notWorking is actually working, but it's corrupting previousMillis such that cylonUp and cylonDown are getting stuck.
I added a delayMillis, and it's scanning now:

Code: Select all

//NeoPixel LED Digital Strip Cylon Eye v1.10 Created by EternalCore
#include <Adafruit_NeoPixel.h>

//Settings:
#define PIN 1 //The Pin out your Neopixel DIN strip/stick is connected to (Default is 6)
#define numPixelsInStrip 8 //The total amount of pixel's/led's in your connected strip/stick (Default is 60)

long previousMillis = 0;        // will store last time LED was updated
long delayMillis = 0;
int neoPixelToChange = 0; //track which neoPixel to change
int neoPixel_j = 0; //stores values for program cycles
int fadeDirection = -1;//change sigen to fade up or down
boolean cylonDirection = true; //keeps track of the direction the pixels should swipe
boolean cylonPause = false; //keeps track of the pause inbetween swipes

Adafruit_NeoPixel strip = Adafruit_NeoPixel(numPixelsInStrip, PIN, NEO_GRB + NEO_KHZ800); //Standered Strip function

void setup() { 
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
void loop() {
  cylonEye_notWorking(40, 500);
}

void cylonEye(uint8_t swipeSpeed, uint8_t pauseTime) {

  //if needed, change directions
  if (neoPixelToChange > numPixelsInStrip) {
    cylonDirection = false;
  }
  if (neoPixelToChange < 0) {
    cylonDirection = true;
  }

  //run the swipe
  if (cylonDirection) {
    cylonUp(strip.Color(175, 0, 0), strip.Color(25, 0, 0), swipeSpeed); // red
  } else {
    cylonDown(strip.Color(175, 0, 0), strip.Color(25, 0, 0), swipeSpeed); // red
  }
}

void cylonEye_notWorking(uint8_t swipeSpeed, uint8_t pauseTime) {

  if (cylonPause) { //we are on a pause break from swipes

    unsigned long currentPauseMillis = millis();

    //check to see if we've waited long enough
    if ((currentPauseMillis - delayMillis) > pauseTime) { /////////////////////////////// this is not getting called. Why????????????????
      // save the last time you checked the pause
      delayMillis = currentPauseMillis;
      cylonPause = false; //end the pause
    }

  } else {

    //if needed, change directions
    if (neoPixelToChange > numPixelsInStrip) {
      cylonDirection = false;
    }
    if (neoPixelToChange < 0) {
      cylonDirection = true;
      cylonPause = true; //take a break from the swipe
      //turn all pixels off
      for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(0, 0, 0));
      }
      strip.show();
      delayMillis = millis();
    }

    //run the swipe
    if (cylonDirection) {
      cylonUp(strip.Color(175, 0, 0), strip.Color(25, 0, 0), swipeSpeed); // red
    } else {
      cylonDown(strip.Color(175, 0, 0), strip.Color(25, 0, 0), swipeSpeed); // red
    }
  }
}

void cylonUp(uint32_t c0, uint32_t c1, uint8_t wait) {

  unsigned long currentMillis = millis();

  //neoPixelToChange * wait
  if (currentMillis - previousMillis > wait) {

    //turn all pixels off
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0, 0, 0));
    }

    // save the last time you changed a NeoPixel
    previousMillis = currentMillis;

    //change a pixel
    strip.setPixelColor(neoPixelToChange, c0); //primary color
    strip.setPixelColor(neoPixelToChange - 1, c1); //secondary color
    strip.setPixelColor(neoPixelToChange + 1, c1); //secondary color
    strip.show();
    neoPixelToChange++;
  }
}

void cylonDown(uint32_t c0, uint32_t c1, uint8_t wait) {

  unsigned long currentMillis = millis();

  //neoPixelToChange * wait
  if (currentMillis - previousMillis > wait) {

    //turn all pixels off
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0, 0, 0));
    }

    // save the last time you changed a NeoPixel
    previousMillis = currentMillis;

    //change a pixel
    strip.setPixelColor(neoPixelToChange, c0); //primary color
    strip.setPixelColor(neoPixelToChange - 1, c1); //secondary color
    strip.setPixelColor(neoPixelToChange + 1, c1); //secondary color
    strip.show();
    neoPixelToChange--; //is there any way to combine this with cylonUp, since this is the only line that is different?
  }
}

// Fill all the dots with one color
void allColor(uint32_t c) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
  }
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”