Go Down

Topic: trouble with non-blocking function (Read 2654 times) previous topic - next topic

noiasca

#15
Apr 15, 2020, 11:59 pm Last Edit: Apr 16, 2020, 12:00 am by noiasca
don't be disappointed. You have jumped into OOP, you can pulse individual LEDs, you have made you first setter method - hey  that's more than others do in months!

Save the sketch, you will need it some day for shure. But now start a new project.
Without looking to much on other projects, my (stepwise) approach would be:

a) make a simple bouncing LED (e.g. 1 out of 6)
b) I assume a full range PWM is not necessary, just declare 2 or 3 "trailing" LEDS with 80% 60% 40%  PWM and just let them follow the 100% LED. I'm very confident that this is all you will need and can be done with some for loops. Be carefull at the beginning and the end of your boarders - but that should be more or less all i guess.

You see? Just turn you head 180 degrees and try a different approach:
don't "dim" one LED. Take over the control/the bouncing, switch it on for 100% and handle the -1, -2, +1, +2 LED with reduced PWM.

just google ... oh, this one:

https://learn.adafruit.com/larson-scanner-shades/arduino-code

yes I know it's for Neopixel, but that's more or less the same procedure I would do:
set the boundaries, bounce a "master" LED - give an reduced effect onto the trailing/heading LEDs.


how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

rcayot

Okay, thought I'd give an update.  Partial success!  And thank you noiasca!

So I have been able to get my eight LEDs to pulse, and sequence them such that they successively pulse, and the 'next' one starts before teh first one is completed.

here is the code.

Code: [Select]
/*
  pulse LED with PWM

  https://forum.arduino.cc/index.php?topic=675980
  by noiasca
*/
uint32_t CMillis = 0;
class PulseLed
{
  private :
    const byte ledPin;                       // has to be a GPIO with PWM
    uint8_t interval = 2;                   // the intervall in milliseconds for each change
    uint32_t previousMillis = 0;             // stores the last change
    uint8_t pwm = 0;                         // actual value of PWM
    uint8_t start = 0;                       // range from (lowest PWM value)
    uint8_t end =  255;                      // range to (highest PWM value
    uint8_t pulsecount = 0;
    int y =0;
    bool dir = true;
  public :
    PulseLed ( byte ledPin) :
      ledPin(ledPin)
    {}

    void begin()
    {
      pinMode(ledPin, OUTPUT);
    }
    void setInterval(byte newInterval)
    {
      interval = newInterval;
    }

    void setRange( byte newStart, byte newEnd)
    {
      if (newEnd > newStart)
      {
        start = newStart;
        end = newEnd;
      }
    }
    void setpulsecount(byte newpulsecount)
    {
      pulsecount = newpulsecount;
    }
   
  void tick()
{
  if ((millis() - previousMillis > interval) && (pulsecount != 0))
  {
  previousMillis = millis();
  if  (y >= start && dir == true)
    {
    if ((y <= end -1) && (dir == true)) y = y + 1; 
    else dir = false;
    }
    else
    {
    if ((y > start) && (dir == false)) y = y - 1;
    else dir = true;
    }
    analogWrite(ledPin, y);
    if (y == start)
    {
    pulsecount = (pulsecount - 1);
    }
  } 
}
 
};

PulseLed pulseLed[] {2, 3, 4, 5, 6, 7};

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < 6; i ++)
  { 
  pulseLed[i].setRange(0, 255);   // set an individuall PWM range from ... to ...
  pulseLed[i].setInterval(1);    // set the interval/speed of pulsing
  pulseLed[i].setpulsecount(1);  // set number of times to pulse
  }
CMillis = millis();
}

void loop()
{
pulseLed[0].tick(); 
if (millis() - CMillis <= 500) {return;}
pulseLed[1].tick(); 
if (millis() - CMillis <= 1000) {return;}
pulseLed[2].tick(); 
if (millis() - CMillis <= 1500) {return;}
pulseLed[3].tick(); 
if (millis() - CMillis <= 2000) {return;}
pulseLed[4].tick(); 
if (millis() - CMillis <= 2500) {return;}
pulseLed[5].tick(); 
}


Now that I have gotten this far, I am goint to try to reverse them and perhaps other stuff.  I have really learned a lot going through this exercise.


My goal is to be able to design wearables for my many nieces and grand nieces and friends with young ones that like 'bling'.  Part of this of course is the ability to control the flashing and pulsing of various LEDs.

Again, thanks to all who have looked in on this project and contributed.

Roger Ayotte

PerryBebbington

Thank you for the update Roger, much appreciated.

rcayot

Thank you noiasca, and Perry.

I guess I will try something fromt eh example you gave noiasca, but I have run into a programming issue I cannot figure out.

