210 lines
6.2 KiB
C++
210 lines
6.2 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 CAPACITY
|
|
#define capacity 250
|
|
|
|
//======================================================
|
|
// 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 24 // NUMBER OF LED'S IN NEO PIXEL STRIP
|
|
|
|
// 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 A7 // 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 A1 // LED STRIP PIN NUMBER
|
|
#define SENSOR_PIN A0 // IMPACT SENSOR
|
|
#define RANDOM_SOURCE A4 // 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
|
|
//----------------------------------------------------------
|
|
BluetoothSerial Bluetooth;
|
|
Button button(2); // BUTTON ON PIN 2
|
|
#define LED_STRIP 33 // LED STRIP PIN NUMBER
|
|
#define SENSOR_PIN 39 // IMPACT SENSOR
|
|
#define RANDOM_SOURCE 34 // RANDOM NUMBER GENERATOR SOURCE
|
|
|
|
// End Of Arduino Selection ARDUINO_MEGA
|
|
#endif
|
|
|
|
// 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);
|
|
|
|
// Make Sure Of The Core
|
|
#ifdef ESP32
|
|
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));
|
|
|
|
// POWER UP MESSGAE
|
|
dualcomm(POWERUP);
|
|
|
|
// Display Color Wipe
|
|
colorWipe(pixels.Color(255, 255, 255), 15); // White
|
|
delay(100);
|
|
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();
|
|
}
|
|
|
|
//======================================================
|
|
// 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(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
|
|
{
|
|
terminal(JSON_ERROR);
|
|
}
|
|
Bluetooth.flush();
|
|
}
|
|
}
|
|
|
|
//======================================================
|
|
// END OF PROGRAM
|
|
//======================================================
|