Hi all,
I'm trying to create a simple PWM dimmer using an ATTiny 45. I've written the code and debuged using an UNO. I basically want to use an analogRead on a potential divider to set the PWM duty. Simples. This works.
What I am having trouble with is setting different curves or scalings. I am doing this by creating a lookup array. Initially I created an array 256 large with a the value being the analogWrite value. Again this working fine on the UNO but when ported to the tiny the PWM signal did odd things, flickering and snapping on and off as I moved the pot. Shortening the array size improved things down to 26 elements where it worked as expected. I've tried PROGMEM which increases the size of array that works but only to around 40 odd.
The original programme had some averaging code to do some smoothing over 10 cycles. This is commented out below, but still there. It didn't really work on the Tiny.
If this is indeed an SRAM issue any ideas on work around or other options? Or am I doing something silly?
Thanks,
Nick
Code:
#include <avr/pgmspace.h>
/*
Simple Dimmer
Reads analog value on port defined below and outputs PWM signal referenced from lookup table
*/
//Linear
//const byte levelLookup[256] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255};
//Up and Down
//const byte levelLookup[256] = {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};
//S Curve
//const byte levelLookup[256] = {0,0,0,0,0,0,0,1,1,1,1,1,2,2,2,3,3,3,4,4,4,5,5,6,6,7,7,8,9,9,10,10,11,12,12,13,14,15,15,16,17,18,18,19,20,21,22,23,24,25,26,27,27,28,29,30,31,33,34,35,36,37,38,39,40,41,42,44,45,46,47,48,50,51,52,53,54,56,57,58,60,61,62,63,65,66,67,69,70,72,73,74,76,77,78,80,81,83,84,85,87,88,90,91,93,94,96,97,98,100,101,103,104,106,107,109,110,112,113,115,116,118,119,121,122,124,125,127,128,130,131,133,134,136,137,139,140,142,143,145,146,148,149,151,152,154,155,157,158,159,161,162,164,165,167,168,170,171,172,174,175,177,178,179,181,182,183,185,186,188,189,190,192,193,194,195,197,198,199,201,202,203,204,205,207,208,209,210,211,213,214,215,216,217,218,219,220,221,222,224,225,226,227,228,228,229,230,231,232,233,234,235,236,237,237,238,239,240,240,241,242,243,243,244,245,245,246,246,247,248,248,249,249,250,250,251,251,251,252,252,252,253,253,253,254,254,254,254,254,255,255,255,255,255,255,255};
//const byte levelLookup[100] = {0,0,0,1,2,3,4,5,6,8,10,12,14,16,18,21,24,27,29,33,36,39,42,46,50,53,57,61,65,69,73,77,81,85,90,94,98,103,107,112,116,121,125,130,134,139,143,148,152,157,161,165,170,174,178,182,186,190,194,198,202,205,209,213,216,219,222,226,228,231,234,237,239,241,243,245,247,249,250,251,252,253,254,255,255,255};
//const byte levelLookup[14] = {0,20,40,60,80,100,120,140,160,180,200,220,240,255};
//const byte levelLookup[26] = {0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190,200,210,220,230,240,255};
PROGMEM prog_uint16_t levelLookup[] = {0,0,2,4,6,10,14,18,24,29,36,42,50,57,65,73,81,90,98,107,116,125,134,143,152,161,170,178,186,194,202,209,216,222,228,234,239,243,247,250,252,254,255,255};
//int readings[10];
//byte index = 0;
//int total = 0;
//int average = 0;
#define PIN_INPUT 3
#define PIN_OUTPUT 0
void setup()
{
// for (int i = 0; i < 10; i++) {
// readings[i] = 0;
// }
//pinMode(pinOutput, OUTPUT);
}
void loop() {
//This works on UNO but not ATTiny at all...
//total = total - readings[index];
//readings[index] = analogRead(pinInput);
//total = total + readings[index];
//index++;
//byte temp = total*255/10230;
//analogWrite(pinOutput, levelLookup[temp]);
//if (index >= 10) index = 0;
//This mostly works. Using PROGMEM seams to make it better.
//Only the array 26 big works fully.
int temp = analogRead(PIN_INPUT);
temp = (temp*51)/1023;
analogWrite(PIN_OUTPUT, pgm_read_word_near(levelLookup+temp));
delay(1); // delay to keep things timely
}