Untitled

 avatar
unknown
plain_text
a year ago
8.8 kB
5
Indexable
#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 stopMessageTimer = 0; // Timer for displaying STOP message
bool displayStop = false; // Flag to indicate whether to display STOP message
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.print(az);
  Serial.print(" | H: ");
  Serial.println(sensorTriggered);
}

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; // 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
    if (!displayStop) {
      stopMessageTimer = millis(); // Start the timer
      displayStop = true; // Set the flag
      displayStopMessage(); // Display STOP message
    }
    sensorTriggered = false; // Reset the flag after displaying the message
  } else {
    myDisplay.displayReset();
  }

  // Check if the timer has expired and clear the flag
  if (displayStop && millis() - stopMessageTimer >= stopMessageDuration) {
    displayStop = false; // Clear the flag
  }

  // 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();
      }
    }
  }
}
Editor is loading...
Leave a Comment