Help with sequencing arrays to control analogWrite output.

Hello,

Instead of using arrays to control different outputs to LEDS (which is what most tutorials online use array integers for).. How do i assign an array to sequence the brightness of an LED with the analogWrite PWM output.

There are 3 different arrays so far, each of them are [256] integers long (the full 8bit output limit in varying the PWM).
Each array is a different waveform, so far:

int sine_wave [256];
int triange_wave [256];
int square_wave [256];

This is just an example so I haven't added the array contents but they are just a series of numbers from 0 to 255, starting at 128 as a wavetable look up table which sequences those particular waveshapes in the correct way.

So i plan to assign those arrays to an analogWrite pin with a potentiometer controlling the rate with millis.

Im super new to Arduino and coding in general, but this is the most basic form of what I'm using Arduino for, so i was hoping i could get an idea on how to do this.

I'm sorry if i haven't provided enough information!

How do i assign an array to sequence the brightness of an LED with the analogWrite PWM output.

This question doesn't make sense. The PWM output is a discrete value at any given moment. If you want to change that value as a function of time, you need two arrays - one for the duration that any given PWM value is to be applied, and one for the values to apply.

consider the following.

Not sure how you plan on using a single pot. Assume you want to use the pot to change the rate a waveform runs at

// -----------------------------------------------------------------------------
byte sine [] = {
     128, 131, 134, 137, 140, 143, 146, 149,
     152, 156, 159, 162, 165, 168, 171, 174,
     176, 179, 182, 185, 188, 191, 193, 196,
     199, 201, 204, 206, 209, 211, 213, 216,
     218, 220, 222, 224, 226, 228, 230, 232,
     234, 236, 237, 239, 240, 242, 243, 245,
     246, 247, 248, 249, 250, 251, 252, 252,
     253, 254, 254, 255, 255, 255, 255, 255,
     255, 255, 255, 255, 255, 255, 254, 254,
     253, 252, 252, 251, 250, 249, 248, 247,
     246, 245, 243, 242, 240, 239, 237, 236,
     234, 232, 230, 228, 226, 224, 222, 220,
     218, 216, 213, 211, 209, 206, 204, 201,
     199, 196, 193, 191, 188, 185, 182, 179,
     176, 174, 171, 168, 165, 162, 159, 156,
     152, 149, 146, 143, 140, 137, 134, 131,
     128, 124, 121, 118, 115, 112, 109, 106,
     103,  99,  96,  93,  90,  87,  84,  81,
      79,  76,  73,  70,  67,  64,  62,  59,
      56,  54,  51,  49,  46,  44,  42,  39,
      37,  35,  33,  31,  29,  27,  25,  23,
      21,  19,  18,  16,  15,  13,  12,  10,
       9,   8,   7,   6,   5,   4,   3,   3,
       2,   1,   1,   0,   0,   0,   0,   0,
       0,   0,   0,   0,   0,   0,   1,   1,
       2,   3,   3,   4,   5,   6,   7,   8,
       9,  10,  12,  13,  15,  16,  18,  19,
      21,  23,  25,  27,  29,  31,  33,  35,
      37,  39,  42,  44,  46,  49,  51,  54,
      56,  59,  62,  64,  67,  70,  73,  76,
      79,  81,  84,  87,  90,  93,  96,  99,
     103, 106, 109, 112, 115, 118, 121, 124,
};

byte tri [] = {
       0,   2,   4,   6,   8,  10,  12,  14,
      16,  18,  20,  22,  24,  26,  28,  30,
      32,  34,  36,  38,  40,  42,  44,  46,
      48,  50,  52,  54,  56,  58,  60,  62,
      64,  66,  68,  70,  72,  74,  76,  78,
      80,  82,  84,  86,  88,  90,  92,  94,
      96,  98, 100, 102, 104, 106, 108, 110,
     112, 114, 116, 118, 120, 122, 124, 126,
     128, 130, 132, 134, 136, 138, 140, 142,
     144, 146, 148, 150, 152, 154, 156, 158,
     160, 162, 164, 166, 168, 170, 172, 174,
     176, 178, 180, 182, 184, 186, 188, 190,
     192, 194, 196, 198, 200, 202, 204, 206,
     208, 210, 212, 214, 216, 218, 220, 222,
     224, 226, 228, 230, 232, 234, 236, 238,
     240, 242, 244, 246, 248, 250, 252, 254,
     254, 252, 250, 248, 246, 244, 242, 240,
     238, 236, 234, 232, 230, 228, 226, 224,
     222, 220, 218, 216, 214, 212, 210, 208,
     206, 204, 202, 200, 198, 196, 194, 192,
     190, 188, 186, 184, 182, 180, 178, 176,
     174, 172, 170, 168, 166, 164, 162, 160,
     158, 156, 154, 152, 150, 148, 146, 144,
     142, 140, 138, 136, 134, 132, 130, 128,
     126, 124, 122, 120, 118, 116, 114, 112,
     110, 108, 106, 104, 102, 100,  98,  96,
      94,  92,  90,  88,  86,  84,  82,  80,
      78,  76,  74,  72,  70,  68,  66,  64,
      62,  60,  58,  56,  54,  52,  50,  48,
      46,  44,  42,  40,  38,  36,  34,  32,
      30,  28,  26,  24,  22,  20,  18,  16,
      14,  12,  10,   8,   6,   4,   2,   0,
};

