cris
Published © GPL3+

Hey, smile!🙂 [with animations]

I made a small project with LEDs and a MAX7219 matrix.

IntermediateProtipOver 1 day80
Hey, smile!🙂 [with animations]

Things used in this project

Hardware components

MAXREFDES99# MAX7219 Display Driver Shield
Maxim Integrated MAXREFDES99# MAX7219 Display Driver Shield
Example
×1
ATMega8AU
Example
×1
IR Sensor Moduke
Example
×1

Software apps and online services

KiCad
KiCad
Tinkercad
Autodesk Tinkercad
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Multitool, Screwdriver
Multitool, Screwdriver
3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

Enclosure

Sketchfab still processing.

Code

Code

C/C++
// include library
#include "MaxMatrix.h"

int DIN = 11;   // DIN pin of MAX7219 module
int CLK = 13;   // CLK pin of MAX7219 module
int CS = 10;    // CS pin of MAX7219 module
int maxInUse = 1;

MaxMatrix m(DIN, CS, CLK, maxInUse);

int buttonPin = 12;
int toggleState = 1;
int lastButtonState = 1;
long unsigned int lastPress;
int debounceTime = 20;

// Define variables for LED pins
int green = 9; // green
int yellow = 8; // yellow
int red = 7; // red

// Define variables for current time and previous time
unsigned long currentTime;
unsigned long previousLedTime = 0;
unsigned long previousMatrixTime = 0;
// Define variables for LED states
boolean greenState = false;
boolean yellowState = false;
boolean redState = false;
int matrixState = LOW;

// smile_1
char smile_1[] = {   8, 8,
                     0b00000000,
                     0b00000100,
                     0b01100010,
                     0b00000010,
                     0b00000010,
                     0b01100010,
                     0b00000100,
                     0b00000000
                 };
// smile_2
char smile_2[] = {8, 8,
                  0b00111100,
                  0b01000010,
                  0b10101001,
                  0b10000101,
                  0b10000101,
                  0b10101001,
                  0b01000010,
                  0b00111100
                 };
// hi_1
char hi_1[] = {8, 8,
               0b00010000,
               0b00001001,
               0b11101011,
               0b11111100,
               0b11111100,
               0b11101011,
               0b00001001,
               0b00001000
              };
// hi_2
char hi_2[] = {8, 8,
               0b00010000,
               0b00001001,
               0b11101011,
               0b11111100,
               0b11111100,
               0b11101011,
               0b00001001,
               0b00010000
              };

// nothing
char nothing[] = {8, 8,
                  B00000000,
                  B00000000,
                  B00000000,
                  B00000000,
                  B00000000,
                  B00000000,
                  B00000000,
                  B00000000
                 };

void setup() {
  m.init(); // MAX7219 initialization
  m.setIntensity(1); // initial led matrix intensity, 0-15
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
  // Set LED pins as output
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(red, OUTPUT);
}

void loop() {

  unsigned long buttonMillis = millis();
  int buttonState = digitalRead(buttonPin);



  if ((buttonMillis - lastPress) > debounceTime)  //if the time between the last buttonChange is greater than the debounceTime
  {
    lastPress = buttonMillis;   //update lastPress
    if (buttonState == 0 && lastButtonState == 1)   //if button is pressed and was released last change
    {
      toggleState = ! toggleState;                //toggle the state
      lastButtonState = 0;    //record the lastButtonState
    }

    if (buttonState == 1 && lastButtonState == 0)   //if button is not pressed, and was pressed last change
    {
      lastButtonState = 1;    //record the lastButtonState
    }
  }

  if (toggleState == 0)
  {
    // Get current time
    currentTime = millis();
    //m.writeSprite(0, 0, smile_1);
    // Check if it's time to turn on LED 1
    if (currentTime - previousLedTime >= 2000 && !greenState) {
      digitalWrite(yellow, LOW); // Turn off LED 2
      digitalWrite(red, LOW);
      digitalWrite(green, HIGH); // Turn on LED 1
      greenState = true; // Update LED 1 state
      m.writeSprite(0, 0, smile_1);
      previousLedTime = currentTime; // Update previous time
    }

    // Check if it's time to turn off LED 1 and turn on LED 2
    if (currentTime - previousLedTime >= 7000 && greenState && !yellowState) {
      digitalWrite(red, LOW);
      digitalWrite(green, LOW); // Turn off LED 1
      digitalWrite(yellow, HIGH); // Turn on LED 2
      yellowState = true; // Update LED 2 state
      m.writeSprite(0, 0, smile_2);
      previousLedTime = currentTime; // Update previous time
    }

    // Check if it's time to turn off LED 2 and turn on LED 3
    if (currentTime - previousLedTime >= 2000 && yellowState && !redState) {
      digitalWrite(yellow, LOW); // Turn off LED 2
      digitalWrite(red, HIGH); // Turn on LED 3
      redState = true; // Update LED 3 state
      m.writeSprite(0, 0, smile_2);
      previousLedTime = currentTime; // Update previous time
    }

    // Check if it's time to turn off LED 3 and start over
    if (currentTime - previousLedTime >= 4000 && redState) {
      digitalWrite(red, LOW); // Turn off LED 3
      greenState = false; // Reset LED 1 state
      yellowState = false; // Reset LED 2 state
      redState = false; // Reset LED 3 state
      m.writeSprite(0, 0, nothing);
      previousLedTime = currentTime; // Update previous time
      toggleState = 1;
    }

  } // if toggleState == 0

  if (toggleState == 1)
  {
    //digitalWrite(red, HIGH);
    Serial.write("toggleState == 1 \n");

    unsigned long ledTime = millis();
    unsigned long matrixTime = millis();

    if (ledTime - previousLedTime < 1000)
    {
      digitalWrite(yellow, HIGH);
      //previousLedTime = ledTime;
    }
    else if ((ledTime - previousLedTime > 1000) && (ledTime - previousLedTime < 1500))
    {
      digitalWrite(yellow, LOW);
      //previousLedTime = ledTime;
    }
    else
    {
      previousLedTime = ledTime;
    }

    // ... add Maxtrix
    if (matrixTime - previousMatrixTime >= 1000) {
      // save the last time you blinked the LED
      previousMatrixTime = matrixTime;

      // if the LED is off turn it on and vice-versa:
      if (matrixState == LOW)
      {
        matrixState = HIGH;
        m.writeSprite(0, 0, hi_1);
        Serial.println("Matrix is 1!");

      }
      else
      {
        matrixState = LOW;
        m.writeSprite(0, 0, hi_2);
        Serial.println("Matrix is 2!");
      }
      
    } // interval

  }

} // void loop

Credits

cris
5 projects • 0 followers
I started with an Arduino board now I try to build homemade circuits with components mostly salvaged from scrap boards.
Contact

Comments

Please log in or sign up to comment.