I have a pretty ambitious Arduino project for a newbie that uses LEDs to illustrate traffic flow on a physical intersection model.
I'm using TinkerCAD to make a mock-up before I make the real one. Photo of TinkerCAD attached. The strips are the roads, and each tile lights up when a simulated vehicle is occupying it. The tiles are class objects that have an integer location and a boolean occupied status that vehicles will read before advancing to it. The vehicles are also class objects with variables and functions of their own and they're created the same way as tiles.
I'm having an issue where after the tile objects are created, the program freezes while assigning location values to the tiles and outputs "ÿ" in the serial monitor. See screenshot.
At first I wondered if it was a memory issue, but nothing I do with optimizing code has had any effect. The error happens in the same place every time. I've also done some math and I don't think I should be reaching the memory limit yet, though I don't know how that works in TinkerCAD.
I also tried splitting the for loop into smaller chunks, but that didn't have any effect either.
If I shrink the NUM_TILES value to 100-200 the program will advance to the next chunk, but then freezes mid print in the console when it's adding values for the vehicle objects (without the weird ÿ, interestingly enough).
Like I said, I'm new to Arduino and coding, so I'm not super familiar with the more advanced aspects like memory overflow, pointers, objects, etc. I just know enough to be dangerous. Any help is appreciated.
Relevant code excerpt below:
#include <Adafruit_NeoPixel.h>
#define RED 255,0,0
#define GREEN 0,255,0
#define BLUE 0,0,255
#define YELLOW 100,100,0
#define WHITE 100,100,100
#define NUM_TILES 292
#define STRIP_OUT 2
Adafruit_NeoPixel Strip1(NUM_TILES, STRIP_OUT);
class Tile
{
private:
short int _location;
bool _occupied = false;
public:
Tile(){}
void setlocation(short int location){_location = location;}
bool checkoccupied(){return _occupied;}
void occupy(){_occupied = true;}
void vacate(){_occupied = false;}
};
void LEDRed(int bulb){
Strip1.setPixelColor(bulb, RED);}
void LEDGreen(int bulb){
Strip1.setPixelColor(bulb, GREEN);}
void LEDBlue(int bulb){
Strip1.setPixelColor(bulb, BLUE);}
void LEDYellow(int bulb){
Strip1.setPixelColor(bulb, YELLOW);}
void LEDWhite(int bulb){
Strip1.setPixelColor(bulb, WHITE);}
Tile *tile = new Tile[NUM_TILES];
void setup()
{
for(int i = 0 ; i < NUM_TILES; i++){tile[i].setlocation(i);} // Freezes during this step.
Serial.print("\nTile Setup Complete");
}