Alright, so I figured it out in the end, thank you to everyone who helped!
Here is the final code:
#include "funshield.h"
//setting up
constexpr int ledPins[] { led1_pin, led2_pin, led3_pin, led4_pin };
constexpr int ledPinsCount = sizeof(ledPins) / sizeof(ledPins[0]);
constexpr int Button1 = button1_pin;
constexpr int Button2 = button2_pin;
constexpr int activationDelay = 1000;
constexpr int periodicDelay = 300;
unsigned long lastTime = 0;
unsigned long lastTime2 = 0;
bool firsttime = true;
bool firsttime2 = true;
int counter = 0;
bool wasDown = false;
bool wasDown2 = false;
int isDown = 0;
int isDown2 = 0;
//Binary counter
void BN(int number, const int leds[], int ledsCount)
{
for (int i = ledsCount - 1; i >= 0; --i) {
bool currentBit = number & 1;
digitalWrite(leds[i], currentBit ? ON : OFF);
number = number >> 1;
}
}
//Smart function
void TheThing(int Button) {
if (Button == Button1) {
counter = (counter + 1) % 16;
BN(counter, ledPins, ledPinsCount);
}
if (Button == button2) {
counter = (counter - 1) % 16;
BN(counter, ledPins, ledPinsCount);
}
}
void setup() {
for (int i = 0; i < ledPinsCount; ++i) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], OFF);
}
pinMode(button1_pin, INPUT);
pinMode(button2_pin, INPUT);
}
void loop()
{
isDown = digitalRead(button1_pin);
isDown2 = digitalRead(button2_pin);
unsigned long times = millis();
//Button1 pressed and first loop it is being held
if (isDown == ON && firsttime == true) {
if (times - lastTime >= activationDelay) {
TheThing(Button1);
firsttime = false;
lastTime = times;
}
}
//Button1 pressed and not first loop it is being held
if (isDown == ON && firsttime == false) {
if (times - lastTime >= periodicDelay) {
TheThing(Button1);
lastTime = times;
}
}
//Button1 stopped being pressed
if (isDown == OFF) {
firsttime = true;
lastTime = times;
}
//Button2 pressed and first loop it is being held
if (isDown2 == ON && firsttime2 == true) {
if (times - lastTime2 >= activationDelay) {
TheThing(Button2);
firsttime2 = false;
lastTime2 = times;
}
}
//Button2 pressed and not first loop it is being held
if (isDown2 == ON && firsttime2 == false) {
if (times - lastTime2 >= periodicDelay) {
TheThing(Button2);
lastTime2 = times;
}
}
//Button2 stopped being pressed
if (isDown2 == OFF ) {
firsttime2 = true;
lastTime2 = times;
}
//Button1 changed states
if (wasDown != isDown) {
//Button1 currently down
if (isDown == ON) {
TheThing(Button1);
}
wasDown = isDown;
}
//Button2 changed states
if (wasDown2 != isDown2) {
//Button2 currently down
if (isDown2 == ON) {
TheThing(Button2);
}
wasDown2 = isDown2;
}
}
If anyone has any tips or ideas, feel free to share!
For now I will enjoy the fact that I managed it.