/// Libraries
#include <LiquidCrystal_I2C.h>
#include <Adafruit_NeoPixel.h>
#include <TM1637.h>
// pins initialize
#define armKey 22
#define armLed 23
#define safeLed 25
#define CLK 13
#define DIN 11
#define CS 10
const int CLKTme = 2;
const int DIO = 3;
#define btn1 41
#define btn2 43
#define btn3 45
#define btn4 47
#define btn5 49
#define btn6 51
#define btn7 53
#define btn8 52
///Lcd variables
unsigned long startTime = 0;
unsigned long Timeleft = 0;
const unsigned long Timer = 120000; // 2 minutes timer
/// 8x8 matric variables
#define NUM_SEGMENTS 4
/// WS2812 initializing
#define PIN_WS2812B 37
#define NUM_PIXELS 8
/// temp sensor
const float BETA = 3950;
/// project variables
bool armText = false;
bool safeText = false;
bool game1 = true;
bool game2 = true;
// bool game3 = true;
// Class constructors
LiquidCrystal_I2C lcd(0x27, 16, 2);
Adafruit_NeoPixel strip(NUM_PIXELS, PIN_WS2812B, NEO_GRB + NEO_KHZ800);
TM1637 tm(CLKTme, DIO);
void setup() {
Serial.begin(115200);
pinMode(armKey, INPUT_PULLUP);
pinMode(armLed, OUTPUT);
pinMode(safeLed, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(DIN, OUTPUT);
pinMode(CS, OUTPUT);
pinMode (PIN_WS2812B, OUTPUT);
pinMode(btn1, INPUT_PULLUP);
pinMode(btn2, INPUT_PULLUP);
pinMode(btn3, INPUT_PULLUP);
pinMode(btn4, INPUT_PULLUP);
pinMode(btn5, INPUT_PULLUP);
pinMode(btn6, INPUT_PULLUP);
pinMode(btn7, INPUT_PULLUP);
pinMode(btn8, INPUT_PULLUP);
/// LCD initializing
lcd.init();
lcd.backlight();
strip.begin();
strip.show();
tm.init();
tm.set(BRIGHT_TYPICAL);
}
void loop() {
if (digitalRead(armKey) == 1) {
if (!armText) {
Serial.println("on");
lcd.setCursor(5, 0);
lcd.print("ARMED");
digitalWrite(armLed, HIGH);
digitalWrite(safeLed, LOW);
delay(1000);
allMatrixOn();
allWs2812Green();
lcd.setCursor(0, 0);
lcd.print("TIME LEFT: ");
lcd.setCursor(11, 0);
lcd.print("120s");
tm.display(2, 0);
tm.display(3, 0);
Temp();
safeText = false;
armText = true;
game1 = false;
startTime = millis();
}
if (millis() - startTime < Timer) {
lcd.setCursor(11, 0);
lcd.print((Timer - startTime) / 1000);
lcd.print("s ");
startTime = millis();
if (game1 == false && digitalRead(btn1) == 1 && digitalRead(btn2) == 0 && digitalRead(btn3) == 1 && digitalRead(btn4) == 1 && digitalRead(btn5) == 0 && digitalRead(btn6) == 0 && digitalRead(btn7) == 1 && digitalRead(btn8) == 1 ) {
turnMatrixOffUpTo(1);
game1 = true;
game2 = false;
Serial.println("game1");
}
// else if(game2 == false )
}
} else {
if (!safeText) {
Serial.println("off");
lcd.clear();
digitalWrite(armLed, LOW);
digitalWrite(safeLed, HIGH);
lcd.setCursor(5, 0);
lcd.print("SAFE");
allWs2812OFF();
allMatrixOff();
tm.display(0, 0);
tm.display(1, 0);
safeText = true;
armText = false;
game1 = true;
game2 = true;
startTime = 0;
}
}
delay(500);
}
/////////////////////////////////////////////////////////////////
//////////////// 8x8 MATRIX functions START //////////////////
/////////////////////////////////////////////////////////////////
void allMatrixOn() {
for (byte row = 1; row <= 8; row++) {
digitalWrite(CS, LOW);
for (int i = 0; i < NUM_SEGMENTS; i++) {
shiftOut(DIN, CLK, MSBFIRST, row);
shiftOut(DIN, CLK, MSBFIRST, 0xFF); // Full row ON
}
digitalWrite(CS, HIGH);
}
}
void allMatrixOff() {
for (byte row = 1; row <= 8; row++) {
digitalWrite(CS, LOW);
for (int i = 0; i < NUM_SEGMENTS; i++) {
shiftOut(DIN, CLK, MSBFIRST, row);
shiftOut(DIN, CLK, MSBFIRST, 0x00); // Full row OFF
}
digitalWrite(CS, HIGH);
}
}
void turnMatrixOffUpTo(byte moduleNumber) {
for (byte row = 1; row <= 8; row++) {
digitalWrite(CS, LOW);
for (int i = NUM_SEGMENTS; i > 0; i--) {
shiftOut(DIN, CLK, MSBFIRST, row);
if (i <= moduleNumber)
shiftOut(DIN, CLK, MSBFIRST, 0x00); // OFF
else
shiftOut(DIN, CLK, MSBFIRST, 0xFF); // ON
}
digitalWrite(CS, HIGH);
}
}
/////////////////////////////////////////////////////////////////
//////////////// 8x8 MATRIX functions END ////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
//////////////// WS2812B functions START /////////////////////
/////////////////////////////////////////////////////////////////
void allWs2812Green() {
for (int i = 0; i < NUM_PIXELS; i++) {
strip.setPixelColor(i, strip.Color(0, 255, 0));
}
strip.show();
}
// Function: All Red
void allWs2812Red() {
for (int i = 0; i < NUM_PIXELS; i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
strip.show();
}
// Function: Red Flash
void redFlashWs2812(int delayTime) {
for (int i = 0; i < NUM_PIXELS; i++) {
strip.setPixelColor(i, strip.Color(255, 0, 0));
}
strip.show();
delay(delayTime);
strip.clear();
strip.show();
delay(delayTime);
}
/// Function: All off
void allWs2812OFF() {
for (int i = 0; i < NUM_PIXELS; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
}
/////////////////////////////////////////////////////////////////
//////////////// WS2812B functions END /////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
//////////////// Temperature functions START /////////////////
/////////////////////////////////////////////////////////////////
int Temp() {
int analogValue = analogRead(A0);
int celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
tm.display(0, (celsius / 10) % 10);
tm.display(1, celsius % 10);
return celsius;
}
/////////////////////////////////////////////////////////////////
//////////////// Temperature functions END /////////////////
/////////////////////////////////////////////////////////////////Arm Key Switch
Arm Key LED
Safe Key LED
Case LED STRIP
8x8 DOT MATRIX