Need help at coding

You should stop investing time into apologising and start real learning.

You seem to stuff together code-fragments that you find online without understanding them.

You did not write when you started this project. Until 2. May there are 5 days left.

If you go on stuffing things together in hoping that it will work it is very unlikely that this method will be successfull right in time.

To draw a picture that shall explain it:

Your trying to make it work is like
you can ride a normal bike but have no experience with BMX-freestyle artistic biking. And now you are trying to do BMX-freestyling riding a half-pipe trying to ride saltos.

You can go on for 5 days trying again and again riding saltos using different half-pipes. This will not work.

You should practise more easy things for 2 or 3 days like jumps over a ramp without salto. To get the feeling for the BMX-bike and your body.
And after that starting practising saltos.

But you seem to be in a fear you won't finish it and keep waisting time by trying to practise saltos. For which it is very sure if you do this as the first step it will surely not work.

Me personal I will NEVER post ready to use code.
I will support learning.

Your code can be divided into the following parts:

  1. reading in the MPU-values
  2. checking if it is
    a. NOT tilt to the left nor to the right
    b. tilt to the left
    c. tilt to the right

which is a three-part checking.

  • reading in the hall-sensor with setting the boolean flag

  • checking the boolean flag "hall-sensor beeing triggered" or not
    and in case it is triggered stop tilt-animation immidiately

  • show "STOP" on the display.

Terminating the tilt animation immidiately means to replace ALL for-loops in the animation with a non-blocking way of executing the code.

To able to do this you have to LEARN and UNDERSTAND non-blocking timing.
My offer is to support your learning. You have to decide either following this way or going on in the way you worked before but without my support.

This is a tutorial that explains non-blocking timing

It is a crucial pre-condition to understand the basic principle how non-blocking timing works to proceed.

I just read it and I'm wondering if it really is applicable to my code. Base on my understandings Non - blocking checks how much time has passed by and if the right amount of time has passed by it would execute that part of the code. You are free to correct me but tilting left and right to display the left and right turn animation doesn't require enough time to execute this part of code, it needs to surpass the value of ax < -4000 or ax > 4000. The display of STOP STOP doesn't also require enough time to execute this code only if the high magnetic field is detected by the Hall effect and clear the display when the magnetic field is Falling.

Now we are on the road to find the solution.

You are asking specific questions. This shows where your understanding is at the moment.
And this makes clear which detail I have to explain next.

Maybe you do not understand it. The code-version in post # 44 executes the tilt-animation in a non-blocking way .

You should really answer this question

do you understand what timing these lines of code create?

What else except writing with BIG BOLD LETTERS does it need to make you answer this question?

If I understand it right, It checks if 5 seconds have elapsed since the last time the Hall effect activated, and if 5 seconds have passed then it will execute the STOP STOP function to handle the hall effect sensor and update the display. One error I see here is the time its supposed to be 5,000 instead of 5,000,000 and also if I do this code it will constantly send the STOP STOP at the MAX7219 after the 5 seconds have passed even if the Hall Effect didn't detect a high magnetic field. Correct me if I'm wrong.

Here is a video tutorial that explains the non-blocking timing with code in the

if (currentMillis - lastHallEffectTime >= 5000)

style

This video is 15 minutes long. Huh?! soo long!!??? Yes !
Leave out 60 tik-toks and you have 15 minutes time
After these 15 minutes you will understand how it works.

getting an OVERVIEW about the code:

After that take a sheet of paper and write down in normal words.
Yes you are reading right in normal words.

The different things your code shall do.
just as a unsorted list

  • measure ........
  • check if ..........
    • in case of ....... do .....
    • otherwise ..... do .....

than add on the right what parts of this "doing" shall be executed all the time and what parts are executed from time to time.

You might get additional ideas what your code have to to additionally.
Add those ideas to the list. Write them down in normal words.

This will help you to get a functional overview.

If you are not yet very familiar with coding the translation from functional to real code is unusual to you. Without having a clear picture about the PURE functional level it is hard to understand what the code does / or shall do.

And this is the reason why it is important to not start immidiately with saltos in the halfpipe
ehm ups ! sorry

to not write code or use online found code immidiately

I've ended up with this

Measure

  • Measure the value of ax, ay, and az in accelerometer.
  • Measure the magnetic field whether it is high or low.

Check if

  • Check if the value of ax is ax > 4000, if so, do the left turn animation.
  • Otherwise check if the value of ax is ax < -4000, if so, do the right turn animation.
  • Check if the magnetic field is High, if so, display the STOP STOP STOP at the MAX7219.
  • Otherwise if the magnetic field is Falling, clear the display.

The only hard part for me is, I dont know where and how to start.

Your description is pretty good but there are some details missing.

