Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.1 kB
7
Indexable
Never
#include <Wire.h>
#include <MPU6050.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CS_PIN 53
#define DATA_PIN 51
#define CLK_PIN 52
#define HALL_SENSOR_PIN 3

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

unsigned int delayTime = 100;
int pinSTOP = 3;
bool STOPdisplayWasNotCleared;
bool leftRightDisplayWasNotCleared;

void report(int16_t ax) {
  const uint32_t interval = 500;
  static uint32_t last = 0;
  uint32_t now = millis();
  if (now - last >= interval) {
    last += interval; // shows that acceleration animation takes up to 7ms
    //last = now;
    Serial.print("ms:");
    Serial.print(now);
    Serial.print(" Hall:");
    Serial.print(digitalRead(pinSTOP));
    Serial.print(" ");
    // Print the X, Y, and Z values to the serial monitor
    Serial.print("X: ");
    Serial.print(ax);
    Serial.println();
  }
}

void tiltRightAnimation_OneStep();
void tiltLeftAnimation_OneStep();
void displayStopMessage();

void setup() {
  Serial.begin(115200);
  pinMode(pinSTOP, INPUT_PULLUP);
  myDisplay.begin();
  myDisplay.setIntensity(8);
  myDisplay.displayClear();
}

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

    if (ax > 4000 && !STOPdisplayWasNotCleared) {
        tiltRightAnimation_OneStep();
        leftRightDisplayWasNotCleared = 1; // enable display clearing
    } else if (ax < -4000 && !STOPdisplayWasNotCleared) {
        tiltLeftAnimation_OneStep();
        leftRightDisplayWasNotCleared = 1; // enable display clearing
    } else if (leftRightDisplayWasNotCleared && !STOPdisplayWasNotCleared) {
        myDisplay.displayClear();
        leftRightDisplayWasNotCleared = 0; // disable display clearing
    }

    bool hallEffectDetected = !digitalRead(HALL_SENSOR_PIN); // Invert the digitalRead() result
    if (hallEffectDetected) {
        displayStopMessage();
        STOPdisplayWasNotCleared = 1; // enable display clearing
    } else {
        if (STOPdisplayWasNotCleared) {
            myDisplay.displayClear();
            STOPdisplayWasNotCleared = 0; // disable display clearing, not wasting CPU time
        }
    }

    report(ax); // Pass the value of ax to the report() function
}

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

void tiltRightAnimation_OneStep() {
  if (myDisplay.displayAnimate()) {
    myDisplay.displayReset();
    myDisplay.setTextAlignment(PA_CENTER);
    myDisplay.setSpeed(30);
    myDisplay.displayScroll(" <<<< ", PA_CENTER, PA_FADE, 20);
  }
}

void tiltLeftAnimation_OneStep() {
  if (myDisplay.displayAnimate()) {
    myDisplay.displayReset();
    myDisplay.setTextAlignment(PA_CENTER);
    myDisplay.setSpeed(30);
    myDisplay.displayScroll(" >>>> ", PA_CENTER, PA_FADE, 20);
  }
}
Leave a Comment