Last active
August 29, 2015 13:57
-
-
Save eberfreitas/9627108 to your computer and use it in GitHub Desktop.
Code for a game I'm developing for the birthday party of my boy :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "LedControl.h" | |
// Infra red pin | |
const int irPin = 13; | |
// Global variables | |
int previousIrVal = HIGH; // The previous value of the IR reading. HIGH by default | |
int gameOn = 1; // If the game is running or not. It's on by default | |
int score = 0; // Initial score | |
int time = 0; // Elapsed time playing | |
int timeSpan = 100; // Preventive time span for new points | |
int aMoment = 1000; // Time in milliseconds to increase time | |
int pointValue = 1; // How much a hit scores | |
long previousTime4Score = 0; // Last time a score was made | |
long previousTime4Timer = 0; // Last time a moment was counted | |
// Load | |
// Clock | |
// Data | |
LedControl lc = LedControl(8, 10, 9, 1); | |
void setup() { | |
//Serial.begin(9600); | |
pinMode(irPin, INPUT); | |
lc.shutdown(0, false); | |
lc.setIntensity(0, 15); | |
for (int i = 1; i <= 3; i++) { | |
lc.clearDisplay(0); | |
lc.setDigit(0, 3, (byte) i, false); | |
delay(100); | |
lc.clearDisplay(0); | |
lc.setDigit(0, 2, (byte) i, false); | |
delay(100); | |
lc.clearDisplay(0); | |
lc.setDigit(0, 1, (byte) i, false); | |
delay(100); | |
lc.clearDisplay(0); | |
lc.setDigit(0, 0, (byte) i, false); | |
delay(100); | |
lc.clearDisplay(0); | |
lc.setDigit(0, 7, (byte) i, false); | |
delay(100); | |
lc.clearDisplay(0); | |
lc.setDigit(0, 6, (byte) i, false); | |
delay(100); | |
lc.clearDisplay(0); | |
lc.setDigit(0, 5, (byte) i, false); | |
delay(100); | |
lc.clearDisplay(0); | |
lc.setDigit(0, 4, (byte) i, false); | |
delay(100); | |
} | |
lc.clearDisplay(0); | |
delay(200); | |
} | |
void loop() { | |
if (gameOn == 1) { | |
countdown(); | |
keepscore(); | |
} | |
displayScore(); | |
displayTime(); | |
} | |
// Method that actually counts time for the game | |
void countdown() { | |
long now = millis(); | |
int diff = now - previousTime4Timer; | |
// We don't use delay so we rely on the board time to | |
// add another second to the timer, which should be | |
// acurate enough | |
if (diff >= aMoment) { | |
time += 1; | |
previousTime4Timer = now; | |
// If we reach 60 seconds, we stop the game | |
if (time >= 60) { | |
gameOn = 0; | |
} | |
} | |
} | |
// This function controls the score | |
void keepscore() { | |
int currentIrVal = digitalRead(irPin); | |
// If there is something interupting the signal and | |
// it was not like that before, we try to register the score | |
if (currentIrVal == LOW && previousIrVal != LOW) { | |
long now = millis(); | |
int diff = now - previousTime4Score; | |
// But only if the last point as made after | |
// a time span... | |
if (diff > timeSpan) { | |
score += + pointValue; | |
// We are working with a 4 digit display so | |
// if we score more than 9999 points, we | |
// reset the score... | |
if (score > 9999) { | |
score = 0; | |
} | |
// This should add more fun to the game, like an | |
// achievement. If the player has the skills to | |
// make the right amout of points | |
// we increase the number of points one ball scores | |
if (score >= 30) { | |
pointValue = 9; | |
} | |
if (score >= 300) { | |
pointValue = 37; | |
} | |
if (score >= 3000) { | |
pointValue = 73; | |
} | |
previousTime4Score = now; | |
} | |
} | |
previousIrVal = currentIrVal; | |
} | |
void displayScore() { | |
int transition, thousands, hundreds, tens, ones; | |
thousands = int(score / 1000); | |
transition = thousands * 1000; | |
hundreds = int((score - transition) / 100); | |
transition += hundreds * 100; | |
tens = int((score - transition) / 10); | |
transition += tens * 10; | |
ones = score - transition; | |
lc.setDigit(0, 3, (byte) thousands, false); | |
lc.setDigit(0, 2, (byte) hundreds, false); | |
lc.setDigit(0, 1, (byte) tens, false); | |
lc.setDigit(0, 0, (byte) ones, false); | |
} | |
void displayTime() { | |
int transition, tens, ones; | |
if (gameOn == 1) { | |
tens = int(time / 10); | |
transition = tens * 10; | |
ones = time - transition; | |
lc.setDigit(0, 6, (byte) tens, false); | |
lc.setDigit(0, 5, (byte) ones, false); | |
} else { | |
lc.setChar(0, 6, '-', false); | |
lc.setChar(0, 5, '-', false); | |
} | |
lc.setChar(0, 4, '-', false); | |
lc.setChar(0, 7, '-', false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment