259 lines
7.6 KiB
C++

//======================================================
// LIBRARIES
//======================================================
#include <Button.h>
#include <ArduinoJson.h>
#include <TimeTrigger.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// MODE SELECTION VARIABLES
int mode;
bool flagChange = false;
// FIXED MESSAGES
#define POWERUP "{\"status\":\"POWER UP\"}"
#define UNDERSTOOD "{\"status\":\"OK\"}"
#define JSON_ERROR "{\"status\":\"ERROR JSON\"}"
#define TARGET_MET "{\"status\":\"TARGET MEET\"}"
#define GAME_OVER "{\"status\":\"GAME OVER\"}"
#define TIME_OVER "{\"status\":\"TIME OVER\"}"
#define UNKNOWN_GAME "{\"status\":\"UNKNOWN GAME\"}"
// GAME NAMES
#define GM_COUNTER "{\"gm\":\"COUNTER\"}"
#define GM_TATTACK "{\"gm\":\"TIME ATTACK\"}"
#define GM_REACTION "{\"gm\":\"REACTION\"}"
#define GM_DECISION "{\"gm\":\"DECISION\"}"
#define GM_STAMINA "{\"gm\":\"STAMINA\"}"
// COMMAND LIST
#define CM_START "{\"cm\":\"START\"}"
#define CM_RESET "{\"cm\":\"RESET\"}"
#define CM_STOP "{\"cm\":\"STOP\"}"
#define CM_EXIT "{\"cm\":\"EXIT\"}"
// JSON JSON_CAPACITY
#define JSON_CAPACITY 500
//======================================================
// HARDWARE SELECTION
//======================================================
#define TERMINAL Serial // SERIAL PORT USED AS TERMINAL WINDO FOR DEBUG
#define VTH 65 // THRESHOLD VOLTAGE FOR ZERO POSITIONNING KICKING POWER SENSOR
#define PWR 65 // MIN THRESHOLD VOLTAGE FROM ZERO POINT TO BE ACCOUNTED AS KICK
#define NUM_PIXELS 22 // NUMBER OF LED'S IN NEO PIXEL STRIP
#define MPU_ADD 0x69 // On Board MPU9250 I2C Address
#define OLED_ADD 0x3C // Connected I2C OLED Address
// Incase You Are Using Arduino MEGA
#ifdef ARDUINO_AVR_MEGA2560
#define BLUETOOTH Serial1
Button button(52); // BUTTON ON PIN 2
#define LED_STRIP 53 // LED STRIP PIN NUMBER
#define SENSOR_PIN A0 // IMPACT SENSOR
#define RANDOM_SOURCE A2 // RANDOM NUMBER GENERATOR SOURCE
// Incase You Are Using Arduino Nano
#elif ARDUINO_AVR_UNO || ARDUINO_AVR_NANO
//----------------------------------------------------------
/* ARDUINO NANO DOESNOT HAVE A SECOND NATIVE
SERIAL PORT SO WE USE SOFTWARE SERIAL LIBRARY */
#include <SoftwareSerial.h>
//----------------------------------------------------------
SoftwareSerial BLUETOOTH(10, 11); // RX, TX
Button button(2); // BUTTON ON PIN 2
#define LED_STRIP 8 // LED STRIP PIN NUMBER
#define SENSOR_PIN A0 // IMPACT SENSOR
#define RANDOM_SOURCE A3 // RANDOM NUMBER GENERATOR SOURCE
// if This Was The ESP32 Core
#elif ESP32
//----------------------------------------------------------
#include <BluetoothSerial.h>
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
//----------------------------------------------------------
// BLUETOOTH DEFINITION
BluetoothSerial BLUETOOTH;
// BUTTONS
Button button(2); // BUTTON ON PIN 2
// PIEZO SENSOR PIN
#define SENSOR_PIN 39 // IMPACT SENSOR
// LED STRIP PIN SET
#define LED_STRIP 26 // LED STRIP PIN NUMBER
#define RANDOM_SOURCE 34 // RANDOM NUMBER GENERATOR SOURCE
// Battery To 5V Level Booster Setup & Control
#define BOOST_PIN 14
#define BOOST_SETUP pinMode(BOOST_PIN, OUTPUT)
#define BOOST_ON digitalWrite(BOOST_PIN, HIGH)
#define BOOST_OFF digitalWrite(BOOST_PIN, LOW)
// Battery Level Monitoring
#define BATTERY (float)(((analogRead(35) * (3.3 / 4096)) * 2) + 0.28)
#define CHARGE mapf(BATTERY, 3.27, 4.31, 0, 100)
// End Of Arduino Selection ARDUINO_MEGA
#endif
// Float Mapping Function
double mapf(double val, double in_min, double in_max, double out_min, double out_max) {
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// NEO PIXEL STRIP
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PIXELS, LED_STRIP, NEO_GRB + NEO_KHZ800);
//======================================================
// DEFINITION & DECLERATIONS
//======================================================
// Serial Flags
#define isBluetooth BLUETOOTH.available()
#define isTERMINAL TERMINAL.available()
//======================================================
// READING IMPACT POWER FROM ANALOG PINS
//======================================================
// READING THE IMPACT
int readImpact()
{
int value = abs(constrain(analogRead(SENSOR_PIN), VTH, 1023));
value = map(value, VTH, 1023, 0, 1023);
return value;
}
//======================================================
// MAIN HARDWARE SETUP
//======================================================
void setup()
{
// Communications
TERMINAL.begin(9600);
// Oled Display
oled_init();
// Make Sure Of The Core
#ifdef ESP32
BOOST_SETUP;
BOOST_OFF;
BLUETOOTH.begin("KICKER"); //Bluetooth device name
#else
BLUETOOTH.begin(9600);
#endif
// I/O
button.begin(); // Button
pixels.begin(); // Neo Pixel
LED_CLEAR(); // Turn Off All LED
// The Random Number Source
randomSeed(analogRead(RANDOM_SOURCE));
// Battery Managment
if(BATTERY <= 3.7) {
// Display Battery Low
dualcomm("Battery Low");
// Display Battery Voltage
while(BATTERY <= 3.7) { Serial.println(BATTERY); drawProgressBarDemo(CHARGE); oled_show(); delay(5000); }
// Clearing The Screen
oled_clear();
oled_show();
}
// POWER UP MESSGAE
dualcomm(POWERUP);
// TURN BOOSTER ON
BOOST_ON;
// Blue, Red And Green
colorWipe(pixels.Color(0, 0, 255), 15); // Blue
delay(100);
colorWipe(pixels.Color(255, 0, 0), 15); // Red
delay(100);
colorWipe(pixels.Color(0, 255, 0), 15); // Green
// Delay 1 Sec
delay(500);
// Red FadeOut
LED_GREEN_FADEOUT();
// Display Rainbow
LED_CLEAR();
// TURN BOOSTER ON
BOOST_OFF;
}
//======================================================
// MAIN PROGRAM LOOP
//======================================================
void loop()
{
// Reading From Bluetooth
if(BLUETOOTH.available())
{
// Reading Message From Bluetooth
String msg = BLUETOOTH.readString();
msg.trim();
// Display On Sertial
terminal(msg);
// Parsing Objects Message Objects
DynamicJsonDocument root(JSON_CAPACITY);
// Deserialize the JSON document
DeserializationError error = deserializeJson(root, msg);
// If The System Understood The Message Clearly
if(!error)
{
// Sending Status Message
dualcomm(UNDERSTOOD);
// Reading Game Name & Settings
String game = root["gm"];
String settings = root["set"];
// Clearing JSON Buffer
root.clear();
// Entering To Game Selector
game_selector(game, settings);
}
else
{
// Display The Error Message
terminal(JSON_ERROR);
}
// Clearing Buffer
BLUETOOTH.flush();
}
}
//======================================================
// END OF PROGRAM
//======================================================