trouble with non-blocking function

Sorry to keep on it, but SOMETHING is going on I do not understand.

I went with the individual assignments.

void loop()

{
if (up == true)
{
sequence[0] = 5;
sequence[1] = 4;
sequence[2] = 3;
sequence[3] = 2;
sequence[4] = 1;
sequence[5] = 0;
// Serial.println(up);
}
else
{
sequence[0] = 0;
sequence[1] = 1;
sequence[2] = 2;
sequence[3] = 3;
sequence[4] = 4;
sequence[5] = 5;
up = !up;
}

pulseLed[sequence[0]].tick();  
if (millis() - CMillis <= 50) {return;}
pulseLed[sequence[1]].tick();  
if (millis() - CMillis <= 100) {return;}
pulseLed[sequence[2]].tick();  
if (millis() - CMillis <= 150) {return;}
pulseLed[sequence[3]].tick();  
if (millis() - CMillis <= 200) {return;}
pulseLed[sequence[4]].tick();  
if (millis() - CMillis <= 250) {return;}
pulseLed[sequence[5]].tick();
if (millis() - CMillis <= 775) {return;}    

for (int i = 0; i < 6; i ++)  
{
pulseLed[i].setpulsecount(1);
}    
// Serial.println(up);

// Serial.println(up);
CMillis = millis();
up = !up;
}

Unfortunately, something is not right. As is, the variable 'up' either always evaluyates as 'true' or it oscillates between 1, and 0, but still the sequence is as if up = true! even if I do a Serial.println(up); and shows the change betweenm 1, and 0, for some reason, the cycle begins only when up = true.

there is some logic going on I do not get.

Roger

Roger

I have to improve my English skills for sure, but I guess your are flogging a dead horse.

Nevertheless, if you want to get your code checked by others, post a full, compileable sketch...

okay, okay!

Thanks!

Unfortunately, something is not right. As is, the variable 'up' either always evaluates as 'true' or it oscillates between 1, and 0, but still the sequence is as if up = true! even if I do a Serial.println(up); and shows the change between 1, and 0, for some reason, the cycle begins only when up = true.

If I've read that correctly then follow your code:

if (up == true)

So it does the bit in the if(), when it gets to the end it does the pulseLED stuff and at the very end up becomes not true.
Next time up is not true so it does the else sequence, at which point up is made true, followed by the pulseLED stuff followed by up being made not true.

I don't believe that's what you want to happen.

Do you know you can have a multidimensional array?
So:

sequence[2][6] = {{0, 1, 2, 3, 5, 5}, {5, 4, 3, 2, 1, 0}}

You just then have to change the value of the first index between 0 and 1 to select which sequence you get.

I would like to thank noiasca and Perry for their help. I while I was never able to get the class based sketch noiasca sent to work teh way I wanted, he sent me another example to try. I workedd that sketch, and modfied it and have attached to sketch.

This program uses 12 LEDs and lights them with PWM in a 'larson' like scan. I added three more patterns to the code, which can be selected using a button, which runs through a different pattern when repeatedly pushed.

My favorite pattern is the one I call wave.

Thanks again!

multi_larson_button4.ino (12.9 KB)

I'm glad you got it working :slight_smile:

nice to get feedback, appreciate!

Any chance that you work on more improvements? There is absolutely no need to read a button with an interrupt. To be honest imho this is a bad practice. You have invested a lot of time in this sketch and now you are spoiling it with an interrupt routine.

Read how to debounce a button and get rid of your interrupt

