0% found this document useful (0 votes)
50 views7 pages

Arduino Two-Player Game Code

Atmel PlatformIO code for table soccer game test

Uploaded by

rnd.programmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

Arduino Two-Player Game Code

Atmel PlatformIO code for table soccer game test

Uploaded by

rnd.programmer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <EmSevenSegment.

h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include <DFMiniMp3.h>
//#include <FastLED.h>
//#include <SoftwareSerial.h>
//#include <DFMiniMp3.h>

# define credit A2 //credit


# define button A3 //start button
# define sensor1 4 //player 1 ir sensor
# define sensor2 5//player 2 ir sensor

# define relay1 13 //motor


# define relay2 11 //motor

int Time = 0;
int Score1 = 0;
int Score2 = 0;
boolean state1, state2 = false;

int dataPin1 = 26;


int clockPin1 = 27;
int strobePin1 = 28;

int dataPin2 = 29;


int clockPin2 = 30;
int strobePin2 = 31;

int dataPin3 = 32;


int clockPin3 = 33;
int strobePin3 = 34;
byte segChar[] = {63, 6, 91, 79, 102, 109, 125, 7, 127, 111};

int creditState = 0;
int buttonState = 0;
int sensor1value = 0;
int sensor2value = 0;

int flag1 = 0;
int song = 1;
int player = 0;

const int Motorcontrol = A0;//driver connection


int value = 0;

const unsigned long eventIntervalue = 1000;


unsigned long previousTime = 0;
unsigned long songtime = 0;

int Player1count = 0;

int Player2count = 0;

class Mp3Notify
{
public:
static void PrintlnSourceAction(DfMp3_PlaySources source, const char* action)
{
if (source & DfMp3_PlaySources_Sd)
{
[Link]("SD Card, ");
}
if (source & DfMp3_PlaySources_Usb)
{
[Link]("USB Disk, ");
}
if (source & DfMp3_PlaySources_Flash)
{
[Link]("Flash, ");
}
[Link](action);
}
static void OnError(uint16_t errorCode)
{
// see DfMp3_Error for code meaning
[Link]();
[Link]("Com Error ");
[Link](errorCode);
}
static void OnPlayFinished(DfMp3_PlaySources source, uint16_t track)
{
[Link]("Play finished for #");
[Link](track);
}
static void OnPlaySourceOnline(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, "online");
}
static void OnPlaySourceInserted(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, "inserted");
}
static void OnPlaySourceRemoved(DfMp3_PlaySources source)
{
PrintlnSourceAction(source, "removed");
}
};

// instance a DFMiniMp3 object,


// defined with the above notification class and the hardware serial class
//
//DFMiniMp3<HardwareSerial, Mp3Notify> mp3(Serial3);
SoftwareSerial secondarySerial(15, 14); // RX, TX
DFMiniMp3<SoftwareSerial, Mp3Notify> mp3(secondarySerial);

