Hello,
I am trying to understand how and why my code is supposed to work. I can get it to do what I want, but I would stop short of saying I truly understand it. I am trying to create a lookup table using PROGMEM, and when I use a for loop, the program is locked in that loop and won't due anything else until the for loop is complete. I would like to track time(millis()) and Serial.print time for testing. I can get the for loop to do as it is intended, maybe there is a way around the for loop to do as I wanted, so I recreated the for loop using if statements. Everything works a lot better, however, it won't use the 1st step in the PROGMEM, and skips over it unless I force it to -1 "pgm_read_byte_near(LEDset + i - 1);"
I am attaching my code for advise, and so that I can learn more about what's going on. Thanks in advance for the help.
int LED_pin = 9;
int minBright = 50; //I want to restrict brightness in later code, it works as is, but it won't do what I want long term as it will reduce the desired steps
int maxBright = 100;
int Brightness;
int i; //used to step through lookup table, needs to be global, not sure why
unsigned long previousMillis;
/******27 steps******/
const PROGMEM uint8_t LEDset[] = {
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,250,255};
void setup(){
pinMode(LED_pin, OUTPUT);
Serial.begin(9600);
}
void loop(){
unsigned long currentMillis = millis();
unsigned long time = millis();
if (currentMillis - previousMillis >= 1000){
if ( i < 27){
i++;
Brightness = pgm_read_byte_near(LEDset + i - 1);
analogWrite(LED_pin, Brightness= constrain(Brightness, minBright, maxBright)); //I want to restrict brightness in later code, it works as is, but it won't do what I want long term, as it will reduce the desired steps
Serial.print("Time = ");
Serial.println(time);
Serial.print("Brightness = ");
Serial.println(Brightness);
Serial.print("i = ");
Serial.println(i);
/*for (byte i = 0; i < 27; i++){ //Cycles through the table as expected, using for loop locks up the program until it has finished. I want to be able to do other things while in the loop
Brightness = pgm_read_byte_near(LEDset + i);
analogWrite(LED_pin, Brightness);
Serial.print("Time = ");
Serial.println(time);
Serial.print("Brightness = ");
Serial.println(Brightness);
} */
}
previousMillis = currentMillis;
}
}