If this is done, we should talk about your code duplicates ...

    if (actual >= 12) newPwm[actual -12] =   pos[0];             // 255;   // twelve LEDs before
    if (actual + 12 < totalNoLeds) newPwm[actual + 12] =  pos[0];   // twelve LEDs after current LED
    if (actual >= 11) newPwm[actual -11] =                pos[0];   // eleven LEDs before
    if (actual + 11 < totalNoLeds) newPwm[actual + 11] =  pos[0];   // eleven LEDs after current LED
    if (actual >= 10) newPwm[actual -10] =                pos[0];   // ten LEDs before
    if (actual + 10 < totalNoLeds) newPwm[actual + 10] =  pos[0];   // ten LEDs after current LED
    if (actual >= 9) newPwm[actual -9] =                pos[0];   // nine LEDs before
    if (actual + 9 < totalNoLeds) newPwm[actual + 9] =  pos[0];   // nine LEDs after current LED
    if (actual >= 8) newPwm[actual -8] =                pos[0];   // eight LEDs before
    if (actual + 8 < totalNoLeds) newPwm[actual + 8] =  pos[0];   // eight LEDs after current LED
    if (actual >= 7) newPwm[actual -7] =                pos[0];   // seven LEDs before
    if (actual + 7 < totalNoLeds) newPwm[actual + 7] =  pos[0];   // seven LEDs after current LED
    if (actual >= 6) newPwm[actual -6] =                pos[0];   // six LEDs before
    if (actual + 6 < totalNoLeds) newPwm[actual + 6] =  pos[0];   // six LEDs after current LED
    if (actual >= 5) newPwm[actual -5] =                pos[1];   // five LEDs before
    if (actual + 5 < totalNoLeds) newPwm[actual + 5] =  pos[1];   // five LEDs after current LED
    if (actual >= 4) newPwm[actual -4] =                pos[2];   // four LEDs before
    if (actual + 4 < totalNoLeds) newPwm[actual + 4] =  pos[2];   // four LEDs after current LED
    if (actual >= 3) newPwm[actual -3] =                pos[3];   // three LEDs before
    if (actual + 3 < totalNoLeds) newPwm[actual + 3] =  pos[3];   // three LEDs after current LED
    if (actual >= 2) newPwm[actual -2] =                pos[4];   // two LEDs before
    if (actual + 2 < totalNoLeds) newPwm[actual + 2] =  pos[4];   // two LEDs after current LED
    if (actual >= 1) newPwm[actual - 1] =               pos[5];   // one LED before current LED
    if (actual + 1 < totalNoLeds) newPwm[actual + 1] =  pos[5];   // one LED after current LED

after a two weeks crash course, I'm very confident you can do that better ... #believeInYou

Thanks noiasca. I know the code is bloated, I am thinking about how I can make a general case and iterate through.

I have used buttons and debounced before, but this was very quick and easy. Just wanted to be sure I could make it work.

I will try the alternate button approach an then work on condensing the code. It would be good to get a few generic cases which could be modified using lists/arrays.

Roger

thanks for the encouragement!

I was able to condense my sketch to something much smaller, I used a 2 dimensional array to hold the pattern for three of the patterns. The fourth, the one I call 'twopattern' is really giving me fits. this pattern shows two 'bright' leds starting from each end of the LED series of 12. The bright LEDs appear to migrate towards the opposite ends, and then pass through or bounce off (depending on how your eyes see it) and each bright LED is trailed by two dimmer LEDS. Writing the original code was not hard, I was even able to replace 11 lines of code with ONE! however, that was only for the 'right' side set of six LEDs, the left side has been very troublesome. I cannot seem to find the proper loop code to replace the eleven lines (that work) for the left set.

Any ideas and I could REALLY appreciate it. I have tried many things, unfortunately they have not worked. The commented out line:

// if (actualleft >= (i+5)) newleftPwm[actualleft - i*ldir] =   two[5-i];

Does cycle the left bright LED back and forth, but I cannot for the life of me get the trailing LEDs to work.

// Larson scanner with PWM

// Modified by Roger Ayotte
// code uses 12 LEDs and 4 different patterns of light
// modeled after a 'larson' type of pattern.
// https://forum.arduino.cc/index.php?topic=675980

#define DEBUG_UART 1
const uint8_t ledPin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};  // PWM pins 2 through 13
const uint8_t totalNoLeds = sizeof(ledPin);
const int buttonpin0 = 0;     // the number of the pushbutton pin
int button0pushed = 0;       // variable for reading the pushbutton status
int pat = 0;
int i;
int j;
// patternin[0] is positive pattern, [1] is negative, and [2] is wave
int patternij[3][12] = {
{255, 75, 25, 10, 5, 0, 0, 0, 0, 0, 0, 0}, 
{0, 5, 25, 75, 255, 255, 255, 255, 255, 255, 255, 255},
{0, 5, 25, 255, 25, 5, 0, 5, 25, 255, 25, 5},
};
 
void pattern() 
{
if (button0pushed == 0) j=0;
    else if (button0pushed == 1) j=1;
    else if (button0pushed == 2) return;
    else if (button0pushed == 3) return;
    
  static uint8_t  actual = totalNoLeds;
  static uint32_t previousMillis = 0;
  const uint16_t myIntervall = 100;
  static int8_t dir = 1;
  
  if (millis() - previousMillis >= myIntervall)
  {
    byte newPwm[] = {0,0,0,0,0,0,0,0,0,0,0,0};
    if (dir == 1)
    {
      if (actual >= totalNoLeds - 1) dir = -1;   
    }
    else if (actual <= 0) dir = 1;
    actual += dir;
    
    if (actual <= totalNoLeds - 1) 

for (int i = 0; i< totalNoLeds - 1; i = i+1)
{    
if (actual >= i) newPwm[actual -i] =   patternij[j][i];             
if (actual + i < totalNoLeds) newPwm[actual + i] =  patternij[j][i];
}   

    for (byte i = 0; i < totalNoLeds; i++)
    {
     analogWrite(ledPin[i], newPwm[i]);
    }

previousMillis = millis();
  }
}