So to make it very explicit:
will measuring ax be: done all the time ____ or_____ only from time to time?

will reading in the magnetic sensor be: done all the time ____ or_____ only from time to time?

As far as I have understood your descriptions:

whenever the HALL-sensor detects strong magnetic field immidiately show STOP STOP

Your device can be in the following 4 "modes"

  1. show nothing on the display // <=== this is a mode too !
  2. show tilt left animation
  3. show tilt right animation
  4. show STOP STOP

If the functionality shall be:
as soon as a magnet is in front of the HALL-Sensor
which makes the HALL-Sensor detecting a strong magnetic field
show immidiately "STOP STOP" on the display

This means that a tilt left or tilt-right-animation shall be stopped immidiately and your device changes to mode 4 show STOP STOP

This includes that the tilt-animation can have started 0.1 seconds ago and shall be immidiately be stopped.

is this correct?

if it is correct executing the tilt-animation must be two things:

  1. CONDITIONAL to strong magnetic field NOT detected
  2. animation must be stopable at any time

the 2. thing: animation must be stopable at any time requires checking very fast
strong magnetic field detected?

This is done by proceeding the animation in steps with quickly jump in / quickly jump out

check "strong magnetic field detected?"
if no strong magnetic field is detected do one single step of the animation

there is function loop()
so the basic structure is

void loop()
  readIn_Ax_Ay_Az();
  
  // sensorTriggered is set to true by interrupt
  if (sensorTriggered == false) {
    
    if (tiltToTheRight) {
      do_ONE_AnimationStep_toTheRight();
    }

    if (tiltToTheLeft) {
      do_ONE_AnimationStep_toTheLeft();
    }
    
  }
  else {
    displayStopMessage();  
  }
}

Make an attempt to use the looping character of void loop() itself
to proceed through your tilt-to-the-right-animation by
jump in do one animation-step
jump out

with void loop() caring about the repeating / the looping

Can you double check this code, I have a good feeling about this, also

Answer

  • Yes the ax and the magnetic sensor should be read and measure at all time
void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);
  displayAcceleration(ax, ay, az);

  // Check for strong magnetic field
  bool strongMagneticField = sensorTriggered;
  if (strongMagneticField) {
    displayStopMessage();
    sensorTriggered = false; // Reset the flag after displaying the message
  }

  // Execute tilt animation if strong magnetic field is NOT detected
  if (!strongMagneticField) {
    unsigned long currentMillis = millis();
    if (currentMillis - lastAnimationUpdate >= tiltAnimationDelay) {
      lastAnimationUpdate = currentMillis;

      if (ax > 4000) {
        tiltRightAnimation_OneStep();
      } else if (ax < -4000) {
        tiltLeftAnimation_OneStep();
      }
    }
  }
}

Can you check this, it works the same as your basic structure but with the logic I have in my coding, the only issue is that I don't have the arduino in me as of now so I have no way to tell whether it works or not but it's still good if you can double check it. Thank you very much again for your support and help.

Hey just an quick update, tried it at the arduino and it won't work can you guide me how to fix this?

ok.

Do you have any clue how?

The code that you have posted uses the function-names that I used in my post that shows the basic structure. I mean this one

Basic structure is meant as showing the basic principle.

I want to give an example with using pictures.
This picture shows the principle how a gear with two tooth-wheels work.

It is really ONLY showing the principle.

It does not even represent a toy-gear. Something like this

You will have to build the real functions.
And therefore you need a basic understanding of how functions are written.

It does not make sense that you try to build this gear as the next step


because you do not yet have a basic understanding of how to build gears with this product.

You would have to do some basic exercises like this

These are picture that shall visualise the principle.

For porgramming you should at least read
part 1: Arduino Sketch Structure and Flow
and
part 2: Arduino Sketch Main Loop and Calling Functions

of this tutorial.
Arduino learn to program

This will take you 20 to 30 minutes.

After that make an own attempt to
pick that part of the code that you think does the lefttilt-animation
and write your first own attempt to put this code into a function with name

"do_ONE_Step_leftTiltAnimation"
I suggest that you write it down
and
that you add all the questions that arise in your head while you try to write it.
I put a pre-condition for further help:
I will only comment and explain how to do it right if you post your

own attempt

of a function that has a part of this code inside

#include <Wire.h>
#include <MPU6050.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <LedControl.h>

#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CS_PIN 53
#define DATA_PIN 51
#define CLK_PIN 52
#define HALL_SENSOR_PIN 2

MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
MPU6050 mpu;

LedControl lc = LedControl(51, 52, 53, 4); // Pins: DIN, CLK, CS, # of Displays connected
volatile bool sensorTriggered = false; // Use volatile for interrupt safety

