#include <SoftwareSerial.h>
#include "TFMini.h"
#define distance_threshold 30 // cm
// Setup software serial port
SoftwareSerial mySerial(10, 11); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini1;
TFMini tfmini2;
TFMini tfmini3;
#define light_sensor1 7
#define light_sensor2 6
#define light_sensor3 5
#define light_sensor4 4
#define light_sensor5 3
#define light_sensor6 2
#define light_sensor7 14
#define light_sensor8 15
#define light_sensor9 16
#define light_sensor10 17
#define light_sensor11 18
#define light_sensor12 19
#define light_sensor13 20
#define light_sensor14 21
#define occupancy_sensor1 8
#define occupancy_sensor2 9
#define occupancy_sensor3 10
#define occupancy_sensor4 11
#define occupancy_sensor5 12
#define unlock_button 13
#define open_button 23
#define reset_button 25
#define start_button 27
#define status_led_green 29
#define status_led_red 31
#define magnetic_lock 33
#define speaker 35
//mp3 player
bool magnetic_lock_status = false;
int light_sensor[14] = {light_sensor1 , light_sensor2 , light_sensor3 , light_sensor4 ,
light_sensor5 , light_sensor6 , light_sensor7 , light_sensor8 ,
light_sensor9 , light_sensor10 , light_sensor11 , light_sensor12 ,
light_sensor13 , light_sensor14 };
int occupancy_sensor[5] = {occupancy_sensor1 , occupancy_sensor2 , occupancy_sensor3 , occupancy_sensor4 ,
occupancy_sensor5};
bool laser_status = false;
int number_of_lasers = 14;
int failures = 0;
void setup()
{
Serial.begin(9600);
Serial1.begin(TFMINI_BAUDRATE);
Serial2.begin(TFMINI_BAUDRATE);
Serial3.begin(TFMINI_BAUDRATE);
// Step 3: Initialize the TF Mini sensor
tfmini1.begin(&Serial1);
tfmini1.begin(&Serial2);
tfmini1.begin(&Serial3);
for(int i = 0 ; i < 14 ; i++)
{
pinMode(light_sensor[i] , INPUT_PULLUP);
pinMode(occupancy_sensor[i] , INPUT_PULLUP);
}
pinMode(unlock_button , INPUT_PULLUP);
pinMode(open_button , INPUT_PULLUP);
pinMode(reset_button , INPUT_PULLUP);
pinMode(start_button , INPUT_PULLUP);
pinMode(status_led_green , OUTPUT);
pinMode(status_led_red , OUTPUT);
pinMode(magnetic_lock , OUTPUT);
pinMode(speaker , OUTPUT);
digitalWrite(status_led_green , LOW);
digitalWrite(status_led_red , LOW);
Serial.println("waiting for game to start");
while(1)
{
if(checking_game_start() == false) Serial.println("start condition failed");
else
{
Serial.println("start condition passed");
break;
}
}
Serial.println("\n\n\nGame started");
}
void loop()
{
Serial.println("waiting for game to start");
while(1)
{ check_unlock_button();
if(checking_game_start() == false) Serial.println("start condition failed");
else
{
Serial.println("start condition passed");
break;
}
}
Serial.println("\n\n\nGame started");
laser_status = false;
while(1)
{
check_unlock_button();
for (int i = 0 ; i < number_of_lasers ; i++)
{
if(digitalRead(light_sensor[i])==LOW) laser_status = true;
}
if(laser_status == true) break;
if(digitalRead(open_button) == LOW ) break;
}
if(laser_status == true)
{ failures++;
digitalWrite(status_led_red , HIGH);
digitalWrite(status_led_green , LOW);
//disable sound
}
else
{
failures = 0;
digitalWrite(status_led_green , HIGH);
digitalWrite(status_led_red , LOW);
//play success tune
digitalWrite(magnetic_lock , LOW);
}
int occupancy_sensor_count = 0;
while(occupancy_sensor_count < 5)
{
for (int i = 0 ; i < 5 ; i++)
{ occupancy_sensor_count = 0;
if(digitalRead(occupancy_sensor[i]) == LOW) occupancy_sensor_count++;
}
}
while(digitalRead(open_button));
//play reset sound
digitalWrite(status_led_green , LOW);
digitalWrite(status_led_red , LOW);
digitalWrite(magnetic_lock , LOW);
if(failures == 2)
{
failures = 0;
number_of_lasers--;
if(number_of_lasers < 4) number_of_lasers = 4;
}
}
void check_unlock_button()
{
if(digitalRead(unlock_button) == LOW)
{
double prev_millis = millis();
while(digitalRead(unlock_button) == LOW);
if((millis() - prev_millis) > 5000) {magnetic_lock_status = true; digitalWrite(magnetic_lock , HIGH);}
else { magnetic_lock_status = false; digitalWrite(magnetic_lock , LOW);}
}
}
bool checking_game_start()
{
Serial.println("checking if magnetic snesor is on");
while(start_button == HIGH);
Serial.println("checking if magnetic snesor is off for 5 sec");
double prev_millis = millis();
if((millis() - prev_millis) > 5000) return true;
else return false;
}
bool distance_check()
{
uint16_t dist1 , dist2 , dist3;
dist = tfmini1.getDistance();
if(dist < distance_threshold) return false;
dist = tfmini1.getDistance();
if(dist < distance_threshold) return false;
dist = tfmini1.getDistance();
if(dist < distance_threshold) return false;
return true;
}