Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
8.3 kB
2
Indexable
Never
#include "LedControl.h"
#include <Wire.h>
#include <MPU6050.h>

//Begin STOP STOP Hall Effect 
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define MAX_DEVICES 4
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define CLK_PIN 52
#define DATA_PIN 51
#define CS_PIN 53
int hallSensorPin = 2;
volatile bool sensorTriggered = false;
MD_Parola myDisplay = MD_Parola(MD_MAX72XX::FC16_HW, CS_PIN, MAX_DEVICES);

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

LedControl lc = LedControl(51, 52, 53, 4); // Pins: DIN, CLK, CS, # of Displays connected
MPU6050 mpu;

unsigned long delayTime = 200; // Delay between Frames

// 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()
{
  lc.shutdown(0, false); // Wake up displays
  lc.shutdown(1, false);
  lc.shutdown(2, false);
  lc.shutdown(3, false);
  lc.setIntensity(0, 5); // Set intensity levels
  lc.setIntensity(1, 5);
  lc.setIntensity(2, 5);
  lc.setIntensity(3, 5);
  lc.clearDisplay(0); // Clear Displays
  lc.clearDisplay(1);
  lc.clearDisplay(2);
  lc.clearDisplay(3);

  Wire.begin();
  mpu.initialize();

  // Begin STOP STOP Hall Effect Setup
  myDisplay.begin();
  myDisplay.setIntensity(8);
  myDisplay.displayClear();
  pinMode(hallSensorPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(hallSensorPin), sensorInterrupt, FALLING); // Attach interrupt on CHANGE
  // End STOP STOP Hall Effect Setup
}

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

  // Detect tilt direction
  if (ax > 10000) // Tilt to the right
  {
    // Put #1 frame on both Displays
    sinvader1a();
    delay(delayTime);
    sinvader2a();
    delay(delayTime);

    // Put #2 frame on both Displays
    sinvader3a();
    delay(delayTime);
    sinvader4a();
    delay(delayTime);

    // Put #3 frame on both Displays
    sinvader1b();
    delay(delayTime);
    sinvader2b();
    delay(delayTime);

    // Put #4 frame on both Displays
    sinvader3b();
    delay(delayTime);
    sinvader4b();
    delay(delayTime);
  }
  else if (ax < -10000) // Tilt to the left
  {
    // Put #1 frame on both Displays
    sinvader4c();
    delay(delayTime);
    sinvader3c();
    delay(delayTime);

    // Put #2 frame on both Displays
    sinvader2c();
    delay(delayTime);
    sinvader1c();
    delay(delayTime);

    // Put #3 frame on both Displays
    sinvader4d();
    delay(delayTime);
    sinvader3d();
    delay(delayTime);

    // Put #4 frame on both Displays
    sinvader2d();
    delay(delayTime);
    sinvader1d();
    delay(delayTime);
  }

// Begin STOP STOP Hall Effect Loop
if (sensorTriggered) {
    myDisplay.displayScroll("STOP STOP STOP STOP ", PA_CENTER, PA_SCROLL_RIGHT, 30);
    sensorTriggered = false;
  }
  if (myDisplay.displayAnimate()) {
    myDisplay.displayClear();
  }
// End STOP STOP Hall Effect Loop
}

// Take values in Arrays and Display them
void sinvader1a()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0, i, invader1a[i]);
  }
}

void sinvader1b()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0, i, invader1b[i]);
  }
}

void sinvader2a()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(1, i, invader2a[i]);
  }
}

void sinvader2b()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(1, i, invader2b[i]);
  }
}

void sinvader3a()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(2, i, invader3a[i]);
  }
}

void sinvader3b()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(2, i, invader3b[i]);
  }
}

void sinvader4a()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(3, i, invader4a[i]);
  }
}

void sinvader4b()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(3, i, invader4b[i]);
  }
}

void sinvader4c()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(3, i, invader4c[i]);
  }
}

void sinvader3c()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(2, i, invader3c[i]);
  }
}

void sinvader2c()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(1, i, invader2c[i]);
  }
}

void sinvader1c()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0, i, invader1c[i]);
  }
}

void sinvader4d()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(3, i, invader4d[i]);
  }
}

void sinvader3d()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(2, i, invader3d[i]);
  }
}

void sinvader2d()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(1, i, invader2d[i]);
  }
}

void sinvader1d()
{
  for (int i = 0; i < 8; i++)
  {
    lc.setRow(0, i, invader1d[i]);
  }
}
Leave a Comment