First here is the whole code.
Code: [Select]
/*
  pulse LED with PWM

  https://forum.arduino.cc/index.php?topic=675980
  by noiasca
*/
bool up = true;
int sequence[] = {0, 1, 2, 3, 4, 5};
int i;
int y = 0;
uint32_t CMillis = 0;
class PulseLed
{
  private :
    const byte ledPin;                       // has to be a GPIO with PWM
    uint8_t interval = 2;                   // the intervall in milliseconds for each change
    uint32_t previousMillis = 0;             // stores the last change
    uint8_t pwm = 0;                         // actual value of PWM
    uint8_t start = 0;                       // range from (lowest PWM value)
    uint8_t end =  255;                      // range to (highest PWM value
    uint8_t pulsecount = 0;
       int y =0;
    bool dir = true;
  public :
    PulseLed ( byte ledPin) :
      ledPin(ledPin)
    {}

    void begin()
    {
      pinMode(ledPin, OUTPUT);
    }
    void setInterval(byte newInterval)
    {
      interval = newInterval;
    }

    void setRange( byte newStart, byte newEnd)
    {
      if (newEnd > newStart)
      {
        start = newStart;
        end = newEnd;
      }
    }
    void setpulsecount(byte newpulsecount)
    {
      pulsecount = newpulsecount;
    }
   
  void tick()
{
  if ((millis() - previousMillis > interval) && (pulsecount != 0))
  {
  previousMillis = millis();
  if  (y >= start && dir == true)
    {
    if ((y <= end -1) && (dir == true)) y = y + 85; 
    else dir = false;
    }
    else
    {
    if ((y > start) && (dir == false)) y = y - 1;
    else dir = true;
    }
    analogWrite(ledPin, y);
    if (y <= start)
    {
    pulsecount = (pulsecount - 1);
    }
  } 
}
 
};

PulseLed pulseLed[] {2, 3, 4, 5, 6, 7};

void setup()
{
  Serial.begin(9600);
  for (int i = 0; i < 6; i ++)
  { 
  pulseLed[i].setRange(0, 255);   // set an individuall PWM range from ... to ...
  pulseLed[i].setInterval(1);    // set the interval/speed of pulsing
  pulseLed[i].setpulsecount(1);  // set number of times to pulse
  }
CMillis = millis();
}

void loop()
{
if (up == true) {int sequence[] = {5, 4, 3, 2, 1, 0};} this does not change the element sequence
// int sequence[] = {5, 4, 3, 2, 1, 0}; if uncommented, this will run in the 'reverse' sequence
// Serial.println(sequence[0]);
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);
}   
// int sequence[] = {0, 1, 2, 3, 4, 5};
//up = false;

CMillis = millis();
}


I solved the one issue with a timer and the use of an if statement with a {return} at the end.  It works, I have NO idea why.

Second

I use an array from 0-5 to select the ledpin for the tick() call.  What I was attempting to do was reverse the sequence of ledpin from 0->5 to 5->0.  I initialize the array as sequence{0,1,2,3,4,5} and if it runs it will run with that order.  If I reassign the array to sequence{5,4,3,2,1,0} it will run in reverse. 

However!  I do not seem to be able to re-assign the elements of sequence{} ising an if statement block.  The if up==true statement will return true if tested, but the re-assignment does not happen.

I have been puzzled by theis.  Is re-assigning an array in an if statement not allowed? 
any insight will be appreciated.  I am convinced that if I can solve these issues I will learn something.

I will also start over using your suggested technique noiasca, and let you know how it works!

Code: [Select]
void loop()
{
if (up == true) {int sequence[] = {5, 4, 3, 2, 1, 0};} this does not change the element sequence
// int sequence[] = {5, 4, 3, 2, 1, 0}; if uncommented, this will run in the 'reverse' sequence
// Serial.println(sequence[0]);
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);
}   
// int sequence[] = {0, 1, 2, 3, 4, 5};
//up = false;

CMillis = millis();
}

noiasca

#19
Apr 24, 2020, 06:50 pm Last Edit: Apr 24, 2020, 09:05 pm by noiasca
long story short: you can't "reassign" an array in that way.

you "could" do

if you have this as global

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

assign each value:

sequence[0] = 5;
sequence[1] = 4;
sequence[2] = 3;
sequence[3] = 2;
sequence[4] = 1;
sequence[5] = 0;

but hey, that's what for loops are made for.


edit1:
the following example picks up the idea of trailing LEDs and heading LEDs with different PWM.


Code: [Select]

// Larson scanner with PWM
// https://forum.arduino.cc/index.php?topic=675980

#define DEBUG_UART 1

const uint8_t ledPin[] = {3, 5, 6, 9, 10, 11};  // UNO PWM pins 3, 5, 6, 9, 10, 11
const uint8_t totalNoLeds = sizeof(ledPin);