unsigned long tiltAnimationTimer = 0; // Timer for tilt animation delay
unsigned long scrollTimer = 0; // Timer for scrolling message delay
const unsigned long tiltAnimationDelay = 200; // Delay between tilt animation frames
const unsigned long scrollDelay = 20; // Delay for scrolling message

// Put values in arrays
byte invader1a[] =
    {
        B00010010, // First frame of invader #1
        B00100100,
        B01001001,
        B10010010,
        B10010010,
        B01001001,
        B00100100,
        B00010010};

byte invader1b[] =
    {
        B00000000, // Second frame of invader #1
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader2a[] =
    {
        B00010010, // First frame of invader #1
        B00100100,
        B01001001,
        B10010010,
        B10010010,
        B01001001,
        B00100100,
        B00010010};

byte invader2b[] =
    {
        B00000000, // Second frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader3a[] =
    {
        B00010010, // First frame of invader #1
        B00100100,
        B01001001,
        B10010010,
        B10010010,
        B01001001,
        B00100100,
        B00010010};

byte invader3b[] =
    {
        B00000000, // First frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader4a[] =
    {
        B00010010, // First frame of invader #1
        B00100100,
        B01001001,
        B10010010,
        B10010010,
        B01001001,
        B00100100,
        B00010010};

byte invader4b[] =
    {
        B00000000, // Second frame of invader #2
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

// Put values in arrays
byte invader1c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader1d[] =
    {
        B00000000, // Second frame of invader #1
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader2c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader2d[] =
    {
        B00000000, // Second frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader3c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader3d[] =
    {
        B00000000, // First frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000
    };
        
byte invader4c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader4d[] =
    {
        B00000000, // Second frame of invader #2
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

void setup() {
  Serial.begin(115200);
  Wire.begin();
  mpu.initialize();
  SPI.begin(); // Initialize SPI

  lc.shutdown(0, false); // Wake up displays
  lc.shutdown(1, false);
  lc.shutdown(2, false);
  lc.shutdown(3, false);
  lc.setIntensity(0, 15); // Set intensity levels
  lc.setIntensity(1, 15);
  lc.setIntensity(2, 15);
  lc.setIntensity(3, 15);
  lc.clearDisplay(0); // Clear Displays
  lc.clearDisplay(1);
  lc.clearDisplay(2);
  lc.clearDisplay(3);

  myDisplay.begin();
  myDisplay.setIntensity(8);
  myDisplay.displayClear();

  pinMode(HALL_SENSOR_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_PIN), sensorInterrupt, CHANGE); // Attach interrupt on CHANGE
}

void sensorInterrupt() {
  sensorTriggered = !digitalRead(HALL_SENSOR_PIN); // Read the sensor state in the interrupt
}

void displayAcceleration(int16_t ax, int16_t ay, int16_t az) {
  // Print the X, Y, and Z values to the serial monitor
  Serial.print("X: ");
  Serial.print(ax);
  Serial.print(" | Y: ");
  Serial.print(ay);
  Serial.print(" | Z: ");
  Serial.println(az);
}

void tiltRightAnimation() {
  sinvader1a();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader2a();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader3a();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader4a();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader1b();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader2b();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader3b();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader4b();
  tiltAnimationTimer = 0; // Reset timer after animation is complete
}

void tiltLeftAnimation() {
  sinvader4c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader3c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader2c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader1c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader4d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader3d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader2d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader1d();
  tiltAnimationTimer = 0; // Reset timer after animation is complete
}

void displayStopMessage() {
  myDisplay.setTextAlignment(PA_CENTER);
  myDisplay.setSpeed(30);
  myDisplay.displayScroll("STOP STOP STOP STOP ", PA_CENTER, PA_SCROLL_LEFT, 20);
}

void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);
  displayAcceleration(ax, ay, az);

  // Detect tilt direction and run appropriate animation
  if (ax > 5000) {
    tiltRightAnimation();
  } else if (ax < -5000) {
    tiltLeftAnimation();
  }

  // Check for strong magnetic field
  if (sensorTriggered) {
    displayStopMessage();
    sensorTriggered = false;
  } else {
    myDisplay.displayReset();
  }

  // Handle scrolling message
  if (millis() - scrollTimer >= scrollDelay) {
    scrollTimer = millis(); // Update scroll timer
    myDisplay.displayAnimate();
  }

  // Check if it's time to update display frames
  if (tiltAnimationTimer > 0 && millis() >= tiltAnimationTimer) {
    tiltAnimationTimer = 0; // Reset timer after displaying the frame
    myDisplay.displayAnimate();
  }
}


// Function definitions for displaying animation frames
void sinvader1a()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(0, i, invader1a[i]);
  }
}

void sinvader1b()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(0, i, invader1b[i]);
  }
}