void wavepattern()
{
  static uint8_t  actual = totalNoLeds;
  static uint32_t previousMillis = 0;
  const uint16_t myIntervall = 50;
  static int8_t dir = 1;

  if (millis() - previousMillis >= myIntervall)
  {
    byte newPwm[] = {0,0,0,0,0,0,0,0,0,0,0,0};
       
    if (actual <= totalNoLeds - 1) actual = actual +1;
    
    else if (actual >= (totalNoLeds-1)) actual = 0;

    if (actual <= totalNoLeds - 1) 
   
int i;
for (int i = 0; i< totalNoLeds - 1; i = i+1)
{    
if (actual >= i) newPwm[actual -i] =   patternij[2][i];             
if (actual + i < totalNoLeds) newPwm[actual + i] =  patternij[2][i];
}
   
#if DEBUG_UART
// Serial.println(button0pushed);
// delay(100);    
#endif  
  

for (byte i = 0; i < totalNoLeds; i++)
    {
     analogWrite(ledPin[i], newPwm[i]);
    }

// delay(100);
previousMillis = millis();
  }
}

void twopattern()
{
  static uint8_t  actualright = (totalNoLeds/2);
  static uint8_t  actualleft = (totalNoLeds/2);
  static uint32_t previousMillis = 0;
  const uint16_t myIntervall = 100;
  static int8_t rdir = 1;
  static int8_t ldir = 1;
  if (millis() - previousMillis >= myIntervall)
  {
    byte newrightPwm[(totalNoLeds/2)];
    memset(newrightPwm, 0, (totalNoLeds/2));
    if (rdir == 1)
    {
      if (actualright >= (totalNoLeds/2) - 1)
        rdir = -1;
    }
    else if (actualright <= 0)
      rdir = 1;
    actualright += rdir;

  byte newleftPwm[(totalNoLeds/2)];
  memset(newleftPwm, 6, (totalNoLeds));
    
    if (ldir == 1)
    {
      if (actualleft >= (totalNoLeds) - 1)
        ldir = -1;
    }
    else if ((actualleft) <= 6)
      ldir = 1;
    actualleft += ldir;  

int two[] = {0, 0, 0, 5, 25, 255};
int i;
for (int i = 0; i<= (totalNoLeds/2 - 1); i = i+1)    
if (actualright >= i) newrightPwm[actualright -i*rdir] =   two[5-i];  

// if (actualleft >= (i+5)) newleftPwm[actualleft - i*ldir] =   two[5-i];  

    if (actualleft >= 5) newleftPwm[actualleft -5] =               two[0];       
    if (actualleft + 5 < totalNoLeds) newleftPwm[actualleft + 5] = two[0];   
    if (actualleft >= 4) newleftPwm[actualleft -4] =               two[1];   
    if (actualleft + 4 < totalNoLeds) newleftPwm[actualleft + 4] = two[0];   
    if (actualleft >= 3) newleftPwm[actualleft -3] =               two[2];   
    if (actualleft + 3 < totalNoLeds) newleftPwm[actualleft + 3*ldir] = two[0];   
    if (actualleft >= 2) newleftPwm[actualleft - 2*ldir]         = two[3];       
    if (actualleft + 2 < totalNoLeds) newleftPwm[actualleft + 2*ldir] = two[0];
    if (actualleft >= 1) newleftPwm[actualleft + ldir]           = two[4];   
    if (actualleft + 1 < totalNoLeds) newleftPwm[actualleft + ldir] = two[0];
    if (actualleft + 0 < totalNoLeds) newleftPwm[actualleft]     = two[5];  
    
        
    for (int i = 0, j = 11;  i < (totalNoLeds/2); i++, j--)  
    {
    analogWrite(ledPin[j], newleftPwm[j]);
    analogWrite(ledPin[i], newrightPwm[i]);
    }
    
#if DEBUG_UART
// Serial.println(actualleft);
// Serial.println(actualright);
// Serial.println(ledPin[i]);
#endif

previousMillis = millis();
   }
}

 
void setup() 
{
#if DEBUG_UART
Serial.begin(115200);
#endif

  for (uint8_t i = 0; i < totalNoLeds; i++) 
  {
    pinMode(i, OUTPUT);
  } 
// initialize the pushbutton pin as an input:
pinMode(buttonpin0, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonpin0), PATISR, FALLING);  

}

void loop()
{
// Serial.println(button0pushed);
if (button0pushed == 2) wavepattern();
if (button0pushed == 3) twopattern();
else pattern();
}

void PATISR()
{
button0pushed = button0pushed +1;
if (button0pushed == 4  ) button0pushed = 0;
}