Elegoo Official
Published

Visualized Hand Washing Timer

A Visualized Hand Washing Timer to ensure that you wash your hands long enough to achieve disinfection.

BeginnerFull instructions provided1 hour365
Visualized Hand Washing Timer

Things used in this project

Hardware components

Elegoo Arduino Nano V3+
×1
Elegoo MAX7219 Module
×2
Elegoo 9V Battery
×1
Elegoo Ultrasonic Sensor
×1
Slide Switch
×1
Wire
×1

Story

Read more

Schematics

Visualized Hand Washing Timer

Code

Visualized Hand Washing Timer

Arduino
#include "LedControl.h"
#include "SR04.h"


#define TRIG_PIN 11
#define ECHO_PIN 10


SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
#define CARTIME 200
//DIN -9
//CS  -8
//CLk -7
LedControl lc = LedControl(9, 7, 8, 2);
byte Data2[] =
{ B00000000,
  B00000000,
  B00000000,
  B11111111,
  B00000000,
  B00000000,
  B00000000,
  B11111111,
};


byte tmp[8];
byte tmp2[8];
byte Data3[] =
{ B00000111,
  B00000111,
  B00000101,
  B11111111,
  B00000000,
  B00000000,
  B00000000,
  B11111111,
};


byte Data4[] =
{
  B00000000,
  B00000000,
  B00000000,
  B11111111,
  B11100000,
  B11100000,
  B10100000,
  B11111111,
};




int data[20][8] = {
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {0, 0, 0, 0, 0, 0, 0, 0},
  {1, 1, 1, 1, 1, 1, 1, 1}
};




void setup() {
  Serial.begin(9600);
  MAX7219_Init();
  lc.setIntensity(0, 1);
  lc.setIntensity(1, 1);
}


void loop() {


  if (sr04.Distance() <= 8 && sr04.Distance() >= 1) {
    while (data[3][1] != 1 && data[3][6] != 1) {
      int n;
      MAX7219_Display();
      MAX7219_Display_drop();
      delay(150);
      n = random(0, 2);
      if (n == 1)
        data[0][3] = 1;
      if (n == 0)
        data[0][4] = 1;
    }
    delay(150);
    lc.clearDisplay(0);
    lc.clearDisplay(1);
    MAX7219_Display_Car();
    MAX7219_Display_clear();
  }


}


void MAX7219_Display_clear() {
  for (int i = 0; i < 19; i++)
    for (int j = 0; j < 9; j++)
    {
      data[i][j] = 0;
    }
  for (int j = 0; j < 9; j++)
  {
    data[19][1] = 0;
  }
  MAX7219_Display();
}


void MAX7219_Display_Car()
{


  for (int j = 0; j < 8; j++)
  {
    lc.setRow(0, j, Data2[j]);
  }
  delay(CARTIME);


  memcpy(tmp, Data3, sizeof(Data3));
  for (int i = 0; i < 8; i++)
  {
    for (int j = 0; j < 8; j++)
    {
      lc.setRow(0, j, tmp[j]);
    }
    for (int i = 0; i < 8; i++)
    {
      lc.setRow(1, i, Data2[i]);
    }
    tmp[0] <<= 1;
    tmp[1] <<= 1;
    tmp[2] <<= 1;
    delay(CARTIME);
  }


  memcpy(tmp2, Data4, sizeof(Data4));
  for (int i = 0; i < 8; i++)
  {
    for (int j = 0; j < 8; j++)
    {
      lc.setRow(0, j, tmp2[j]);
    }
    tmp2[4] >>= 1;
    tmp2[5] >>= 1;
    tmp2[6] >>= 1;
    delay(CARTIME);
  }


  memcpy(tmp, Data3, sizeof(Data3));
  for (int i = 0; i < 8; i++)
  {
    for (int j = 0; j < 8; j++)
    {
      lc.setRow(1, j, tmp[j]);
    }
    for (int i = 0; i < 8; i++)
    {
      lc.setRow(0, i, Data2[i]);
    }
    tmp[0] <<= 1;
    tmp[1] <<= 1;
    tmp[2] <<= 1;
    delay(CARTIME);
  }


  memcpy(tmp2, Data4, sizeof(Data4));
  for (int i = 0; i < 8; i++)
  {
    for (int j = 0; j < 8; j++)
    {
      lc.setRow(1, j, tmp2[j]);
    }
    tmp2[4] >>= 1;
    tmp2[5] >>= 1;
    tmp2[6] >>= 1;
    delay(CARTIME);
  }
}
void MAX7219_Display_drop() {


  for (int i = 18; i > 0; i--) {


    for (int j = 0; j < 8; j++) {
      if (data[i][j] != 0 && data[i + 1][j] != 0 && data[i + 2][j] != 0) {
        if (j <= 3 && data[i + 1][j - 1] == 0) {
          Switch(&data[i][j], &data[i + 1][j - 1]);
        }
        if (j >= 4 && data[i + 1][j + 1] == 0) {
          Switch(&data[i][j], &data[i + 1][j + 1]);
        }
      }
      if (data[i][j] == 0 && data[i - 1][j] != 0) {
        Switch(&data[i][j], &data[i - 1][j]);
      }




    }
  }
}


void Switch(int *a, int *b) {
  *a ^= *b;
  *b ^= *a;
  *a ^= *b;
}
void MAX7219_Display()
{
  for (int j = 0; j < 8; j++)
  {
    lc.setRow(0, j, Int_array_to_byte(j));
  }


  for (int j = 8; j < 16; j++)
  {
    lc.setRow(1, j - 8, Int_array_to_byte(j));
  }
}
int Int_array_to_byte(int a)//
{
  int temp = 0;
  for (int i = 0; i < 8 ; i++)
  {
    int b = 1;
    for (int m = 0; m < (7 - i); m++) {


      b = b * 2;
    }
    temp = data[a + 3][i] * b + temp;


  }
  return temp;


}


void MAX7219_Init()
{
  for (int i = 0; i < 2; i++)
  {
    lc.shutdown(i, false);
    lc.setIntensity(i, 1);
    lc.clearDisplay(i);
  }
}

Credits

Elegoo Official

Elegoo Official

3 projects • 18 followers

Comments