byte sq [] = {
       0, 255
};

// -----------------------------------------------------------------------------
enum { LED10 = 10, LED11, LED12, LED13 };

byte buts [] = { A1, A2, A3 };
byte leds [] = { LED10, LED11, LED12, LED13 };

struct LedWave_s {
    byte            led;
    byte           *wave;
    int             size;
    unsigned long   msecDwell;

    unsigned long   msecLst;
    int             idx;
};

LedWave_s ledWave [] = {
    { LED10, sine, sizeof(sine),  10 },
    { LED11, tri,  sizeof(tri),   20 },
    { LED12, sq,   sizeof(sq),   500 },
};

#define N_LEDWAVE   (sizeof(ledWave)/sizeof(LedWave_s))

unsigned long  msec;

// ---------------------------------------------------------
void
wave (
    LedWave_s *w)
{
    if (msec - w->msecLst > w->msecDwell)  {
        w->msecLst = msec;
        analogWrite (w->led, w->wave [w->idx]);
        w->idx =  w->idx < (w->size - 1) ? w->idx + 1 : 0;

#if 0
        char s [80];
        sprintf (s, "wave: pin %d, idx %d, val %3d",
            w->led, w->idx, w->wave [w->idx]);
        Serial.println (s);
#endif
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    msec = millis ();

    LedWave_s  *w = ledWave;
    for (unsigned n = 0; n < N_LEDWAVE; n++, w++)
        wave (w);

}

// ---------------------------------------------------------
void
setup (void)
{
    Serial.begin (115200);

    for (unsigned i =0; i < sizeof(buts); i++)
        pinMode (buts [i], INPUT_PULLUP);

    for (unsigned i =0; i < sizeof(leds); i++)  {
        pinMode (leds [i], OUTPUT);
        digitalWrite (leds [i], HIGH);
    }
}

I had something "simpler" in my old programing folder that you might want to look at as well. The period is hardcoded in the const uint32_t period; constant but you could make that non const and connect its value to a mapping from a potentiometer, and dynamically calculate the stepDuration which I calculate only once at the moment.

const uint8_t waveTable[] = {
  128,  135,  143,  151,  159,  167,  174,  182,  189,  196,  202,  208,  214,
  220,  225,  230,  235,  239,  242,  246,  248,  251,  252,  253,  254,  255,
  254,  253,  252,  251,  248,  246,  242,  239,  235,  230,  225,  220,  214,
  208,  202,  196,  189,  182,  174,  167,  159,  151,  143,  135,  128,  120,
  112,  104,  96,  88,  81,  73,  66,  59,  53,  47,  41,  35,  30,  25,  20,
  16,  13,  9,  7,  4,  3,  2,  1,  0,  1,  2,  3,  4,  7,  9,  13,  16,  20,
  25,  30,  35,  41,  47,  53,  59,  66,  73,  81,  88,  96,  104,  112,  120
}; // this is a sinusoid centered around 128, with 100 samples

const uint8_t nbSteps = sizeof(waveTable) / sizeof(waveTable[0]);
const uint8_t ledPWMPin = 6; // Pick a PWM pin

const uint32_t period = 1000; // 1s
const uint32_t stepDuration = period / nbSteps;

void checkLed(bool forceStart = false)
{
  static uint32_t lastChange;
  static uint8_t stepIndex = 0;
  if (forceStart) {
    stepIndex = 0;
    lastChange = millis() - 2 * stepDuration; // to force a display
  }

  if (millis() - lastChange >= stepDuration) {
    analogWrite(ledPWMPin, waveTable[stepIndex++]);
    if (stepIndex >= nbSteps) stepIndex = 0;
    lastChange = millis();
  }
}

void setup()
{
  pinMode(ledPWMPin, OUTPUT);
  checkLed(true); // get going
}

void loop()
{
  checkLed(); // the led will pulse along a sinus wave
  // you can do other stuff here as long as it's not too long

}

thanks for the input guys, i found this project which is almost exactly the starting point of what im trying to achieve, which shouldnt be hard to convert to a nano.

se7en_costanza:
thanks for the input guys, i found this project which is almost exactly the starting point of what im trying to achieve, which shouldnt be hard to convert to a nano.

https://www.arduino.cc/en/Tutorial/DueSimpleWaveformGenerator

I'm curious to see how you will handle the DAC part of this tutorial on a nano :slight_smile:

change them to output to PWM pins, remove the 12 bit resolution and stick to 0-1023, scale the samples down to 256 or lower, there was a few lines of code i had to remove but its working great.

Sure - that’s the code a few of us have posted above

But You definitely don’t get a voltage waveform like triangle or square - you still get 0 or 5V at a certain pulse width if you look at a your wave on a scope. That's enough to drive the perception of a voltage change on a LED but not a Wave form Generator