void sinvader2a()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(1, i, invader2a[i]);
  }
}

void sinvader2b()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(1, i, invader2b[i]);
  }
}

void sinvader3a()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(2, i, invader3a[i]);
  }
}

void sinvader3b()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(2, i, invader3b[i]);
  }
}

void sinvader4a()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(3, i, invader4a[i]);
  }
}

void sinvader4b()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(3, i, invader4b[i]);
  }
}

void sinvader4c()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(3, i, invader4c[i]);
  }
}

void sinvader3c()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(2, i, invader3c[i]);
  }
}

void sinvader2c()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(1, i, invader2c[i]);
  }
}

void sinvader1c()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(0, i, invader1c[i]);
  }
}

void sinvader1d()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(0, i, invader1d[i]);
  }
}

void sinvader2d()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(1, i, invader2d[i]);
  }
}

void sinvader3d()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(2, i, invader3d[i]);
  }
}

void sinvader4d()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(3, i, invader4d[i]);
  }
}


void loop()
  readIn_Ax_Ay_Az();
  
  // sensorTriggered is set to true by interrupt
  if (sensorTriggered == false) {
    
    if (tiltToTheRight) {
      do_ONE_AnimationStep_toTheRight();
    }

    if (tiltToTheLeft) {
      do_ONE_AnimationStep_toTheLeft();
    }
    
  }
  else {
    displayStopMessage();  
  }
}

If you would just post something like this

do_ONE_Step_leftTiltAnimation()
  // I don't know what to put in here

I will repeat
make an own attempt to pick a part of the complete code.
20 or 50 times if it is nescessary.
So please take a part of the real code-lines from the complete code above at your first attempt.
And

after

having done this add all your questions

here's my attempt I also included comments to make sure that I understand every function

#include <Wire.h>
#include <MPU6050.h>
#include <LedControl.h>

unsigned long lastAnimationUpdate = 0;
const unsigned long tiltAnimationDelay = 200; // Delay between tilt animation frames

// Bytes for the Left arrow animation
byte invader1c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader1d[] =
    {
        B00000000, // Second frame of invader #1
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader2c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader2d[] =
    {
        B00000000, // Second frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader3c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader3d[] =
    {
        B00000000, // First frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000
    };
        
byte invader4c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader4d[] =
    {
        B00000000, // Second frame of invader #2
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

void setup() {
  Serial.begin(115200);
  Wire.begin();
  mpu.initialize();
  SPI.begin(); // Initialize SPI

  lc.shutdown(0, false); // Wake up displays
  lc.shutdown(1, false);
  lc.shutdown(2, false);
  lc.shutdown(3, false);
  lc.setIntensity(0, 15); // Set intensity levels
  lc.setIntensity(1, 15);
  lc.setIntensity(2, 15);
  lc.setIntensity(3, 15);
  lc.clearDisplay(0); // Clear Displays
  lc.clearDisplay(1);
  lc.clearDisplay(2);
  lc.clearDisplay(3);

  myDisplay.begin();
  myDisplay.setIntensity(8);
  myDisplay.displayClear();
}

// Bytes function
void do_ONE_Step_lefttiltAnimation() {
  sinvader4c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader3c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader2c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader1c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader4d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader3d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader2d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader1d();
  tiltAnimationTimer = 0; // Reset timer after animation is complete
}

// Loop the function so it will always display the left animation when it reaches the certain value
void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az); // measure the value of the accelerometer to make it display the left turn animation 
  displayAcceleration(ax, ay, az);

  // Detect tilt direction and run appropriate animation
  if (ax > 5000) {
    do_ONE_Step_lefttiltAnimation();
  }
}

// Define every byte to prevent error
void sinvader4c()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(3, i, invader4c[i]);
  }
}

void sinvader3c()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(2, i, invader3c[i]);
  }
}

void sinvader2c()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(1, i, invader2c[i]);
  }
}

void sinvader1c()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(0, i, invader1c[i]);
  }
}

void sinvader1d()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(0, i, invader1d[i]);
  }
}

void sinvader2d()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(1, i, invader2d[i]);
  }
}

void sinvader3d()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(2, i, invader3d[i]);
  }
}

void sinvader4d()
{
  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(3, i, invader4d[i]);
  }
}

Im dedicated to learn to make this code of ours to work before the deadline, so if you could teach me I'll use this way to learn more. Just enlighten me and I'll do the best I can to learn and provide the satisfaction you needed to help me.

OK so here is my feedback.

Doing only one step of the animation means executing only

    lc.setRow(0, i, invader1a[i]);

