Sorry for inconvience. When I use the while loop I can't switch between different animations unless the while loop is finished. I tried multiple methods including one that I attached but none of them helped.
void heartbeatEffect2() {
unsigned long startTime = millis();
unsigned long elapsedTime = 0;
const unsigned long duration = 30 * 60 * 1000;
while (digitalRead(buttonPin1) == HIGH){
while (elapsedTime < duration) {
// Check if button1 is pressed, and exit the loop if true
for (int brightness = 0; brightness <= 15; brightness++) {
display.setIntensity(0, brightness);
displayImage(IMAGES[0]);
delay(50);
}
for (int brightness = 14; brightness >= 0; brightness--) {
display.setIntensity(0, brightness);
displayImage(IMAGES[0]);
delay(50);
}
// Update elapsed time
elapsedTime = millis() - startTime;
}
}
}
Here is the last version of my code:
#include <LedControl.h>
const int buttonPin1 = 2, buttonPin2 = 3, DIN_PIN = 11, CS_PIN = 10, CLK_PIN = 13,buttonPin3 = 4;
bool runLoop1 = false, runLoop2 = false;
const uint64_t dakik[] = {
//removed
};
const int dakik_LEN = sizeof(dakik)/8;
const uint64_t sev[] = {
//removed
};
const int sev_LEN = sizeof(sev)/8;
const uint64_t ok[] = {
//removed
};
const int ok_LEN = sizeof(ok)/8;
const uint64_t ada[] = {
//removed
};
const int ada_LEN = sizeof(ada)/8;
const uint64_t kalp[] = {
//removed
};
const int kalp_LEN = sizeof(kalp)/8;
const uint64_t IMAGES[] = {
//removed
};
const int IMAGES_LEN = sizeof(IMAGES) / 8;
enum AnimationState { IDLE, LOOP1, LOOP2, LOOP3, HEARTBEAT }; // Added LOOP3
AnimationState currentState = IDLE;
LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN);
void setup() {
display.clearDisplay(0);
display.shutdown(0, false);
display.setIntensity(0, 2);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
}
void loop() {
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
switch (currentState) {
case IDLE:
if (buttonState1 == LOW) currentState = LOOP1;
else if (buttonState2 == LOW) currentState = LOOP2;
else if (buttonState3 == LOW) currentState = LOOP3; // Check for button3 press
break;
case LOOP1: loop1(); currentState = IDLE; break;
case LOOP2: loop2(); currentState = IDLE; break;
case LOOP3: loop3(); currentState = IDLE; break; // Added case for LOOP3
case HEARTBEAT: heartbeatEffect(); currentState = IDLE; break;
default: currentState = IDLE; break;
}
delay(50);
}
void displayImage(uint64_t image) {
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
display.setLed(0, i, j, bitRead((image >> i * 8) & 0xFF, j));
}
void loop1() {
const uint64_t* loops[] = {sev, ok, ada, kalp};
const int lens[] = {sev_LEN, ok_LEN, ada_LEN, kalp_LEN};
const int delays[] = {500, 200, 200, 500};
const int intensities[] = {10, 8, 8, 10}; // Adjust intensities as needed
for (int l = 0; l < 4; l++) {
display.setIntensity(0, intensities[l]); // Set intensity for the specific loop
for (int j = 0; j < lens[l]; j++) {
displayImage(loops[l][j]);
delay(delays[l]);
if (digitalRead(buttonPin2) == LOW) {
runLoop1 = false;
return;
}
}
}
}
void loop2() { heartbeatEffect(); }
void loop3() { heartbeatEffect2(); }
void heartbeatEffect() {
int i= 0;
int buttonState2 = digitalRead(buttonPin2);
while (i<5){
for (int brightness = 0; brightness <= 15; brightness++) {
display.setIntensity(0, brightness);
displayImage(IMAGES[0]);
delay(50);
}
for (int brightness = 14; brightness >= 0; brightness--) {
display.setIntensity(0, brightness);
displayImage(IMAGES[0]);
delay(50);
}
i++;
}
}
void heartbeatEffect2() {
unsigned long startTime = millis();
unsigned long elapsedTime = 0;
//display.setIntensity(0, brightness);
//isplayImage(dakik[0]);
//delay(100);
//display.clearDisplay(0);
const unsigned long duration = 60 * 1000;
while (elapsedTime < duration) {
for (int brightness = 0; brightness <= 15; brightness++) {
display.setIntensity(0, brightness);
displayImage(IMAGES[0]);
delay(50);
}
for (int brightness = 14; brightness >= 0; brightness--) {
display.setIntensity(0, brightness);
displayImage(IMAGES[0]);
delay(50);
}
elapsedTime = millis() - startTime;
}
}
For example when switching to loop1 while loop3 is active, waiting one minute is necessary for the loop to finish. Unless I can't switch between animations.