#include <avr/io.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
unsigned long previousMillis_PC1 = 0;
const long interval_PC1 = 1000;
unsigned long previousMillis_PC1_sh = 0;
const long interval_PC1_sh = 1000;
unsigned long previousMillis_PC2 = 0;
const long interval_PC2 = 1000;
unsigned long previousMillis_PC2_sh = 0;
const long interval_PC2_sh = 1000;
bool insert_while_tone_pc1 = false;
bool insert_while_tone_pc2 = false;
bool pc1_power = false;
bool pc2_power = false;
bool player_1_time_end = false;
bool player_2_time_end = false;
bool first_pu = true; /////need to change
volatile int PC1_min = 0;
volatile int PC1_sec = 0;
volatile int PC1_hour = 0;
volatile bool coininserted_pc1 = false;
volatile bool coininserted_pc2 = false;
volatile int PC2_min = 0;
volatile int PC2_sec = 0;
volatile int PC2_hour = 0;
#define coinSlot1 2
#define coinSlot2 3
#define buzzer_PC1 13
#define buzzer_PC2 12
#define relay_player_1 A0 //player 1 contoller
#define relay_player_2 A2 //player 2 contoller
#define relay_pc_power A1 //pc power buttom
#define relay_screen_power A3 //monitor power source
int tone_pc1 = 0;
int tone_pc2 = 0;
int pc1_shutd = 0;
int pc2_shutd = 0;
int turn_on = 0;
int turn_off = 0;
int ON = 255;
int OFF = 0;
/////////////////////////////////////////////
/////////////////////////////////////////////
volatile float chargetime = 1; /// minutes per coin
int shutd_timer = 16; /// shutdown process time
uint32_t ChipID = 4294967295; /// DEVICE ID MUST be UNIQUE every board
#define prod ///set to prod once all is OK, this test to include ChipID to avoid copy of HEX code
////////////////////////////////////////////
////////////////////////////////////////////
int main_pc1 = shutd_timer;
int main_pc2 = shutd_timer;
int reverse_pc2 = 0;
int timer[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int y = 0;
int z = 8;
uint32_t CHIPID() {
uint32_t chipId = 0;
for (int i = 0; i < 4; i++) {
chipId |= (uint32_t)(eeprom_read_byte((uint8_t*)(0x0E + i)) << (i * 8));
}
Serial.println(chipId);
if (chipId != ChipID) {
lcd.setCursor(0, 0);
lcd.print("NOT VALID DEVICE ID");
lcd.setCursor(0, 1);
lcd.print("CODE has a copyright");
lcd.setCursor(0, 2);
lcd.print("contact manufacturer");
lcd.setCursor(0, 3);
lcd.print("for code detail !!!");
while (true);
} else {
lcd.setCursor(0, 0);
lcd.print("WELCOME");
}
delay(5000);
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
#ifdef prod
CHIPID();
#endif
pinMode(buzzer_PC1, OUTPUT);
pinMode(buzzer_PC2, OUTPUT);
pinMode(relay_player_1, OUTPUT);
pinMode(relay_player_2, OUTPUT);
pinMode(relay_pc_power, OUTPUT);
pinMode(relay_screen_power, OUTPUT);
pinMode(coinSlot1, INPUT_PULLUP);
pinMode(coinSlot2, INPUT_PULLUP);
///////////////read dip value//////////////////
for (int i = 4; i <= 11; i++) {
// Serial.print("pin = ");
// Serial.print(i);
// Serial.print(" ");
pinMode(i, INPUT_PULLUP);
timer[y] = digitalRead(i);
// Serial.print("Status= ");
// Serial.println(timer[y]);
if (!timer[y]) {
chargetime += z;
}
y ++;
z--;
delay(10);
}
Serial.print("chargetime= ");
Serial.println(chargetime);
//////////////////////////////////////////
attachInterrupt(digitalPinToInterrupt(coinSlot1), PC_1, FALLING);
attachInterrupt(digitalPinToInterrupt(coinSlot2), PC_2, FALLING);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("* Pysonet05 *");
analogWrite(relay_player_1, OFF);
analogWrite(relay_player_2, OFF);
analogWrite(relay_pc_power, OFF);
analogWrite(relay_screen_power, OFF);
/*
tone(buzzer_PC1, 262);
delay(150);
tone(buzzer_PC1, 294);
delay(150);
tone(buzzer_PC1, 330);
delay(150);
tone(buzzer_PC1, 349);
delay(150);
tone(buzzer_PC1, 392);
delay(150);
tone(buzzer_PC1, 440);
delay(150);
tone(buzzer_PC1, 494);
delay(150);
tone(buzzer_PC1, 563);
delay(500);
noTone(buzzer_PC1);
delay(250);
tone(buzzer_PC2, 563);
delay(500);
tone(buzzer_PC2, 494);
delay(250);
tone(buzzer_PC2, 440);
delay(250);
tone(buzzer_PC2, 392);
delay(250);
tone(buzzer_PC2, 349);
delay(250);
tone(buzzer_PC2, 330);
delay(250);
tone(buzzer_PC2, 294);
delay(250);
tone(buzzer_PC2, 262);
delay(250);
noTone(buzzer_PC2);
delay(500);
*/
}
void loop() {
coin_insert_beep();
print_time_PC1();
print_time_PC2();
countdown_PC1();
countdown_PC2();
relay();
#ifdef not_prod
lcd.setCursor(0, 1);
lcd.print ("chargetime = ");
lcd.print (chargetime);
// init_power_pc1();
#endif
}
void countdown_PC1() {
unsigned long currentMillis_PC1 = millis();
if (currentMillis_PC1 - previousMillis_PC1 >= interval_PC1) {
previousMillis_PC1 = currentMillis_PC1;
if (PC1_sec > 0 || PC1_min > 0 || PC1_hour > 0) {
warning_pc1();
PC1_sec --;
}
if (PC1_sec < 0) {
PC1_min--;
PC1_sec = 59;
}
if (PC1_min < 0) {
PC1_hour--;
PC1_min = 59;
}
if (PC1_hour < 0) {
PC1_hour = 0;
PC1_min = 0;
PC1_sec = 0;
}
if (PC1_min >= 60) {
PC1_hour += 1;
PC1_min -= 60;
}
}
}
void countdown_PC2() {
unsigned long currentMillis_PC2 = millis();
if (currentMillis_PC2 - previousMillis_PC2 >= interval_PC2) {
previousMillis_PC2 = currentMillis_PC2;
if (PC2_sec > 0 || PC2_min > 0 || PC2_hour > 0) {
warning_pc2();
PC2_sec --;
}
if (PC2_sec < 0) {
PC2_min--;
PC2_sec = 59;
}
if (PC2_min < 0) {
PC2_hour--;
PC2_min = 59;
}
if (PC2_hour < 0) {
PC2_hour = 0;
PC2_min = 0;
PC2_sec = 0;
}
if (PC2_min >= 60) {
PC2_hour += 1;
PC2_min -= 60;
}
}
}
void print_time_PC1() {
if (PC1_sec > 0 || PC1_min > 0 || PC1_hour > 0) {
lcd.setCursor(16, 2);
lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print ("Player1");
lcd.setCursor(8, 2);
if (PC1_hour <= 9) {
lcd.print ("0");
}
lcd.print (PC1_hour);
lcd.print (":");
if (PC1_min <= 9) {
lcd.print ("0");
}
lcd.print (PC1_min);
lcd.print (":");
if (PC1_sec <= 9) {
lcd.print ("0");
}
lcd.print (PC1_sec);
} if (pc1_shutd <= 0 && PC1_sec == 0 && PC1_min == 0 && PC1_hour == 0 ) {
lcd.setCursor(0, 2);
lcd.print ("Player1");
lcd.setCursor(8, 2);
lcd.print ("INSERT Coin ");
} if (pc1_shutd != 0 && PC1_sec == 0 && PC1_min == 0 && PC1_hour == 0 && pc1_power == true) {
lcd.setCursor(0, 2);
lcd.print ("Shutting Down Player 1 ");
}
}
void print_time_PC2() {
if (PC2_sec > 0 || PC2_min > 0 || PC2_hour > 0) {
lcd.setCursor(16, 3);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print ("Player2");
lcd.setCursor(8, 3);
if (PC2_hour <= 9) {
lcd.print ("0");
}
lcd.print (PC2_hour);
lcd.print (":");
if (PC2_min <= 9) {
lcd.print ("0");
}
lcd.print (PC2_min);
lcd.print (":");
if (PC2_sec <= 9) {
lcd.print ("0");
}
lcd.print (PC2_sec);
} if (pc2_shutd <= 0 && PC2_sec == 0 && PC2_min == 0 && PC2_hour == 0 ) {
lcd.setCursor(0, 3);
lcd.print ("Player2");
lcd.setCursor(8, 3);
lcd.print ("INSERT Coin ");
} if (pc2_shutd != 0 && PC2_sec == 0 && PC2_min == 0 && PC2_hour == 0 && pc2_power == true) {
lcd.setCursor(0, 3);
lcd.print ("Shutting Down Player2");
}
}
void PC_1() {
PC1_min += chargetime;
coininserted_pc1 = true;
Serial.println("adding 1");
}
void PC_2 () {
PC2_min += chargetime;
coininserted_pc2 = true;
Serial.println("adding 2");
}
void relay() {
if (
PC1_hour > 0 ||
PC1_min > 0 ||
PC1_sec > 0
) {
analogWrite(relay_player_1, ON);
first_pu = false;
pc1_power = true;
player_1_time_end = false;
} else {
analogWrite(relay_player_1, OFF);
player_1_time_end = true;
}
if (
PC2_hour > 0 ||
PC2_min > 0 ||
PC2_sec > 0
) {
analogWrite(relay_player_2, ON);
first_pu = false;
pc2_power = true;
player_2_time_end = false;
} else {
analogWrite(relay_player_2, OFF);
player_2_time_end = true;
}
if (player_1_time_end && player_2_time_end && !first_pu) {
unsigned long currentMillis_PC2_sh = millis();
if (currentMillis_PC2_sh - previousMillis_PC2_sh >= interval_PC2_sh && turn_off < 8) {
previousMillis_PC2_sh = currentMillis_PC2_sh;
turn_off ++;
Serial.println(turn_off);
if (turn_off >= 1 and turn_off < 3) {
Serial.println("turning off pc");
analogWrite(relay_pc_power, ON);
} else {
analogWrite(relay_pc_power, OFF);
}
if (turn_off >= 8) {
analogWrite(relay_screen_power, OFF); //turn off monitorF
}
}
turn_on = 0;
} else if (!player_1_time_end || !player_2_time_end) {
unsigned long currentMillis_PC2_sh = millis();
if (currentMillis_PC2_sh - previousMillis_PC2_sh >= interval_PC2_sh && turn_on < 5) {
previousMillis_PC2_sh = currentMillis_PC2_sh;
turn_on ++;
Serial.println(turn_on);
if (turn_on >= 1 and turn_on < 4) {
Serial.println("turning on pc");
analogWrite(relay_pc_power, ON);
} else {
analogWrite(relay_pc_power, OFF);
}
}
analogWrite(relay_screen_power, ON);
turn_off = 0;
}
}
void warning_pc1() {
if (PC1_min >= 0 && PC1_sec == 60) {
insert_while_tone_pc1 = false;
}
if (PC1_min <= 1 && PC1_sec == 0 && PC1_hour == 0 || PC1_hour == 0 && PC1_min == 0 && PC1_sec <= 6) {
tone_pc1 = 1500;
insert_while_tone_pc1 = false;
}
if (PC1_min == 0 && PC1_sec == 55 || PC1_min == 0 && PC1_sec == 0 || insert_while_tone_pc1 == true) {
tone_pc1 = 0;
}
// Serial.println(tone_pc1);
//Serial.println("are we going here");
tone(buzzer_PC1, tone_pc1);
delay(20);
noTone(buzzer_PC1);
}
void warning_pc2() {
if (PC2_min >= 1 && PC1_sec == 60) {
insert_while_tone_pc2 = false;
}
if (PC2_min <= 1 && PC2_sec == 0 && PC2_hour == 0 || PC2_hour == 0 && PC2_min == 0 && PC2_sec <= 6) {
tone_pc2 = 1000;
insert_while_tone_pc2 = false;
}
if (PC2_min == 0 && PC2_sec == 55 || PC2_min == 0 && PC2_sec == 0 || insert_while_tone_pc2 == true) {
tone_pc2 = 0;
}
// Serial.println(tone_pc2);
//Serial.println("are we going here");
tone(buzzer_PC2, tone_pc2);
delay(20);
noTone(buzzer_PC2);
}
void coin_insert_beep() {
while (coininserted_pc1) {
tone(buzzer_PC1, 2000);
delay(100);
noTone(buzzer_PC1);
insert_while_tone_pc1 = true;
coininserted_pc1 = 0;
}
while (coininserted_pc2) {
tone(buzzer_PC2, 2000);
delay(100);
noTone(buzzer_PC2);
insert_while_tone_pc2 = true;
coininserted_pc2 = 0;
}
}