#include #include int BUZZER = 2; int goalSensor1 = 4; int goalSensor2 = 5; int SS_PIN = 10; int RST_PIN = 9; MFRC522 mfrc522(SS_PIN, RST_PIN); int scoreSpeler1 = 0; int scoreSpeler2 = 0; int LDR1 = A0; int LDR2 = A1; int laser1 = 6; int laser2 = 7; float tijd; float tijdstip; float tijdstip2; float seconden = 0; int waardeLDR1 = 0; int waardeLDR2 = 0; int statusGoalSensor1 = 0; int statusGoalSensor2 = 0; int nieuweScore = 0; String ontvangen_str; float hardheid = 0; String speler; String score; void setup() { Serial.begin(9600); SPI.begin(); // Init SPI bus mfrc522.PCD_Init(); // Init MFRC522 pinMode(goalSensor1, INPUT); pinMode(goalSensor2, INPUT); pinMode(laser1, OUTPUT); pinMode(laser2, OUTPUT); pinMode(BUZZER, OUTPUT); digitalWrite(goalSensor1, HIGH); digitalWrite(goalSensor2, HIGH); digitalWrite(laser1, LOW); digitalWrite(laser2, LOW); } void loop() { if (Serial.available() > 0) { ontvangen_str = Serial.readString(); if (ontvangen_str == "start"){ geluid_start(); scoreSpeler1 = 0; scoreSpeler2 = 0; digitalWrite(laser1, HIGH); digitalWrite(laser2, HIGH); } else{ speler = ontvangen_str.substring(0,1); score = ontvangen_str.substring(2); nieuweScore = score.toInt(); if(speler == "1"){ scoreSpeler1 = nieuweScore; Serial.print("speler 1 : "); Serial.print(scoreSpeler1); Serial.println(" 0"); } else if(speler == "2"){ scoreSpeler2 = nieuweScore; Serial.print("speler 2 : "); Serial.print(scoreSpeler2); Serial.println(" 0"); } } } tijd = millis(); waardeLDR1 = analogRead(LDR1); waardeLDR2 = analogRead(LDR2); statusGoalSensor1 = digitalRead(goalSensor1); statusGoalSensor2 = digitalRead(goalSensor2); if(waardeLDR1 > 100){ tijdstip = tijd/1000; } if(waardeLDR2 > 100){ tijdstip = tijd*0.001; } if(statusGoalSensor1 == 0){ scoreSpeler2++; tijdstip2 = tijd/1000; seconden = tijdstip2 - tijdstip; hardheid = berekenSchot(seconden); goal(); Serial.print("speler 2 : "); Serial.print(scoreSpeler2); Serial.print(" "); Serial.println(hardheid); delay(2000); } if (statusGoalSensor2 == 0){ scoreSpeler1++; tijdstip2 = tijd*0.001; seconden = tijdstip2 - tijdstip; hardheid = berekenSchot(seconden); goal(); Serial.print("speler 1 : "); Serial.print(scoreSpeler1); Serial.print(" "); Serial.println(hardheid); delay(2000); } if(scoreSpeler1 >= 10) { if (scoreSpeler1 - scoreSpeler2 > 1) { scoreSpeler1 = 0; scoreSpeler2 = 0; Serial.println("gedaan"); digitalWrite(laser1, LOW); digitalWrite(laser2, LOW); geluid_winnaar(); } } if(scoreSpeler2 >= 10) { if (scoreSpeler2 - scoreSpeler1 > 1) { scoreSpeler1 = 0; scoreSpeler2 = 0; Serial.println("gedaan"); geluid_winnaar(); digitalWrite(laser1, LOW); digitalWrite(laser2, LOW); } } // kijken voor nieuwe kaart if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // kaart lezen if ( ! mfrc522.PICC_ReadCardSerial()) { return; } String content= ""; byte letter; for (byte i = 0; i < mfrc522.uid.size; i++) { content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ")); content.concat(String(mfrc522.uid.uidByte[i], HEX)); } //Serial.println(); //Serial.print("Message : "); content.toUpperCase(); if (content.substring(1) == "89 B3 A7 B2" or content.substring(1) == "99 5C C2 C1") { Serial.println("badge"); geluid_scan(); //Serial.println(); delay(3000); } else { //Serial.println(" Access denied"); geluid_slechte_scan(); delay(3000); } } void goal(void) { int a=0; for(a = 0; a < 3; a++) { for (byte x = 250 ; x > 70 ; x--) { for (byte y = 0 ; y < 3 ; y++) { digitalWrite(BUZZER, HIGH); digitalWrite(BUZZER, LOW); delayMicroseconds(x); digitalWrite(BUZZER, LOW); digitalWrite(BUZZER, HIGH); delayMicroseconds(x); } } } } void buzz_sound(int buzz_length_ms, int buzz_delay_us) { long buzz_length_us = buzz_length_ms * (long)1000; while (buzz_length_us > (buzz_delay_us * 2)) { buzz_length_us -= buzz_delay_us * 2; digitalWrite(BUZZER, LOW); digitalWrite(BUZZER, HIGH); delayMicroseconds(buzz_delay_us); digitalWrite(BUZZER, HIGH); digitalWrite(BUZZER, LOW); delayMicroseconds(buzz_delay_us); } } void geluid_winnaar(void) { buzz_sound(100, 1136); buzz_sound(100, 568); buzz_sound(100, 851); buzz_sound(100, 638); buzz_sound(100, 1136); buzz_sound(100, 568); buzz_sound(100, 851); buzz_sound(100, 638); buzz_sound(200, 1136); buzz_sound(200, 568); buzz_sound(200, 851); buzz_sound(200, 638); buzz_sound(400, 1136); buzz_sound(400, 568); buzz_sound(400, 851); buzz_sound(400, 638); buzz_sound(100, 1136); buzz_sound(100, 568); buzz_sound(100, 851); buzz_sound(100, 638); buzz_sound(100, 1136); buzz_sound(100, 568); buzz_sound(100, 851); buzz_sound(100, 638); } void geluid_scan(void) { buzz_sound(200, 158); buzz_sound(200, 568); } void geluid_slechte_scan(void) { buzz_sound(200, 1500); buzz_sound(200, 2000); buzz_sound(200, 2500); } void geluid_start(void) { buzz_sound(100, 500); buzz_sound(200, 600); buzz_sound(100, 400); buzz_sound(100, 500); buzz_sound(200, 400); buzz_sound(100, 600); } float CMS; float berekenSchot(float waarde){ if(waarde != 0) { CMS = 10.0 / waarde; return CMS; } else{ return 0; } }