top of page

Safari browser sometimes has issues displaying images...

I.e: *you have to click on the images to see them...

For a better browsing experience on Brainy-Bits

Please use Chrome, Edge or Firefox browser.

Writer's pictureBrainy-Bits

Halloween Scary mask using Arduino and MAX7219 LED Dot Matrix



OVERVIEW


Since Halloween is near we thought we would do a fun project.


Using an Halloween mask and some LED Dot Matrix to simulate eyes, we will make a candy bowl protector.


With the help of a proximity sensor we will detect when someone tries to take some candy and sound a buzzer and change the Dot matrix eyes to an angry look.


The 8X8 matrix will simulate the eyes moving around randomly until the proximity sensor is triggered.


We also use a WS2812 LED stick to give the mask mouth a yellow color that will also change to Red when triggered.

 

PARTS USED


MAX7219 Display

Arduino UNO


Obstacle Avoidance IR Sensor


These are Amazon affiliate links...

They don't cost you anything and it helps me keep the lights on

if you buy something on Amazon. Thank you!

 

CONNECTIONS




The MAX7219 LED matrix are connected to:

Pin 10-CS

Pin 11-CLK

Pin 12-DIN


Pin 2 is connected to the IR Sensor


Pin 13 is connected to the WS2812 LED stick


The GND pin of the buzzer is connected to Pin 5


The + pin of the buzzer is connected to 3.3V of the UNO


We are using a mini breadboard to split the Ground and Voltage to the various modules.


 

THE CODE


Our Sketch will make use of the “LedControl” Library to communicate with the MAX7219 modules and the “FastLED” library to control the WS2812 LED stick.

The code below assumes that we have 2 Dot Matrix connected together in cascade, but you can modify it easily if you have more or less of them connected.


We select the module we want to write to by using a number based on where it is in the chain, starting from 0 for the first one, 1 for the second, and so on…


As always for more information about the tutorial and explanation of the code please watch our tutorial video.


/* Start of Code */

#include "LedControl.h" // To control MAX7219 LED Dot Matrix

#include "FastLED.h" // To control WS2812 LEDs
#define NUM_LEDS 8 // Number of LEDs on stick
#define DATA_PIN 13 // DIN pin of stick connected to Pin 13 on Arduino
CRGB leds[NUM_LEDS]; // LED Array

#define irpin 2 // IR sensor pin
#define Buzzer 5 // Buzzer Pin

LedControl lc=LedControl(12,11,10,2); // Pins: DIN,CLK,CS, # of Matrix Display connected

unsigned long delayTime=1000; // Default Delay between Frames

// Put values in arrays
byte LeftEyeOpen[] =
{
B01111110, // left eyes open
B10000001,
B10000001,
B10011001,
B10011001,
B10000001,
B10000001,
B01111110
};

byte LeftEyeBlink[] =
{
B00000000, // left eye blink
B00000000,
B00000000,
B11111111,
B11111111,
B00000000,
B00000000,
B00000000
};

byte LeftEyeLookLeft[] =
{
B01111110, // left eyes look left
B10000001,
B10000001,
B10000111,
B10000111,
B10000001,
B10000001,
B01111110
};

byte LeftEyeLookRight[] =
{
B01111110, // left eyes look right
B10000001,
B10000001,
B11100001,
B11100001,
B10000001,
B10000001,
B01111110
};

byte RightEyeOpen[] =
{
B01111110, // right eye open
B10000001,
B10000001,
B10011001,
B10011001,
B10000001,
B10000001,
B01111110
};

byte RightEyeBlink[] =
{
B00000000, // right eye blink
B00000000,
B00000000,
B11111111,
B11111111,
B00000000,
B00000000,
B00000000
};

byte RightEyeLookLeft[] =
{
B01111110, // right eyes look left
B10000001,
B10000001,
B10000111,
B10000111,
B10000001,
B10000001,
B01111110
};

byte RightEyeLookRight[] =
{
B01111110, // right eyes look right
B10000001,
B10000001,
B11100001,
B11100001,
B10000001,
B10000001,
B01111110
};

byte RightEyeAngry[] =
{
B11111000, // right eye Angry
B10011100,
B10000111,
B10000011,
B10011001,
B10011001,
B11000001,
B00111111
};

byte LeftEyeAngry[] =
{
B00011111, // left eye Angry
B00111001,
B11100001,
B10000001,
B10011001,
B10011001,
B10000011,
B11111100
};

void setup()
{
lc.shutdown(0,false); // Wake up Matrix displays
lc.shutdown(1,false);

lc.setIntensity(0,5); // Set intensity levels
lc.setIntensity(1,5);

lc.clearDisplay(0); // Clear Displays
lc.clearDisplay(1);

FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS); // Init of WS2812 LED stick

pinMode(Buzzer, OUTPUT);
digitalWrite(Buzzer, HIGH);
pinMode (irpin, INPUT);
}

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

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

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

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

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

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

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

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

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

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

void loop()
{ 
delay(150);

for(int led = 0; led < NUM_LEDS; led++) { // Set WS2812 LEDs to Orange
leds[led] = CRGB::Orange;
}

FastLED.setBrightness(random(25,100)); // Set brightness to Random value
FastLED.show();

if (digitalRead (irpin) == LOW) // IR detected Show Angry Eyes
{
sLeftEyeAngry();
sRightEyeAngry();

for(int led = 0; led < NUM_LEDS; led++) { // Set WS2812 LEDs color to Red
leds[led] = CRGB::Red;
}
FastLED.setBrightness(100); // Full Brightness
FastLED.show();

digitalWrite(Buzzer, LOW); // Sound Buzzer

while (digitalRead (irpin) == LOW) { // wait until IR is HIGH again
}
digitalWrite(Buzzer, HIGH); // Turn off Buzzer
delay(100); // Wait for IR to reset
}

// Put #1 frame on both Display
sLeftEyeOpen();
sRightEyeOpen();
delay(delayTime);

int displayFrame=random(1, 6); // Randomize Eyes

switch (displayFrame) {

case 2: // Put #2 frame on both Display
sLeftEyeBlink();
sRightEyeBlink();
delay(delayTime-650);
break;

case 3: // Put #3 frame on both Display
sLeftEyeLookLeft();
sRightEyeLookLeft();
delay(delayTime-500);
break;

case 4: // Put #4 frame on both Display
sLeftEyeLookRight();
sRightEyeLookRight();
delay(delayTime-500);
break;
}
}

/* End of Code */


 

TUTORIAL VIDEO




 

DOWNLOAD


Copy the above Sketch code in your Arduino IDE software to program your Arduino.


Used libraries:

You can download the FastLED library here


Download the LedControl library here:  LedControl

825 views0 comments

Recent Posts

See All

Comments


All my content is and will always be Free.

If you feel that my Videos / Tutorials are helping, and you would like to contribute...

 You can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

bottom of page