Need help at coding

I tried implementing the suggestion you gave, you can check here the changes and the problems I'm encountering right now

Wokwi-Ram

The STOP STOP is constantly displaying at the MAX7219 and even after switching the slide switch it wont stop sending the STOP STOP, and because of this the letter "S" wont stop sending at the serial monitor cause it constantly display the STOP message

What suggestions did you follow and what's happening?

The switch doesn't have a ground connected, so it won't be to simulate the hall sensor switching on and off, and I don't see where it prints an "S". And the acceleration printing is happening too fast so see anything.

I connected the switch and saw this:

===

If it isn't working correctly I'd suggest slowing down or commenting out the acceleration printing and adding some debug signals like these to probe where things are happening as expected or not.

  • Serial.print("L")
  • Serial.print("R")

can you try at this link, the STOP is displaying for me but the STOP STOP is constanly displaying after clicking the slide switch once... After clicking it again, the STOP STOP literally stop scrolling making it pause at the displaying the whole word

wokwi-Ram

In this picture the switch is turned off, but the STOP STOP at the MAX7219 only stopped and doesn't clear the display.

On the other hand, on this one the switch was on and start displaying the STOP STOP constandly without it stopping also the S is printing in the serial monitor.

OK. The Ss and the Ms are showing you how fast the code executes the branches where their print statements are.

What is supposed to happen to clear the display? Can you put a Serial.print("C"); where that is supposed to happen?

.... and if you adjust the acceleration?

I do not understand how this is meant to work from start to finish. All I have been able to do is make things happen. Can it be summarized?

the whole project contains 3 hardware connected to arduino mega 2560, the MPU6050 for the gyroscope to measure whether it is tilted to the right or to the left, the other one is Hall Effect, it is to send STOP STOP whenever it detect a high magnetic field, in our case it is connected in the handle bar of the bicycle, and the magnet is at the breaks, so in that case whenever the biker presses its breaks the magnet will come at close at the Hall effect making it receiving a high magnetic field displaying STOP STOP at our 3rd hardware which is the MAX7219 this is the one that displays the left and right turn animation received at the MPU6050 and also the STOP STOP from the Hall effect

To clear the display the magnet at the Hall effect needs to be falling, example I put the magnet close at the Hall effect it will send STOP STOP at the MAX7219 and when I removed the magnet the MAX7219 will finished sending the STOP STOP then clear the display, What do you mean by

can you please enlighten me

If you click on the MPU6050, you get a set of sliders where you can adjust the Ax away from 0 and past +/-4000:

I am confused about the differences between what you want to have happen and what you've coded to have happen.

Where is/should be the clearing code logic in this snippet:

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

  // Check the state of the slide switch
  bool stopCondition = digitalRead(HALL_SENSOR_PIN) == HIGH; // Adjust based on switch connection

  if (stopCondition) {
    displayStopMessage();
  } else {
    Serial.print("M"); // Print "M" to indicate the magnetic field is being checked
  }

  // Execute tilt animation if stop condition is NOT met
  if (!stopCondition) {
    unsigned long currentMillis = millis();
    if (currentMillis - lastAnimationUpdate >= tiltAnimationDelay) {
      lastAnimationUpdate = currentMillis;

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

If it's not there, name and write up a placeholder function per Robin2's tutorial (#81) and figure out where it belongs in your logic. Maybe something like:

void clearDisplay(void){Serial.print("c")};

I now understand. This is an automated, bicycle, motion detection signal. If the rider banks left, signal LEFT, if the rider banks right, signal RIGHT, if the rider applies brakes, signal STOP. Tilting LEFT and RIGHT will be sensed by the MPU and STOP by the Hall sensor near the brakes.

I see nothing that ties the Hall sensor on Pin 2 to the STOP message. Pin 2 might be FALLING and giving "sensorTriggered" a value, but that is where the Hall sensor information stops. I see the STOP message is triggered only by scrollTimer and scrollDelay.

Do you know the MPU can register acceleration in the negative direction (deceleration/stopping)?

Fun Fact: When inline wheeled vehicles turn they actually "steer" in the opposite direction before falling in the intended direction, experiencing centripetal acceleration and the desired turn.

Another fun fact: During the turn when banking, "down" might be aligned with the bicycle frame (or non-inertial sensor Z-axis) rather than as a signigicant tilt angle an outside, inertial observer might view.

Yes... the complexity of 3D turning. I do not know the math behind it, but have been looking at examples and theory.

Do you have any clue why is it constantly clearing the display, I'm trying to fix it but I'm having a trouble fixing it, could you lend me a hand

Wokwi-Ram

to be honest, I didn't know that it can measure that

When is it clearing the display?

I would think you might need to add something to clear the display in here:

  // Execute tilt animation if stop condition is NOT met
  if (!stopCondition) {
    unsigned long currentMillis = millis();
    if (currentMillis - lastAnimationUpdate >= tiltAnimationDelay) {
      lastAnimationUpdate = currentMillis;
      Serial.print("M"); // Print "M" to indicate the magnetic field is being checked
      if (ax > 4000) {
        tiltRightAnimation_OneStep();
      } else if (ax < -4000) {
        tiltLeftAnimation_OneStep();
      }
    }
  }

(I moved the M down inside this rate-limited code.)

I tried putting clear display but still its the same output as the same one

#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 clearDisplay() {
  // Send a signal to clear the display
  Serial.print("C");

  // Clear the display
  myDisplay.displayClear();
}

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 at a rate of once per 500 milliseconds
  static uint32_t lastPrintMs = 0;
  if (millis() - lastPrintMs >= 500) {
    lastPrintMs = millis();
    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() {
  Serial.print("S"); // Print "S" to indicate the stop message is displayed
  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);

  // Clear the display
  clearDisplay();

  // Check the state of the slide switch
  bool stopCondition = digitalRead(HALL_SENSOR_PIN) == HIGH; // Adjust based on switch connection

  if (stopCondition) {
    displayStopMessage();
  } else {
    // Execute tilt animation if stop condition is NOT met
    if (!stopCondition) {
      unsigned long currentMillis = millis();
      if (currentMillis - lastAnimationUpdate >= tiltAnimationDelay) {
        lastAnimationUpdate = currentMillis;
        Serial.print("M"); // Print "M" to indicate the magnetic field is being checked
        if (ax > 4000) {
          tiltRightAnimation_OneStep();
        } else if (ax < -4000) {
          tiltLeftAnimation_OneStep();
        }
      }
    }
  }
  clearDisplay();
}



As you can see the S is to see that the STOP STOP is displaying and the C is for clearing the display but both of them are printing in the serial monitor means that while the STOP STOP is displaying it also clears the display

So if stopCondition is true, you want to displayStopMessage and not clearDisplay?

Then clearDisplay belongs somewhere in the else part.

yeah thats right or else it will not display anything and will only clear the display over time

This is the one that I do not know :sweat_smile:

Both "S" and "C" should only be called once, when needed.

If you have a "STOP" condition, set a STOPFLAG. Only when you have a STOPFLAG, display "STOP" and cler the "STOPFLAG". Only display STOP when you have a STOP condition AND when STOPFLAG is set.

Same with CLEAR.

can you lend me a hand? I'm really having a hard time fixing it I don't even know what I'm doing :sweat_smile:

You should at least post your actual complete code