21 October 2019

Computer Says: Break Time!

 *** SPOILER ALERT ***

* Dear avid readers: if you intend to play the game please wait to read this! *


For the Communication assignment, I first considered getting inspiration from the idea of assigning items on my desk into categories. Then I looked at my desk.

Yeah... no.

I was also thinking about the serial monitor communication and how I might be able to write the computer as a character, and the materials for this assignment (specifically the RFID reader) reminded me of making games.... and kaPOW! This idea:

Ta-da!


So easy, right? Somehow a part of me believes that just a little bit, despite logic and consistent evidence to the contrary. Fortunately, Fotoon was kind enough to lend me her RFID reader, and I had an extra Uno board from last semester, so I knew this would be possible

Many hours later, I scrapped the idea of the LED matrix and reused my "Simon Says" code with some modifications. I also went from the four digit seven segment display to the single digit version, and made a simpler countdown as a result. I struggled pretty hard on understanding much of the code and some of the wiring (not where it went but what it did exactly) for these components, and what I have below is largely an experimental mish-mosh of sample-code, found-on-internet-code, and code-that-could-probably-be-simplified.

My feelings while trying to make things work

I like the idea of "think outside the box" puzzles, and I wanted to get back into the swing of test playing concepts. I always worry problems like this will be too obvious, but hopefully people don't figure out the trick right away. :) 

Inside!

Outisde! (tweaked the message after this photo)

The following is a playthrough video, a schematic, and code for both programs that are running (one requires connection to the computer for the serial monitor, the other just need battery power).

Enjoy!

As always,
CapriciousClockwinder2345620 + Toupe4
(no ledge)





