Untitled

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

LedControl lc = LedControl(11, 13, 10, 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();
}

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);
  }
}

// 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