#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
static bool enabled = false; // This is the saved status
#define INTERVAL_MESSAGE1 600 //0.6 วินาที
unsigned long time_1 = 0;
int R = 7;
int G = 6;
int B = 5;
int randNumber;
int score ;
int score_max ;
//currentLine is used to record the current position of the track
int currentLine = 3;
// Postion of car
int carPosition = 3;
//Pin connected to Pin 12 of 74HC595 (Latch)
int latchPin = 8;
//Pin connected to Pin 11 of 74HC595 (Clock)
int clockPin = 12;
//Pin connected to Pin 14 of 74HC595 (Data)
int dataPin = 11;
int pin = 3;
#define VERT_PIN A0
#define HORZ_PIN A2
#define SEL_PIN 2
uint8_t led[8];
uint8_t car[8];
uint8_t trackLine[5];
long counter1 = 0;
void setup() {
// Seed Random Generator with noise from analog pin 0
randomSeed(analogRead(0));
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(pin, OUTPUT);
attachInterrupt(1, crash_inter, RISING);
lcd.begin();
Wire.begin();
Serial.begin(9600);
// Set Track to straight
led[0] = B11100011;
led[1] = B11100011;
led[2] = B11100011;
led[3] = B11100011;
led[4] = B11100011;
led[5] = B11100011;
led[6] = B11100011;
led[7] = B11100011;
// Set car position - looks backward, but the display is LSB so a 1
// on the right will show as a 1 on the left of the screen
car[0] = B00000001;
car[1] = B00000010;
car[2] = B00000100;
car[3] = B00001000;
car[4] = B00010000;
car[5] = B00100000;
car[6] = B01000000;
car[7] = B10000000;
// used to set the track decision
trackLine[0] = B10001111;
trackLine[1] = B11000111;
trackLine[2] = B11100011;
trackLine[3] = B11110001;
// trackLine[4] = B00001001;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Game Start");
}
void loop() {
// counter1 used for delay in animation
counter1++;
// set the LEDs
screenUpdate();
int vert = analogRead(VERT_PIN);
if (vert > 895 || vert < 128) {
if (vert > 895) {
if (!enabled) {
enabled = true;
carPosition = carPosition + 1;
}
}
if (vert < 128) {
if (!enabled) {
enabled = true;
carPosition = carPosition - 1;
}
}
}
analogWrite(R, 0);
analogWrite(G, 255);
analogWrite(B, 0);
// Loop for the action
if (counter1 >= 75) {
counter1 = 0;
// Do track
for (int i = 0 ; i < 7 ; i++) {
led[i] = led[i + 1];
}
// check of the car has crashed if it has then do a crash signal
if (led[0] & car[carPosition]) {
crash();
}
if(millis() - time_1 > INTERVAL_MESSAGE1){
time_1 = millis();
enabled = false;}
led[0] = led[0] | car[carPosition];
randNumber = random(3) - 1;
Serial.println(randNumber);
currentLine = currentLine + randNumber;
Serial.print("currentLine");
Serial.println(currentLine);
if (currentLine == 0) {
currentLine = 1;
}
if (currentLine == 5) {
currentLine = 4;
}
led[7] = trackLine[currentLine - 1];
score = score + 1;
}
}
void screenUpdate() {
uint8_t row = B00000001;
for (byte k = 0; k < 9; k++) {
// Open up the latch ready to receive data
digitalWrite(latchPin, LOW);
shiftIt(~row );
shiftIt(led[k] ); // LED array
// Close the latch, sending the data in the registers out to the matrix
digitalWrite(latchPin, HIGH);
row = row << 1;
}
}
void shiftIt(byte dataOut) {
// Shift out 8 bits LSB first,
// on rising edge of clock
boolean pinState;
//clear shift register read for sending data
digitalWrite(dataPin, LOW);
// for each bit in dataOut send out a bit
for (int i = 0; i < 8; i++) {
//set clockPin to LOW prior to sending bit
digitalWrite(clockPin, LOW);
// if the value of DataOut and (logical AND) a bitmask
// are true, set pinState to 1 (HIGH)
if ( dataOut & (1 << i) ) {
pinState = HIGH;
}
else {
pinState = LOW;
}
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
//send bit out on rising edge of clock
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
}
//stop shifting
digitalWrite(clockPin, LOW);
}
void crash() {
// Used to set all bits on or off
int allBits = B11111111;
analogWrite(R, 255);
analogWrite(G, 0);
analogWrite(B, 0);
lcd.setCursor(0, 0);
lcd.print(" Game Over!");
if (score_max <= score){
score_max = score;
}
lcd.setCursor(0, 1);
lcd.print("Score:");
lcd.print(score);
lcd.setCursor(10, 1);
lcd.print("B:");
lcd.print(score_max);
// Do 10 loop so of on and off
for (int show = 0; show < 10; show++) {
for (int i = 0; i < 8; i++) {
led[i] = allBits;
}
for (int delaylots = 0; delaylots < 200; delaylots++) {
screenUpdate();
}
if (allBits == B11111111) {
allBits = B00000000;
}
else
{
allBits = B11111111;
}
}
// Reset the position of the car to the middle of the track
lcd.clear();
carPosition = 3;
score = 0;
for (int i = 0; i < 8; i++) {
led[i] = trackLine[2];
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Game Start");
analogWrite(R, 0);
analogWrite(G, 255);
analogWrite(B, 0);
}
void crash_inter() {
// Used to set all bits on or off
int allBits = B11111111;
// Do 10 loop so of on and off
for (int show = 0; show < 10; show++) {
for (int i = 0; i < 8; i++) {
led[i] = allBits;
}
for (int delaylots = 0; delaylots < 50; delaylots++) {
screenUpdate();
}
if (allBits == B11111111) {
allBits = B00000000;
}
else
{
allBits = B11111111;
}
}
// Reset the position of the car to the middle of the track
score = 0;
carPosition = 3;
analogWrite(R, 255);
analogWrite(G, 0);
analogWrite(B, 0);
for (int i = 0; i < 8; i++) {
led[i] = trackLine[2];
}
}