New to Arduino, built in LED flashing

Hi there, first of all I'm new to the Arduino world, I've been web developer for years but i decided to get an arduino for fun and to build a simple midi controller for my guitar's processor

With that in mind I purchased a mega board on Amazon, i went with a cheap board just to get an idea of how they work before i jumped into getting the real thing.

For starters since i'm familiar with c and c++, to refresh i wrote a quick library for the LCD that uses the LiquidCrystal library, just so i can print on the LCD with one line, nothing crazy just a simple library which works like a charm.

Then I figured I'd write a simple library to handle the buttons, well 2 libraries, one is for a Button object that just stores the pin and the last state and the other one which handles creating those buttons which works fine, i pass an array of pins and it works good using the Serial to monitor button presses

Then after that was done I jumped into using both the LCD and Buttons library together to monitor the button press on the LCD and that's where i'm having issues.

For some reason if i create more than 3 Button objects the LCD goes blank and the built in led starts flashing, 2 flashes per second or so, If I create 3 buttons then it works just fine. If i create 3 buttons and do Serial.begin with any baud rate then again the same thing happens, flashing built in led.

My guess is i'm running out of RAM? I'm not getting any compile errors, when i upload it the message says i'm using 2% for the Sketch and 1% of dynamic memory

My buttons library is bare bone, all it does is create multiple buttons and set the pinMode() for each the Button object only holds 2 ints the pin number and the last state and has a method that returns the pin number when called.

Any help would be appreciated, thanks!

My guess is you are accessing an array past the limits of what you've declared.
Hard to say without seeing any code tho.

You have 8K SRAM with a Mega, 1% of that is not much at all.

Hi, thank you for the reply!! here's the code

Button.h

#ifndef Button_h
#define Button_h
#include "Arduino.h"

class Button {
	public:
		Button(uint8_t _pin, uint8_t mode);
		uint8_t pin;
		int lastState;
		int getState();
};
#endif

Button.cpp

#include "Arduino.h"
#include "Button.h"

Button::Button(uint8_t _pin, uint8_t mode){
	pin = _pin;
	pinMode(pin,mode);
}
int Button::getState(){
	return digitalRead(pin);
}

Buttons.h

#ifndef Buttons_h
#define Buttons_h
#include "Arduino.h"
#include "Button.h"

class Buttons {
	public:
		Buttons(uint8_t _buttons[], int n, bool pullUp);
		Button buttons[];
		int inputMode;
		uint8_t numberOfButtons = 0;
		Button getButton(int _id);
};

#endif

Buttons.cpp

#include "Arduino.h"
#include "Button.h"
#include "Buttons.h"

Buttons::Buttons(uint8_t _buttons[], int n, bool pullUp){
	numberOfButtons = n;
	if(pullUp==true){
		inputMode = INPUT_PULLUP;
	} else {
		inputMode = INPUT;
	}
	for (int i = 0; i < numberOfButtons; i++){
		buttons[i] = Button(_buttons[i],inputMode);
	}
}
Button Buttons::getButton(int _id){
	return buttons[_id];
}

.ino file

#include <LCD.h>
#include <Buttons.h>

LCD lcd(16,2,12, 11, 5, 4, 3, 2);

uint8_t buttonPins[] = {20,22,24,26};

Buttons buttons(buttonPins, 4, true); // (pins array, pins array length, if pullup)

void setup() {
  lcd.start("Starting Up...");// Shows up message on LCD at startup

  // works if the buttonPins is has 3 items
  lcd.printLine1("Hello");
  // if the buttonPins is 4 or more i get the LED flashing
  
  /*
  // This code works as long as i don't use the LCD class
  Serial.begin(115200);
  Serial.print("Array is  ");
  Serial.println(buttons.numberOfButtons);
  Serial.print("pin: ");
  Serial.println(buttons.buttons[0].pin);
  Serial.print("pin: ");
  Serial.println(buttons.buttons[1].pin);
  Serial.print("pin: ");
  Serial.println(buttons.buttons[2].pin);
  Serial.print("pin: ");
  Serial.println(buttons.buttons[3].pin);
  */
}

void loop() {
  // put your main code here, to run repeatedly:

}

Ok looks like i fixed it, the issue was on Buttons.cpp, i guess i wasn't initilizing the button into the array properly

Buttons::Buttons(uint8_t _buttons[], int n, bool pullUp){
	numberOfButtons = n;
	if(pullUp==true){
		inputMode = INPUT_PULLUP;
	} else {
		inputMode = INPUT;
	}
	for (int i = 0; i < numberOfButtons; i++){
		//HOW I HAD IT BEFORE

		//buttons[i] = Button(_buttons[i],inputMode);

		//FIX, adding the Button type
		Button buttons[i] = Button(_buttons[i],inputMode);
	}
}