Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Commit

Permalink
Started modularizing each component for easier debugging
Browse files Browse the repository at this point in the history
- Removed 'Arduino.h' lib
- Added documentation
  • Loading branch information
garrettsummerfi3ld committed Feb 8, 2021
1 parent 3a288b6 commit a035cbf
Showing 1 changed file with 108 additions and 63 deletions.
171 changes: 108 additions & 63 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#include <Arduino.h>
/**
* @file main.cpp
* @author Garrett Summerfield (garrettsummerfi3ld@gmail.com)
* @brief Button game element for 2020-2021 robotics competition
* @version 0.1
* @date 2021-02-07
*
*/

#include <LiquidCrystal.h>
// Set button values
int button1 = 8, button2 = 9, resetButton = 10;
int redButton = 8, blueButton = 9, resetButton = 10;

// Scoring vars
int redTeamCount = 0, blueTeamCount = 0;

// Delay vars
// Delay and time vars
unsigned long timerDelay = 10000;
unsigned long currentTime;

// Team Delay vars
unsigned long redTeamLastPressTime = 0, blueTeamLastPressTime = 0;
Expand All @@ -18,7 +27,10 @@ const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
// Set LCD values
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Initialization
/**
* @brief Arduino setup for game input
*
*/
void setup()
{
// Sets baud rate to communicate to computer for any messages, used for debug
Expand All @@ -28,30 +40,92 @@ void setup()
lcd.begin(16, 2);

// Set button modes
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(redButton, INPUT);
pinMode(blueButton, INPUT);
pinMode(resetButton, INPUT);

Serial.println("[INFO] Started!");
}

// Main game logic
//
// Each time a button is pressed, it will run the desired commands for which
// button is assigned. There are two main buttons that are used for scoring in
// the game. When a scoring button is pressed, it will add a single point to the
// teams score, however there is a penalty if you press the button too early,
// and it will deduct a point from the team. There is a ten second delay on when
// the button will be active to press.
/**
* @brief Main game logic
*
* Each time a button is pressed, it will run the desired commands for which
* button is assigned. There are two main buttons that are used for scoring in
* the game. When a scoring button is pressed, it will add a single point to the
* teams score, however there is a penalty if you press the button too early,
* and it will deduct a point from the team. There is a ten second delay on when
* the button will be active to press.
*
*/
void loop()
{

// Gets current program time in milliseconds
unsigned long currentTime = millis();
currentTime = millis();

// Red Team
if (digitalRead(redButton) == HIGH)
{
teamAction(redButton);
}

// Blue Team
if (digitalRead(blueButton) == HIGH)
{
teamAction(blueButton);
}

// Red Team Score logic
if (digitalRead(button1) == HIGH)
// Reset Score logic
if (digitalRead(resetButton) == HIGH)
{
resetAction();
debug();
}

displayUpdate();
}

/**
* @brief Updates Display
*
*/
void displayUpdate()
{
// Main Scoreboard
lcd.setCursor(0, 0);
lcd.print("RED TEAM: " + String(redTeamCount));
lcd.setCursor(0, 1);
lcd.print("BLUE TEAM: " + String(blueTeamCount));
}

/**
* @brief Reset action
*
*/
void resetAction()
{
// Clear scoreboard
lcd.clear();

// Set vars to zero (0)
redTeamCount = 0;
blueTeamCount = 0;

// Output "RESET SCORES" to end user and serial bridge
lcd.setCursor(0, 0);
lcd.print("RESET SCORES");
Serial.println("[INFO] Reset Scores!");
delay(2500);
}

/**
* @brief Actions per each button press
*
* @param button
*/
void teamAction(int team)
{
if (team == redButton) {
Serial.println("[INFO] RED Team button pressed!");
// Give points to a team for pressing the button at the right time
if (currentTime - redTeamLastPressTime >= timerDelay)
Expand All @@ -77,17 +151,10 @@ void loop()
delay(150);
Serial.println("[INFO] RED Team score lost!");
}

// Debug Statements, only available through the serial connection
Serial.println("[DEBUG] currentTime:" + String(currentTime));
Serial.println("[DEBUG] redTeamLastPressTime:" + String(redTeamLastPressTime));
Serial.println("[DEBUG] blueTeamLastPressTime:" + String(blueTeamLastPressTime));
debug();
}


// Blue Team Score logic
if (digitalRead(button2) == HIGH)
{
if (team == blueButton) {
Serial.println("[INFO] BLUE Team button pressed!");
// Give points to a team for pressing the button at the right time
if (currentTime - blueTeamLastPressTime >= timerDelay)
Expand All @@ -101,48 +168,26 @@ void loop()
{
blueTeamCount++;
blueTeamLastPressTime = currentTime;
Serial.println("[INFO] BLUE Team score added! TIMER RESET");
Serial.println("[INFO] BLUE Team score added!");
delay(150);
}
// Deduct points from team for pressing too early
// Reset timer from team for pressing too early
else
{
blueTeamCount--;
Serial.println("[INFO] BLUE Team score lost!");
Serial.println("[INFO] BLUE Team timer reset!");
delay(150);
}

// Debug Statements, only available through the serial connection
Serial.println("[DEBUG] currentTime:" + String(currentTime));
Serial.println("[DEBUG] redTeamLastPressTime:" + String(redTeamLastPressTime));
Serial.println("[DEBUG] blueTeamLastPressTime:" + String(blueTeamLastPressTime));
}

// Reset Score logic
if (digitalRead(resetButton) == HIGH)
{
// Clear scoreboard
lcd.clear();

// Set vars to zero (0)
redTeamCount = 0;
blueTeamCount = 0;

// Output "RESET SCORES" to end user and serial bridge
lcd.setCursor(0, 0);
lcd.print("RESET SCORES");
Serial.println("[INFO] Reset Scores!");
delay(2500);

// Debug Statements, only available through the serial connection
Serial.println("[DEBUG] currentTime:" + String(currentTime));
Serial.println("[DEBUG] redTeamLastPressTime:" + String(redTeamLastPressTime));
Serial.println("[DEBUG] blueTeamLastPressTime:" + String(blueTeamLastPressTime));
debug();
}
}

// Main Scoreboard
lcd.setCursor(0, 0);
lcd.print("RED TEAM: " + String(redTeamCount));
lcd.setCursor(0, 1);
lcd.print("BLUE TEAM: " + String(blueTeamCount));
/**
* @brief Debug prompts
*
*/
void debug()
{
Serial.println("[DEBUG] currentTime:" + String(currentTime));
Serial.println("[DEBUG] redTeamLastPressTime:" + String(redTeamLastPressTime));
Serial.println("[DEBUG] blueTeamLastPressTime:" + String(blueTeamLastPressTime));
}

0 comments on commit a035cbf

Please sign in to comment.