#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
// from _75 hc, setting up on/off mapping to make each digit in 7-seg display
byte seven_seg_digits[10] = { B11111100, // = 0
B01100000, // = 1
B11011010, // = 2
B11110010, // = 3
B01100110, // = 4
B10110110, // = 5
B10111110, // = 6
B11100000, // = 7
B11111110, // = 8
B11100110 // = 9
};
//7-seg display pin assignments
int latchPin = 3;
int clockPin = 4;
int dataPin = 2;
void setup() {
Serial.begin(9600);
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Break time, Human!");
delay(500);
Serial.println("Select difficulty for your 'Super Fun Break' game:");
delay(500);
Serial.println("1: Easy");
delay(500);
Serial.println("2: Medium");
delay(1000);
Serial.println("3: Brutal >:)");
// Set latchPin, clockPin, dataPin as output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
// display a number on the digital segment display
void numba(byte digit) {
// set the latchPin to low potential, before sending data
digitalWrite(latchPin, LOW);
// the original data (bit pattern)
shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]);
// set the latchPin to high potential, after sending data
digitalWrite(latchPin, HIGH);
}
void loop() {
if ( ! mfrc522.PICC_IsNewCardPresent())
{
//return;
if(Serial.available() > 0) {
char x = Serial.read();
Serial.println(x);
if(x == '1') {
Serial.println("Lame...");
delay(1000);
//makes the timer "act weird"
for (byte digit = 10; digit > 0; --digit) {
delay(100);
numba(digit - 1);
}
numba(7);
delay(100);
numba(3);
delay(100);
numba(5);
delay(100);
numba(2);
delay(100);
numba(8);
delay(100);
numba(1);
delay(100);
numba(9);
delay(100);
numba(4);
delay(100);
numba(2);
delay(100);
numba(8);
delay(100);
numba(8);
delay(100);
numba(1);
delay(100);
numba(9);
delay(100);
numba(4);
delay(100);
numba(2);
delay(100);
Serial.println("Oops! Looks like that difficulty doesn't work yet. Try again!");
}
if(x == '2') {
Serial.println("Let's see...");
delay(1000);
//makes the timer "act weird"
for (byte digit = 0; digit < 10; ++digit) {
delay(100);
numba(digit + 1);
}
for (byte digit = 0; digit < 10; ++digit) {
delay(100);
numba(digit + 1);
}
for (byte digit = 0; digit < 10; ++digit) {
delay(100);
numba(digit + 1);
}
Serial.println("Huh");
delay(2000);
Serial.println("Looks like there are some technical issues going on.");
delay(2000);
Serial.println("So weird.");
delay(750);
Serial.println("Well, guess you need to select a different option!");
}
if(x == '3') {
Serial.println("AWWWWWWWWWWW YEAH!");
delay(3000);
Serial.println("Ahem.");
delay(500);
Serial.println("Difficulty set. Activate your play session by scanning your 'FUN' card where instructed.");
Serial.println("Check the countdown timer - your time starts now!");
for (byte digit = 10; digit > 0; --digit) {
delay(3500);
numba(digit - 1);
}
delay(10000);
Serial.println("Um... did you read the instructions?");
}
}
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
//Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
//Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
//Serial.print(mfrc522.uid.uidByte[i], HEX);
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) == "9B 95 EB 0A") //change here the UID of the card/cards that you want to give access
{
Serial.println("Wow, you figured it out! Nice job!");
Serial.println();
Serial.println("Now it's time to play the REAL game!");
delay(3000); //keeps from spamming the serial monitor
Serial.println("Oh, break time's over! Wasn't that fun??");
delay(1000);
Serial.println("Now get back to work! ^_^");
delay(3000);
}
else { //if you try a different card or tag
Serial.println("Um, no. I said 'FUN' card, remember?");
delay(3000);
}
}
/*some extra code I tried and either didn't figure out or didn't need:
Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
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) == "9B 95 EB 0A") //change here the UID of the card/cards that you want to give access
{
Serial.println("Authorized access");
Serial.println();
Display(9);
delay(5000);
Display(10);
delay(5000);
Display(11);
delay(5000);
Display(12);
delay(5000);
Display(13);
delay(5000);
Display(14);
delay(5000);
Display(15);
delay(5000);
}
else {
Serial.println(" Access denied");
delay(3000);
}*/
/*int latch=5; //74HC595 pin 9 STCP
int clock=6; //74HC595 pin 10 SHCP
int data=4; //74HC595 pin 8 DS
/*unsigned char table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c
,0x39,0x5e,0x79,0x71,0x00};*/
/*const byte numbers[10][7] = {
// 0: segment lit, 1: unlit
// a b c d e f g
{0, 0, 0, 0, 0, 0, 1}, // 0
{1, 0, 0, 1, 1, 1, 1}, // 1
{0, 0, 1, 0, 0, 1, 0}, // 2
{0, 0, 0, 0, 1, 1, 0}, // 3
{1, 0, 0, 1, 1, 0, 0}, // 4
{0, 1, 0, 0, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 0, 0}, // 6
{0, 0, 0, 1, 1, 1, 1}, // 7
{0, 0, 0, 0, 0, 0, 0}, // 8
{0, 0, 0, 1, 1, 0, 0}, // 9
};*/
/*void Display(unsigned char num) //this is a function for the LED display
{
digitalWrite(latch,LOW);
shiftOut(data,clock,MSBFIRST,table[num]);
digitalWrite(latch,HIGH);
}*/
//THIS IS THE SECOND SET OF CODE FOR SIMON SAYS WITH THE RFID!!!!!!!!
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
#define greenLED 2
#define blueLED 3
#define yellowLED 4
#define gButton 5
#define bButton 6
#define yButton 7
#define start 8
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
void setup() {
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(gButton, INPUT_PULLUP);
pinMode(bButton, INPUT_PULLUP);
pinMode(yButton, INPUT_PULLUP);
pinMode(start, INPUT_PULLUP);
}
void loop() {
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
//Serial.print("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{
//Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
//Serial.print(mfrc522.uid.uidByte[i], HEX);
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) == "9B 95 EB 0A") //change here the UID of the card/cards that you want to give access
{ //that time I wrote snarky dialogue and then remembered this would never be seen :'(
Serial.println("Oh, you want to play Simon Says?");
delay(200);
Serial.println("BRING IT");
Serial.println();
delay(200);
//simon says sequence
digitalWrite(blueLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
delay(200);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(yellowLED, HIGH);
delay(200);
digitalWrite(yellowLED, LOW);
delay(200);
digitalWrite(blueLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
delay(200);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(yellowLED, HIGH);
delay(200);
digitalWrite(yellowLED, LOW);
delay(200);
digitalWrite(blueLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
delay(200);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(yellowLED, HIGH);
delay(200);
digitalWrite(yellowLED, LOW);
delay(200);
digitalWrite(blueLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
delay(200);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(greenLED, LOW);
delay(200);
Serial.print("YOUR TURN >:)");
Serial.print("Press the 3 leftmost buttons to replay the sequence. Press the right button to reset, then rescan");
Serial.print("and prepare to lose again!!!!!");
while (digitalRead(start) == HIGH) {
if (digitalRead(gButton) == LOW) {
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, HIGH);
digitalWrite(yellowLED,LOW);
}
if (digitalRead(bButton) == LOW) {
digitalWrite(blueLED, HIGH);
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED,LOW);
}
if (digitalRead(yButton) == LOW) {
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED,HIGH);
}
if (digitalRead(start) == LOW) {
digitalWrite(blueLED, HIGH);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
delay(200);
digitalWrite(blueLED, HIGH);
digitalWrite(yellowLED, HIGH);
digitalWrite(greenLED, HIGH);
delay(200);
digitalWrite(blueLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(greenLED, LOW);
return;
}
else {
digitalWrite(blueLED, LOW);
digitalWrite(greenLED, LOW);
digitalWrite(yellowLED,LOW);
}
}
}
else {
Serial.println(" Access denied");
delay(3000);
}
}
PS thanks to Random Nerd Tutorials for providing some code that helped me understand and use the RFID

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.