void setup() {
// put your setup code here, to run once:
[Link](9600);
pinMode(credit, INPUT_PULLUP);
pinMode(button, INPUT_PULLUP);
pinMode(sensor1, INPUT_PULLUP);
pinMode(sensor2, INPUT_PULLUP);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(strobePin1, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(dataPin1, OUTPUT);
pinMode(strobePin2, OUTPUT);
pinMode(clockPin2, OUTPUT);
pinMode(dataPin2, OUTPUT);
pinMode(strobePin3, OUTPUT);
pinMode(clockPin3, OUTPUT);
pinMode(dataPin3, OUTPUT);

digitalWrite(relay1, 1);
digitalWrite(relay2, 0);
delay(2000);
digitalWrite(relay2, 1);
digitalWrite(relay1, 0);
delay(2000);
digitalWrite(relay1, 1);
digitalWrite(relay2, 1);
delay(100);
[Link]("initializing...");

[Link]();

uint16_t volume = [Link]();


[Link]("volume ");
[Link](volume);
[Link](18);

uint16_t count = [Link](DfMp3_PlaySource_Sd);


[Link]("files ");
[Link](count);

[Link]("starting...");

void loop() {
// put your main code here, to run repeatedly:

creditState = analogRead(credit);
buttonState = digitalRead(button);
sensor1value = digitalRead(sensor1);
sensor2value = digitalRead(sensor2);
//[Link](creditState);

//_____this is when the game is standby


if (flag1 == 0) {
if (song == 1) {
//mp3_play(3);
mp3.playMp3FolderTrack(2);
delay(100);
song = 0;
}
// mp3.playMp3FolderTrack(1);
if (millis() - songtime > 50000) {
song = 1;
songtime = millis();
}
Time = 0;
Score1 = 0;
Score2 = 0;
dispTime(Time);
dispScore1(Score1);
dispScore2(Score2);

//[Link](105, HighScore);
}

//_______this is when credit is applied and checks for 2 player

if (creditState <= 50 && flag1 == 0) {


[Link]("1 player game");
flag1 = 1;
// player = player + 1;
// delay(200);
//
// if (player >= 2) {
// [Link]("2 player game");
// flag1 = 1;
// delay(200);
// }
}

//______this is whn the start button is pressed

if ( flag1 == 1) {
mp3.playMp3FolderTrack(1);
[Link]("Start");
delay(200);
for (Time = 3; Time >= 0; Time--) {

[Link](Time);
dispTime(Time);
delay(1000);
}
Time = 61;
flag1 = 2;
Score1 = 0;
Score2 = 0;
mp3.playMp3FolderTrack(3);

digitalWrite(relay1, 1);
digitalWrite(relay2, 0);
delay(2500);
digitalWrite(relay2, 1);
digitalWrite(relay1, 0);
delay(2500);
digitalWrite(relay1, 1);
digitalWrite(relay2, 1);
delay(100);

digitalWrite(relay1, 0); //motor on

dispTime(Time);
dispScore1(Score1);
dispScore2(Score2);
}
//______this is the main game play of 120 secs

if (Time >= 0 && flag1 == 2) {


dispScore1(Score1);
dispScore2(Score2);

sensor1value = digitalRead(sensor1); //player1


sensor2value = digitalRead(sensor2); //player2

//__if any 1 sensor cuts score is updated on opponents dispaly


/*if (sensor1value == 0 && state1) { // sensor 1
Score2 = 1 + Score2;
dispScore2(Score2);
delay(200);
[Link](Score2);
state1=false;
}
if (sensor2value == 1){
state1 = true;
//delay(100);
}

if (sensor2value == 0 && state2) { // sensor 2


Score1 = 1 + Score1;
dispScore1(Score1);
delay(200);
[Link](Score1);
state2=false;
}
if (sensor2value == 1){
state2 = true;
//delay(100);
}*/
//___this is ur code uncomment if mine is not working and comment mine

if (!digitalRead(sensor1) && state1) {


Score2 = 1 + Score2;
//Player1count++;
state1 = false;
dispScore2(Score2);
[Link]("Count1: ");
[Link](Score2);

delay(200);
}

if (digitalRead(sensor1)) {
state1 = true;
//delay(100);
}

if (!digitalRead(sensor2) && state2) {


Score1 = 1 + Score1;
//Player2count++;
state2 = false;
dispScore1(Score1);
[Link]("Count2: ");
[Link](Score1);
delay(200);
}

if (digitalRead(sensor2)) {
state2 = true;
//delay(100);
}

//____ Timer part


unsigned long currentTime = millis();
if ( currentTime - previousTime >= eventIntervalue) {
// [Link](currentTime / 1000);
previousTime = currentTime;
[Link]("Time : ");
[Link](Time);
[Link](" sec");
Time--;
dispTime(Time);
}

//____this is ur motor code do the changes

value = 120 - Time;


//[Link]( value);
if (value % 10 == 0 ) {

analogWrite(Motorcontrol, 255);
//delay(10);
analogWrite(Motorcontrol, 120 - value);
[Link]("Speed is ");
[Link](120 - value);

//-----this is when the game ends and send back to standby


if (Time <= 0) {

digitalWrite(relay1, 1); //motor off


// delay(15000);
dispTime(000);
dispScore1(Score1);
dispScore2(Score2);
delay(15000);
song = 1;
flag1 = 0;
player = 0;

Score1 = 0;
Score2 = 0;

[Link]("Game over");
}
}

void dispTime(int Time) {


int t1 = Time % 10;
int t2 = (Time / 10) % 10;
int t3 = (Time / 100) % 10;

digitalWrite(strobePin1, LOW);
shiftOut(dataPin1, clockPin1, MSBFIRST, segChar[t1]);
shiftOut(dataPin1, clockPin1, MSBFIRST, segChar[t2]);
shiftOut(dataPin1, clockPin1, MSBFIRST, segChar[t3]);
//set strobe pin high to stop storing bits
digitalWrite(strobePin1, HIGH);

void dispScore1(int Score1) {


int a1 = Score1 % 10;
int a2 = (Score1 / 10) % 10;
int a3 = (Score1 / 100) % 10;

digitalWrite(strobePin2, LOW);
shiftOut(dataPin2, clockPin2, MSBFIRST, segChar[a1]);
shiftOut(dataPin2, clockPin2, MSBFIRST, segChar[a2]);
shiftOut(dataPin2, clockPin2, MSBFIRST, segChar[a3]);
//set strobe pin high to stop storing bits
digitalWrite(strobePin2, HIGH);

void dispScore2(int Score2) {


int b1 = Score2 % 10;
int b2 = (Score2 / 10) % 10;
int b3 = (Score2 / 100) % 10;

digitalWrite(strobePin3, LOW);
shiftOut(dataPin3, clockPin3, MSBFIRST, segChar[b1]);
shiftOut(dataPin3, clockPin3, MSBFIRST, segChar[b2]);
shiftOut(dataPin3, clockPin3, MSBFIRST, segChar[b3]);
//set strobe pin high to stop storing bits
digitalWrite(strobePin3, HIGH);

You might also like