Skip to main content
added 76 characters in body
Source Link
Majenko
  • 105.9k
  • 5
  • 82
  • 139

Why would this simple code which prints out array values give the right value 240 when the array is addressed with index directly addressed by number but give 184 when addressed usin a varibale n that is incrementing?

PROGMEM const unsigned char COLSA[8] = {
240, 240, 240, 240, 240, 240, 240, 240}; //Stored pattern

void setup() { Serial.begin(9600); }

void loop() { int n; for(n=0;n<8;n=n+1) { Serial.println(n); Serial.println(COLSA[1]); //n=2; //If I use n in this way things are fine. It prints 240. Else it prints 184 Serial.println(COLSA[n]); } delay(100); }

PROGMEM const unsigned char COLSA[8] = {  
      240, 240, 240, 240, 
      240, 240, 240, 240};  //Stored pattern
      
void setup() {
  Serial.begin(9600);
}

void loop() {
  int n;
   for(n=0;n<8;n=n+1) 
    {
      Serial.println(n);
      Serial.println(COLSA[1]);
      //n=2;  //If I use n in this way things are fine. It prints 240. Else it prints 184
      Serial.println(COLSA[n]);
    }
     delay(100);
}

Why would this simple code which prints out array values give the right value 240 when the array is addressed with index directly addressed by number but give 184 when addressed usin a varibale n that is incrementing?

PROGMEM const unsigned char COLSA[8] = {
240, 240, 240, 240, 240, 240, 240, 240}; //Stored pattern

void setup() { Serial.begin(9600); }

void loop() { int n; for(n=0;n<8;n=n+1) { Serial.println(n); Serial.println(COLSA[1]); //n=2; //If I use n in this way things are fine. It prints 240. Else it prints 184 Serial.println(COLSA[n]); } delay(100); }

Why would this simple code which prints out array values give the right value 240 when the array is addressed with index directly addressed by number but give 184 when addressed usin a varibale n that is incrementing?

PROGMEM const unsigned char COLSA[8] = {  
      240, 240, 240, 240, 
      240, 240, 240, 240};  //Stored pattern
      
void setup() {
  Serial.begin(9600);
}

void loop() {
  int n;
   for(n=0;n<8;n=n+1) 
    {
      Serial.println(n);
      Serial.println(COLSA[1]);
      //n=2;  //If I use n in this way things are fine. It prints 240. Else it prints 184
      Serial.println(COLSA[n]);
    }
     delay(100);
}
Source Link

Addressing contents of array using incrementing variable

Why would this simple code which prints out array values give the right value 240 when the array is addressed with index directly addressed by number but give 184 when addressed usin a varibale n that is incrementing?

PROGMEM const unsigned char COLSA[8] = {
240, 240, 240, 240, 240, 240, 240, 240}; //Stored pattern

void setup() { Serial.begin(9600); }

void loop() { int n; for(n=0;n<8;n=n+1) { Serial.println(n); Serial.println(COLSA[1]); //n=2; //If I use n in this way things are fine. It prints 240. Else it prints 184 Serial.println(COLSA[n]); } delay(100); }