instead of the complete for-loop

  for (int i = 0; i < 8; i++) // no reaction to hall-sensor
  {
    lc.setRow(0, i, invader1a[i]);
  }

still to show the complete animation variable i must be
i = 0;
i = 1;
i = 2;
i = 3;
i = 4;
i = 5;
i = 6;
i = 7;

but the counting must be done without a for-loop
and this means the line

    lc.setRow(0, i, invader1a[i]);

must be repeated and variable i must count up from 0 to 7 .

I am sure that you have questions how this can be done.
You should post your own attempt how you think that this might work

This requires to modify the code that you have posted
please modify

Okay thank you very much I'll answer as soon as I got my next attempt it may take a while since I still need to understand fully what you meant since I'm having a hard time catching up.

@ramses_101706 - Which post is the current, complete program?

If you modify your Serial Monitor output to watch "sensorTriggered"

void displayAcceleration(int16_t ax, int16_t ay, int16_t az) {
  // Print the X, Y, and Z values to the serial monitor
  Serial.print("X: ");
  Serial.print(ax);
  Serial.print(" | Y: ");
  Serial.print(ay);
  Serial.print(" | Z: ");
  Serial.print(az);
  Serial.print(" | H: ");
  Serial.println(sensorTriggered);
}

You will see "sensorTriggered" only momentarily becomes HIGH before being changed to LOW, so the following "STOP" will only get a short time to display.

  // Check for strong magnetic field
  if (sensorTriggered) {
    displayStopMessage();
    sensorTriggered = false;
  } else {
    myDisplay.displayReset();
  }

Find a way to keep "sensorTriggered" HIGH to keep "STOP" on the display.

I think I made it right?? I'm not sure but come look at this

#include <Wire.h>
#include <MPU6050.h>
#include <LedControl.h>

unsigned long lastAnimationUpdate = 0;
const unsigned long tiltAnimationDelay = 200; // Delay between tilt animation frames

