I am designing some code to light up some LED’s with an ATTINY, and because of limited RAM, I am using the FLASH to store the patterns. I am currently testing this on a regular Arduino UNO:
#include <MsTimer2.h>
#include <avr/pgmspace.h>
uint8_t const sineWave[] PROGMEM = { 16,16,17,17,18,18,18,19,19,20,20,20,21,21,21,22,22,22,23,23,24,24,24,25,25,25,26,26,26,26,27,27,27,28,28,28,28,29,29,29,29,30,30,30,30,30,30,31,31,31,31,31,31,31,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,31,31,31,31,31,31,31,30,30,30,30,30,30,29,29,29,29,28,28,28,28,27,27,27,26,26,26,26,25,25,25,24,24,24,23,23,22,22,22,21,21,21,20,20,20,19,19,18,18,18,17,17,16,16,16,15,15,14,14,14,13,13,12,12,12,11,11,11,10,10,10, 9, 9, 8, 8, 8, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9,10,10,10,11,11,11,12,12,12,13,13,14,14,14,15,15,16 };
uint8_t const spinWave[] PROGMEM = { 31,31,31,31,31,31, 0,25,24,23,22,20,11,12,13,14,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 0, 4, 8,16,20,24,28,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 0, 4, 8,16,20,24,28,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 0, 4, 8,16,20,24,28,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 0, 4, 8,16,20,24,28,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 0, 4, 8,16,20,24,28,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 0, 4, 8,16,20,24,28,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 0, 4, 8,16,20,24,28,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32 };
uint8_t const blnkWave[] PROGMEM = { 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, 0,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32 };
const uint8_t* const waveTable[] PROGMEM = {sineWave, spinWave, blnkWave};
static int currentWave = 0;
void setup()
{
Serial.begin(115200);
MsTimer2::set(2000, nextPattern);
MsTimer2::start();
}
void loop()
{
Serial.print(currentWave);
Serial.print(": ");
Serial.println(pgm_read_byte_near(waveTable[currentWave]));
delay(100);
}
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void nextPattern()
{
// add one to the current pattern number, and wrap around at the end
currentWave = (currentWave + 1) % ARRAY_SIZE( waveTable);
}
The output is as follows:
0: 33
1: 255
2: 255
I can read the actual expected byte by replacing
Serial.println(pgm_read_byte_near(waveTable[currentWave]));
with
Serial.println(pgm_read_byte_near(waveTable[0]));
or
Serial.println(pgm_read_byte_near(waveTable[1]));
or
Serial.println(pgm_read_byte_near(waveTable[2]));
results with:
0: 16
1: 31
2: 32
Any idea why using the currentWave doesn’t work, and where does it pull the data from?