#include <MD_MAX72xx.h>
#include <SPI.h>
#include <MD_Parola.h>

const byte clk_pin = 13;
const byte data_pin = 11;
const byte chip_select_pin = 10;
const byte max_devices = 7;

const byte buttonIncreasePin = 2; // Pin for increasing speed
const byte buttonDecreasePin = 3; // Pin for decreasing speed
int scrollSpeed = 50;              // Initial scroll speed
unsigned long lastButtonChangeTime = 0;
const unsigned long buttonChangeInterval = 200; // Adjust this interval as needed

MD_Parola matrix = MD_Parola(MD_MAX72XX::PAROLA_HW, chip_select_pin, max_devices);

void setup() {
  Serial.begin(9600);
  matrix.begin();
  matrix.displayClear();

  // Set the button pins as inputs with internal pull-up resistors
  pinMode(buttonIncreasePin, INPUT_PULLUP);
  pinMode(buttonDecreasePin, INPUT_PULLUP);
}

void loop() {
  // Read the button states
  int buttonIncreaseState = digitalRead(buttonIncreasePin);
  int buttonDecreaseState = digitalRead(buttonDecreasePin);
  unsigned long currentTime = millis();

  if (buttonIncreaseState == LOW && currentTime - lastButtonChangeTime >= buttonChangeInterval) {
    // Button for increasing speed is pressed, decrease the scroll speed
    scrollSpeed = max(scrollSpeed - 20, 10);
    lastButtonChangeTime = currentTime;
  } else if (buttonDecreaseState == LOW && currentTime - lastButtonChangeTime >= buttonChangeInterval) {
    // Button for decreasing speed is pressed, increase the scroll speed
    scrollSpeed = min(scrollSpeed + 20, 500);
    lastButtonChangeTime = currentTime;
  }

  Serial.print(scrollSpeed);
  Serial.println();

  if (matrix.displayAnimate()) {
    matrix.displayText("NILOY", PA_LEFT, scrollSpeed, 0, PA_SCROLL_LEFT);
  }
}