Go Down

Topic: Crashing or Data Corruption? (Read 1 time) previous topic - next topic

davisjacobj

I have several arrays: float array1[256] = { .... }
As soon as I access more than two of them, the serial output becomes garbled:
Code: [Select]
}?
}?
}?
}?
}?
}?
}?
}?
}?
}?
}?
}?
}?
}?
}?

as soon as I comment out the usage of the 3rd array
Code: [Select]
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff
stuff


Here's my code. It's an LED controller. I use some arrays of data to determine what values each LED should be outputting. There's 3 inputs: digital on pin 2 (switch), analog on pins 1 and 2 (pots). The data used is dependent on the pot value.
Code: [Select]
bool debug = false;

// Status
int valueCt = 0;
int valueDim = 0;
int valueBright = 0;
int levelR = 0;
int levelG = 0;
int levelB = 0;

bool power = false;

// Time
int timeCur = 0;
int timeReadPot = 0;

// Lookup tables
int ctRed[] = { 256 bytes of data };
int ctGreen[] = { 256 bytes of data };

float brightness[] = { 256 bytes of data };
 
void setup()
{
  Serial.begin( 9600 );
 
  pinMode( 2, INPUT );
  pinMode( 9, OUTPUT );
  pinMode( 10, OUTPUT );
 
  analogWrite( 9, 0 );
  analogWrite( 10, 0 );
 
  pinMode( 5, OUTPUT );

  digitalWrite( 5, HIGH );
}

void loop()
{
  power = digitalRead( 2 );
  valueCt = analogRead( 1 )/4;
  valueDim = analogRead( 2 )/4;
 
  if( power )
  {
    Serial.println( "stuff" );
    levelR = (int)( ctRed[valueCt]*brightness[valueDim] );
    //levelG = (int)( ctGreen[valueCt]*brightness[valueDim] ); <---- This line is commented

    analogWrite( 9, levelR );
    // analogWrite( 10, levelG );
  }
  else
  {
    analogWrite( 9, 0 );
  }
 
  delay( 250 );
}


Along with the serial data corruption in the console, the analogWrite values get all jumbled and the LEDs just flash wildly. Is this a problem with memory overrun or something? I don't even need to use analogWrite to cause the problems. It's simply in accessing the ctGreen array.

I appreciate in advanced any help, suggestions or criticisms that anyone has.

JChristensen

When you say
Quote
float brightness[] = { 256 bytes of data };


do you really mean that there are 256 values in the array? Are you aware that a single float value requires four bytes of storage?  And that ints require two bytes? If you really mean 256 values, then between the three arrays, 2048 bytes have been allocated. The ATmega328P only has 2048 bytes of RAM, and we don't get to use them all, as some is required for stack space, some is used by the Arduino core, etc.  Use up all the RAM and undefined results are pretty much guaranteed.

davisjacobj

You're absolutely right and I didn't even think about the amount of memory each data type takes... I guess I need to make those functions instead of data because I'm WAYYYY over the memory limit.

4 bytes x 256 = 1024 bytes
2 bytes x 256 x 3 = 1536 bytes

2560 > 2048 :(

Thanks for revealing the obvious for me.

johnwasser

You can use PROGMEM to store large arrays of constants in FLASH.  The AVR processor has special instructions for reading from FLASH so you need to call a function to fetch the data, but that's not too hard.

Since your output is 8 bits (analogWrite()) you should probably just use one byte integers everywhere.
Code: [Select]

byte brightness[];  //  Values from 0=off to 255=full
byte red_values[];  //  Values from 0=off to 255=full

// Multiply the color value by brightness, then divide by 255 to get value from 0 to 255
analogWrite(red_pin, ((unsigned int) red_values[v] * (unsigned int) brightness[b]) / 255);


Similarly
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up