// Bytes for the Left arrow animation
byte invader1c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader1d[] =
    {
        B00000000, // Second frame of invader #1
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader2c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader2d[] =
    {
        B00000000, // Second frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader3c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader3d[] =
    {
        B00000000, // First frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000
    };
        
byte invader4c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader4d[] =
    {
        B00000000, // Second frame of invader #2
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

void setup() {
  Serial.begin(115200);
  Wire.begin();
  mpu.initialize();
  SPI.begin(); // Initialize SPI

  lc.shutdown(0, false); // Wake up displays
  lc.shutdown(1, false);
  lc.shutdown(2, false);
  lc.shutdown(3, false);
  lc.setIntensity(0, 15); // Set intensity levels
  lc.setIntensity(1, 15);
  lc.setIntensity(2, 15);
  lc.setIntensity(3, 15);
  lc.clearDisplay(0); // Clear Displays
  lc.clearDisplay(1);
  lc.clearDisplay(2);
  lc.clearDisplay(3);

  myDisplay.begin();
  myDisplay.setIntensity(8);
  myDisplay.displayClear();
}

// Bytes function
void do_ONE_Step_lefttiltAnimation() {
  sinvader4c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader3c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader2c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader1c();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader4d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader3d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader2d();
  tiltAnimationTimer = millis() + tiltAnimationDelay; // Set timer for next frame

  sinvader1d();
  tiltAnimationTimer = 0; // Reset timer after animation is complete
}

// Loop the function so it will always display the left animation when it reaches the certain value
void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az); // measure the value of the accelerometer to make it display the left turn animation 
  displayAcceleration(ax, ay, az);

  // Detect tilt direction and run appropriate animation
  if (ax > 5000) {
    do_ONE_Step_lefttiltAnimation();
  }
}

// Define every byte to prevent error
void sinvader4c()
{
  int i = 0; // Start with i = 0
  lc.setRow(0, i, invader4c[i]); // Execute with i = 0
  i++; // Increment i to 1
  lc.setRow(0, i, invader4c[i]); // Execute with i = 1
  i++; // Increment i to 2
  lc.setRow(0, i, invader4c[i]); // Execute with i = 2
  i++; // Increment i to 3
  lc.setRow(0, i, invader4c[i]); // Execute with i = 3
  i++; // Increment i to 4
  lc.setRow(0, i, invader4c[i]); // Execute with i = 4
  i++; // Increment i to 5
  lc.setRow(0, i, invader4c[i]); // Execute with i = 5
  i++; // Increment i to 6
  lc.setRow(0, i, invader4c[i]); // Execute with i = 6
  i++; // Increment i to 7
  lc.setRow(0, i, invader4c[i]); // Execute with i = 7
}

void sinvader3c()
{
  int i = 0; // Start with i = 0
  lc.setRow(0, i, invader3c[i]); // Execute with i = 0
  i++; // Increment i to 1
  lc.setRow(0, i, invader3c[i]); // Execute with i = 1
  i++; // Increment i to 2
  lc.setRow(0, i, invader3c[i]); // Execute with i = 2
  i++; // Increment i to 3
  lc.setRow(0, i, invader3c[i]); // Execute with i = 3
  i++; // Increment i to 4
  lc.setRow(0, i, invader3c[i]); // Execute with i = 4
  i++; // Increment i to 5
  lc.setRow(0, i, invader3c[i]); // Execute with i = 5
  i++; // Increment i to 6
  lc.setRow(0, i, invader3c[i]); // Execute with i = 6
  i++; // Increment i to 7
  lc.setRow(0, i, invader3c[i]); // Execute with i = 7
}

void sinvader2c()
{
  int i = 0; // Start with i = 0
  lc.setRow(0, i, invader2c[i]); // Execute with i = 0
  i++; // Increment i to 1
  lc.setRow(0, i, invader2c[i]); // Execute with i = 1
  i++; // Increment i to 2
  lc.setRow(0, i, invader2c[i]); // Execute with i = 2
  i++; // Increment i to 3
  lc.setRow(0, i, invader2c[i]); // Execute with i = 3
  i++; // Increment i to 4
  lc.setRow(0, i, invader2c[i]); // Execute with i = 4
  i++; // Increment i to 5
  lc.setRow(0, i, invader2c[i]); // Execute with i = 5
  i++; // Increment i to 6
  lc.setRow(0, i, invader2c[i]); // Execute with i = 6
  i++; // Increment i to 7
  lc.setRow(0, i, invader2c[i]); // Execute with i = 7
}

void sinvader1c()
{
  int i = 0; // Start with i = 0
  lc.setRow(0, i, invader1c[i]); // Execute with i = 0
  i++; // Increment i to 1
  lc.setRow(0, i, invader1c[i]); // Execute with i = 1
  i++; // Increment i to 2
  lc.setRow(0, i, invader1c[i]); // Execute with i = 2
  i++; // Increment i to 3
  lc.setRow(0, i, invader1c[i]); // Execute with i = 3
  i++; // Increment i to 4
  lc.setRow(0, i, invader1c[i]); // Execute with i = 4
  i++; // Increment i to 5
  lc.setRow(0, i, invader1c[i]); // Execute with i = 5
  i++; // Increment i to 6
  lc.setRow(0, i, invader1c[i]); // Execute with i = 6
  i++; // Increment i to 7
  lc.setRow(0, i, invader1c[i]); // Execute with i = 7
}

void sinvader1d()
{
  int i = 0; // Start with i = 0
  lc.setRow(0, i, invader1d[i]); // Execute with i = 0
  i++; // Increment i to 1
  lc.setRow(0, i, invader1d[i]); // Execute with i = 1
  i++; // Increment i to 2
  lc.setRow(0, i, invader1d[i]); // Execute with i = 2
  i++; // Increment i to 3
  lc.setRow(0, i, invader1d[i]); // Execute with i = 3
  i++; // Increment i to 4
  lc.setRow(0, i, invader1d[i]); // Execute with i = 4
  i++; // Increment i to 5
  lc.setRow(0, i, invader1d[i]); // Execute with i = 5
  i++; // Increment i to 6
  lc.setRow(0, i, invader1d[i]); // Execute with i = 6
  i++; // Increment i to 7
  lc.setRow(0, i, invader1d[i]); // Execute with i = 7
}

void sinvader2d()
{
  int i = 0; // Start with i = 0
  lc.setRow(0, i, invader2d[i]); // Execute with i = 0
  i++; // Increment i to 1
  lc.setRow(0, i, invader2d[i]); // Execute with i = 1
  i++; // Increment i to 2
  lc.setRow(0, i, invader2d[i]); // Execute with i = 2
  i++; // Increment i to 3
  lc.setRow(0, i, invader2d[i]); // Execute with i = 3
  i++; // Increment i to 4
  lc.setRow(0, i, invader2d[i]); // Execute with i = 4
  i++; // Increment i to 5
  lc.setRow(0, i, invader2d[i]); // Execute with i = 5
  i++; // Increment i to 6
  lc.setRow(0, i, invader2d[i]); // Execute with i = 6
  i++; // Increment i to 7
  lc.setRow(0, i, invader2d[i]); // Execute with i = 7
}

void sinvader3d()
{
  int i = 0; // Start with i = 0
  lc.setRow(0, i, invader3d[i]); // Execute with i = 0
  i++; // Increment i to 1
  lc.setRow(0, i, invader3d[i]); // Execute with i = 1
  i++; // Increment i to 2
  lc.setRow(0, i, invader3d[i]); // Execute with i = 2
  i++; // Increment i to 3
  lc.setRow(0, i, invader3d[i]); // Execute with i = 3
  i++; // Increment i to 4
  lc.setRow(0, i, invader3d[i]); // Execute with i = 4
  i++; // Increment i to 5
  lc.setRow(0, i, invader3d[i]); // Execute with i = 5
  i++; // Increment i to 6
  lc.setRow(0, i, invader3d[i]); // Execute with i = 6
  i++; // Increment i to 7
  lc.setRow(0, i, invader3d[i]); // Execute with i = 7
}

void sinvader4d()
{
  int i = 0; // Start with i = 0
  lc.setRow(0, i, invader4d[i]); // Execute with i = 0
  i++; // Increment i to 1
  lc.setRow(0, i, invader4d[i]); // Execute with i = 1
  i++; // Increment i to 2
  lc.setRow(0, i, invader4d[i]); // Execute with i = 2
  i++; // Increment i to 3
  lc.setRow(0, i, invader4d[i]); // Execute with i = 3
  i++; // Increment i to 4
  lc.setRow(0, i, invader4d[i]); // Execute with i = 4
  i++; // Increment i to 5
  lc.setRow(0, i, invader4d[i]); // Execute with i = 5
  i++; // Increment i to 6
  lc.setRow(0, i, invader4d[i]); // Execute with i = 6
  i++; // Increment i to 7
  lc.setRow(0, i, invader4d[i]); // Execute with i = 7
}

In this way the i value will always get up by 1 before executing again, and this wont use the for-loop function

as of now this is the complete program but I haven't introduce here yet the new changes since it's not yet done

#include <Wire.h>
#include <MPU6050.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <LedControl.h>

#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 4
#define CS_PIN 53
#define DATA_PIN 51
#define CLK_PIN 52
#define HALL_SENSOR_PIN 2

MD_Parola myDisplay = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
MPU6050 mpu;

LedControl lc = LedControl(51, 52, 53, 4); // Pins: DIN, CLK, CS, # of Displays connected
volatile bool sensorTriggered = false; // Use volatile for interrupt safety

unsigned long lastAnimationUpdate = 0;
unsigned long scrollTimer = 0; // Timer for scrolling message delay
const unsigned long tiltAnimationDelay = 200; // Delay between tilt animation frames
const unsigned long scrollDelay = 20; // Delay for scrolling message

// Put values in arrays
byte invader1a[] =
    {
        B00010010, // First frame of invader #1
        B00100100,
        B01001001,
        B10010010,
        B10010010,
        B01001001,
        B00100100,
        B00010010};

byte invader1b[] =
    {
        B00000000, // Second frame of invader #1
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader2a[] =
    {
        B00010010, // First frame of invader #1
        B00100100,
        B01001001,
        B10010010,
        B10010010,
        B01001001,
        B00100100,
        B00010010};

byte invader2b[] =
    {
        B00000000, // Second frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader3a[] =
    {
        B00010010, // First frame of invader #1
        B00100100,
        B01001001,
        B10010010,
        B10010010,
        B01001001,
        B00100100,
        B00010010};

byte invader3b[] =
    {
        B00000000, // First frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader4a[] =
    {
        B00010010, // First frame of invader #1
        B00100100,
        B01001001,
        B10010010,
        B10010010,
        B01001001,
        B00100100,
        B00010010};

byte invader4b[] =
    {
        B00000000, // Second frame of invader #2
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

// Put values in arrays
byte invader1c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader1d[] =
    {
        B00000000, // Second frame of invader #1
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader2c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader2d[] =
    {
        B00000000, // Second frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

byte invader3c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader3d[] =
    {
        B00000000, // First frame of invader #2 (flipped horizontally)
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000
    };
        
byte invader4c[] =
    {
        B01001000, // First frame of invader #1
        B00100100,
        B10010010,
        B01001001,
        B01001001,
        B10010010,
        B00100100,
        B01001000};

byte invader4d[] =
    {
        B00000000, // Second frame of invader #2
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000,
        B00000000};

void setup() {
  Serial.begin(115200);
  Wire.begin();
  mpu.initialize();
  SPI.begin(); // Initialize SPI

  lc.shutdown(0, false); // Wake up displays
  lc.shutdown(1, false);
  lc.shutdown(2, false);
  lc.shutdown(3, false);
  lc.setIntensity(0, 15); // Set intensity levels
  lc.setIntensity(1, 15);
  lc.setIntensity(2, 15);
  lc.setIntensity(3, 15);
  lc.clearDisplay(0); // Clear Displays
  lc.clearDisplay(1);
  lc.clearDisplay(2);
  lc.clearDisplay(3);

  myDisplay.begin();
  myDisplay.setIntensity(8);
  myDisplay.displayClear();

  pinMode(HALL_SENSOR_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_PIN), sensorInterrupt, CHANGE); // Attach interrupt on CHANGE

  scrollTimer = millis(); // Initialize the scroll timer
}

void sensorInterrupt() {
  sensorTriggered = !digitalRead(HALL_SENSOR_PIN); // Read the sensor state in the interrupt
}

void displayAcceleration(int16_t ax, int16_t ay, int16_t az) {
  // Print the X, Y, and Z values to the serial monitor
  Serial.print("X: ");
  Serial.print(ax);
  Serial.print(" | Y: ");
  Serial.print(ay);
  Serial.print(" | Z: ");
  Serial.println(az);
}

void tiltRightAnimation_OneStep() {
  static int frameIndex = 0; // Static variable to keep track of frame index

  // Define the array of animation frames
  byte* frames[] = {invader1a, invader2a, invader3a, invader4a, invader1b, invader2b, invader3b, invader4b};

  // Display the current frame
  for (int i = 0; i < 8; i++) {
    lc.setRow(frameIndex / 2, i, frames[frameIndex][i]);
  }

  // Increment frame index
  frameIndex++;
  if (frameIndex >= 8) {
    frameIndex = 0; // Reset frame index if it exceeds the number of frames
  }
}

void tiltLeftAnimation_OneStep() {
  static int frameIndex = 0; // Static variable to keep track of frame index

  // Define the array of animation frames
  byte* frames[] = {invader4c, invader3c, invader2c, invader1c, invader4d, invader3d, invader2d, invader1d};

  // Display the current frame
  for (int i = 0; i < 8; i++) {
    lc.setRow(frameIndex / 2, i, frames[frameIndex][i]);
  }

  // Increment frame index
  frameIndex++;
  if (frameIndex >= 8) {
    frameIndex = 0; // Reset frame index if it exceeds the number of frames
  }
}

void displayStopMessage() {
  unsigned long currentMillis = millis();

  if (currentMillis - scrollTimer >= scrollDelay) {
    scrollTimer = currentMillis; // Update the scroll timer

    if (myDisplay.displayAnimate()) {
      myDisplay.displayReset();
      myDisplay.setTextAlignment(PA_CENTER);
      myDisplay.setSpeed(30);
      myDisplay.displayScroll("STOP STOP STOP STOP ", PA_CENTER, PA_SCROLL_LEFT, 20);
    }
  }
}

void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);
  displayAcceleration(ax, ay, az);

  // Check for strong magnetic field
  bool strongMagneticField = sensorTriggered;
  if (strongMagneticField) {
    displayStopMessage();
    sensorTriggered = false; // Reset the flag after displaying the message
  }

  // Execute tilt animation if strong magnetic field is NOT detected
  if (!strongMagneticField) {
    unsigned long currentMillis = millis();
    if (currentMillis - lastAnimationUpdate >= tiltAnimationDelay) {
      lastAnimationUpdate = currentMillis;

      if (ax > 4000) {
        tiltRightAnimation_OneStep();
      } else if (ax < -4000) {
        tiltLeftAnimation_OneStep();
      }
    }
  }
}

I'll update you after trying your recommendation, thank you very much again

Edit:

In the current complete code the if (strongmagnetcfield) is the one that makes the STOP display at the MAX7219 and the structure of the void loop is different from what you posted than in the complete code so I dont have any clue how to start, Its so late here so im about to go to sleep let me get back to you tomorrow morning thank you and sorry

This is not in your program.

Update your "displayAcceleration()" function to show the "sensorTriggered" state (see post 61).

I think this is what you meant. Correct me if I'm wrong

void displayAcceleration(int16_t ax, int16_t ay, int16_t az) {
  // Print the X, Y, and Z values to the serial monitor
  Serial.print("X: ");
  Serial.print(ax);
  Serial.print(" | Y: ");
  Serial.print(ay);
  Serial.print(" | Z: ");
  Serial.print(az);
  Serial.print(" | H: ");
  Serial.println(sensorTriggered);
}

Also here's the loop I also put comments to make sure that I understand the code correctly

void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);
  displayAcceleration(ax, ay, az);

  // Check for strong magnetic field
  bool strongMagneticField = sensorTriggered; // Base on this sensorTriggered function will still apply even if it is named strongMagneticField
  if (strongMagneticField) { // If sensorTriggered is display at the serial monitor, it will display the stop message
    displayStopMessage();
    sensorTriggered = false; // Reset the flag after displaying the message
  }

this part of the loop function is not complete theres still bottom part of this function that I do not included.