void runPattern()
{
  static uint8_t  actual = totalNoLeds;
  static uint32_t previousMillis = 0;
  const uint16_t myIntervall = 200;
  static int8_t dir = 1;
  if (millis() - previousMillis >= myIntervall)
  {
    byte newPwm[totalNoLeds];
    memset(newPwm, 0, totalNoLeds);
    if (dir == 1)
    {
      if (actual >= totalNoLeds - 1)
        dir = -1;
    }
    else if (actual <= 0)
      dir = 1;
    actual += dir;
#if DEBUG_UART
    Serial.println();
    Serial.print(actual);
    Serial.print("\t");
#endif
    //if (actual >= 2) newPwm[actual -2] = 16;                // two LEDs before
    //if (actual + 2 < totalNoLeds) newPwm[actual + 2] = 16;  // two LEDs after current LED
    if (actual >= 1) newPwm[actual - 1] = 31;                 // one LED before current LED
    if (actual + 1 < totalNoLeds) newPwm[actual + 1] = 31;    // one LED after current LED
    newPwm[actual] = 255;
    for (byte i = 0; i < totalNoLeds; i++)
    {
      analogWrite(ledPin[i], newPwm[i]);
#if DEBUG_UART
    Serial.print(newPwm[i]);
    Serial.print("\t");
#endif
    }
    previousMillis = millis();
  }
}

void setup() {
#if DEBUG_UART
  Serial.begin(115200);
#endif
  for (uint8_t i = 0; i < totalNoLeds; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  runPattern();
  // do what ever you want unblocked here
}



now you could combine this with pulsing up the main LED (actual) and let the others follow the main trailing LED/heading LED follow it.

edit2:
after watching lit. hundreds of KITT Videos on youtube, my assumptions are:
the scanner should have 8 lamps
around 4 appear to be on.
when imitating the effect with LEDs it might be better if the "first" LED isn't lighted ab 100%
so this is my final try
Code: [Select]

// Larson scanner with PWM
// https://forum.arduino.cc/index.php?topic=675980
// https://www.youtube.com/watch?v=sCTPbokUKBQ

#define DEBUG_UART 1

const uint8_t ledPin[] = {3, 5, 6, 9, 10, 11};  // UNO PWM pins 3, 5, 6, 9, 10, 11
const uint8_t totalNoLeds = sizeof(ledPin);

void runPattern()
{
  static uint8_t  actual = totalNoLeds;
  static uint32_t previousMillis = 0;
  const uint16_t myIntervall = 200;
  static int8_t dir = 1;
  if (millis() - previousMillis >= myIntervall)
  {
    byte newPwm[totalNoLeds];
    memset(newPwm, 0, totalNoLeds);
    if (dir == 1)
    {
      if (actual >= totalNoLeds - 1)
        dir = -1;
    }
    else if (actual <= 0)
      dir = 1;
    actual += dir;
#if DEBUG_UART
    Serial.println();
    Serial.print(actual);
    Serial.print("\t");
#endif
    //if (actual >= 2) newPwm[actual -2] = 16;                  // V1 two LEDs before
    //if (actual + 2 < totalNoLeds) newPwm[actual + 2] = 16;    // V1two LEDs after current LED
    //if (actual >= 1) newPwm[actual - 1] = 31;                 // V1 one LED before current LED
    //if (actual + 1 < totalNoLeds) newPwm[actual + 1] = 31;    // V2 one LED after current LED

    if (actual - 3 * dir < totalNoLeds ) newPwm[actual - 3 * dir] = 63;
    if (actual - 2 * dir < totalNoLeds ) newPwm[actual - 2 * dir] = 127;
    if (actual - 1 * dir < totalNoLeds ) newPwm[actual - 1 * dir] = 255;
    newPwm[actual] = 200;
    for (byte i = 0; i < totalNoLeds; i++)
    {
      analogWrite(ledPin[i], newPwm[i]);
#if DEBUG_UART
      Serial.print(newPwm[i]);
      Serial.print("\t");
#endif
    }
    previousMillis = millis();
  }
}

void setup() {
#if DEBUG_UART
  Serial.begin(115200);
#endif
  for (uint8_t i = 0; i < totalNoLeds; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  runPattern();
  // do what ever you want unblocked here
}
how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

rcayot

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


I went with the individual assignments.

Code: [Select]
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

noiasca

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...
how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

rcayot


PerryBebbington

#23
Apr 25, 2020, 10:41 am Last Edit: Apr 28, 2020, 03:58 pm by PerryBebbington
Quote
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:
Quote
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:
Code: [Select]
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.




rcayot

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!

PerryBebbington


noiasca

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
https://www.arduino.cc/en/Tutorial/Debounce

If this is done, we should talk about your code duplicates ...
Code: [Select]

    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
how to react on postings:
- post helped: provide your final sketch, say thank you & give karma.
- post not understood: Ask as long as you understand the post
- post is off topic (or you think it is): Stay to your topic. Ask again.
- else: Ask again.

rcayot

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

rcayot

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:

Code: [Select]
// 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.


Code: [Select]

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

Go Up