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