/*
#define NOM_Local "HAPO SENSOR - 0000" //Nom du produit + #Numéro de série
#include <ArduinoBLE.h>
//Creation du service Bluetooth Low Energy (BLE)
BLEService Service_HAPO_SENSOR("fff0");
BLEByteCharacteristic CharacteristicAPP("fff1", BLERead | BLEWrite); //0 = pas d'analyse, 1 = calibrate , 2 = start, 3 = Analyse, 4 = stop
BLEStringCharacteristic CharacteristicSD("fff2", BLERead | BLEWrite | BLENotify, 180); //Pour envoyer le contenu de l'analyse
BLEStringCharacteristic CharacteristicDATE("fff3", BLERead | BLEWrite, 180); //Date et heure + Type de HAPO et size + CALIB
BLEStringCharacteristic CharacteristicCALIB("fff4", BLERead | BLEWrite | BLENotify, 180); //val_FlexMIN_FlexMAX
//APP_ DATE _modele _size_min _max _ millis0 _ millis1 _nbFlex_ mass _ % _ nbL1 _ nbL2 _ nbL3 _%L1_%L2_%L3
//0_AAAA-MM-JJ_hh:mm:ss_HAPOxxxxxx_T_MINx_MAXx_000000000_000000000_000000_0000000_000_000000_000000_000000_000_000_000
//===== POUR L'EXEMPLE =====
//String data = "0_2002-01-20_06:15:33_HAPOxxxxxx_2_0000_1022_000004700_000084700_000015_0000086_074_000015_000006_000001_024_009_041";
//20 janv 2002 à 6h15 et 33sec le HAPO S2 entre 0 et 1022 démarre à 4700ms et termine à 84700ms
//donc l'analyse a duré 1min20 et effectué 15 flexions soulagé une masse de 86kg pour un taux d'utilisation de 74%.
//Pour les niveaux de flexion : 15 petites flexions, 6 moyennes flexions et 1 grosses flexions
//pour un taux d'utilisation de 24% faible, 9% moyenne et 41% forte
*/
//========== CREATION DES VARIABLES ==========
//Communication BLE
byte appMODE = 0;
String CALIB = "X";
String DATEandType = "X", date = "X", type = "X", size = "X";
//Capteur
unsigned short val = 0, val_brute = 0;
unsigned short FlexMIN = 1025, FlexMAX = 0;
unsigned short NivL1 = 0, NivL2 = 0, NivL3 = 0, FlexNiv = 1025;
//Rapport
unsigned int nbL1 = 0, nbL2 = 0, nbL3 = 0, nbFlexion = 0;
unsigned int previousZone = 0, Zone = 0, memZone = 0;
bool memSecu = 0;
unsigned int mass = 0;
//Temps et %
float TimeL0 = 0, TimeL1 = 0, TimeL2 = 0, TimeL3 = 0, TIMETotal = 0;
float PercentL0 = 0, PercentL1 = 0, PercentL2 = 0, PercentL3 = 0, PercentActif = 0;
//Fonction smooth
const byte smooth_nbVal = 20;
unsigned short smooth_i = 0, smooth_total = 0;
unsigned short smooth_read[smooth_nbVal];
//Enregistrement
unsigned long millis0 = 0, millis1 = 0;
String data = "X";
//============================================
void setup() {
/*
//Initialisation BLE
if (!BLE.begin()) {
while (1)
;
}
BLE.setLocalName(NOM_Local);
BLE.setAdvertisedService(Service_HAPO_SENSOR);
Service_HAPO_SENSOR.addCharacteristic(CharacteristicAPP);
Service_HAPO_SENSOR.addCharacteristic(CharacteristicSD);
Service_HAPO_SENSOR.addCharacteristic(CharacteristicDATE);
Service_HAPO_SENSOR.addCharacteristic(CharacteristicCALIB);
BLE.addService(Service_HAPO_SENSOR);
CharacteristicAPP.writeValue(0);
CharacteristicSD.writeValue("X");
CharacteristicDATE.writeValue("X");
CharacteristicCALIB.writeValue("X");
BLE.advertise();
//Initialisation LEDRGB
pinMode(LEDR, OUTPUT);
pinMode(LEDG, OUTPUT);
pinMode(LEDB, OUTPUT);
digitalWrite(LEDR, HIGH);
digitalWrite(LEDG, HIGH);
digitalWrite(LEDB, HIGH);
*/
//Initialisation capteur
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(13, OUTPUT);
}
void CompteurFlexions() {
//Zone0
if (val < NivL1) {
TimeL0++;
Zone = 0;
}
//Zone1
else if (val < NivL2) {
TimeL1++;
Zone = 1;
}
//Zone2
else if (val < NivL3) {
TimeL2++;
Zone = 2;
}
//Zone3
else {
TimeL3++;
Zone = 3;
}
if ((Zone < previousZone) && (memSecu == 0)) {
memSecu = 1;
if (((Zone + 1) - memZone) == 1) {
nbL1++;
} else if (((Zone + 1) - memZone) == 2) {
nbL2++;
} else if (((Zone + 1) - memZone) == 3) {
nbL3++;
}
}
if ((Zone > previousZone) && (memSecu == 1)) {
memSecu = 0;
memZone = Zone - 1;
}
previousZone = Zone;
nbFlexion = nbL1 + nbL2 + nbL3;
mass = (nbL1 * 4 + nbL2 * 8 + nbL3 * 12);
TIMETotal = TimeL0 + TimeL1 + TimeL2 + TimeL3;
PercentL0 = (TimeL0 / TIMETotal) * 100;
PercentL1 = (TimeL1 / TIMETotal) * 100;
PercentL2 = (TimeL2 / TIMETotal) * 100;
PercentL3 = (TimeL3 / TIMETotal) * 100;
PercentActif = PercentL1 + PercentL2 + PercentL3;
millis1 = millis();
}
//Fonction lissage de courbe
unsigned short smooth(unsigned short smooth_VAL) {
unsigned short average;
smooth_total = smooth_total - smooth_read[smooth_i];
smooth_read[smooth_i] = smooth_VAL;
smooth_total = smooth_total + smooth_read[smooth_i];
smooth_i = smooth_i + 1;
if (smooth_i >= smooth_nbVal) {
smooth_i = 0;
}
average = smooth_total / smooth_nbVal;
return average;
}
String completeString(String text, unsigned short width) {
unsigned short size_text = text.length();
while (size_text < width) {
text = text + "x";
size_text = text.length();
}
return text;
}
String completeInt(unsigned int integer, unsigned short width) {
String intString = String(integer);
while (intString.length() < width) {
intString = "0" + intString;
}
return intString;
}
/*
void LedRGB(unsigned short redValue, unsigned short greenValue, unsigned short blueValue) {
digitalWrite(LEDR, redValue);
digitalWrite(LEDG, greenValue);
digitalWrite(LEDB, blueValue);
}
*/
void loop() {
//digitalWrite(13, HIGH);
//Capteur
val_brute = analogRead(A0); //Acquisition de la valeur du capteur
val = smooth(val_brute);
//Serial.println(val_brute);
//Analyse en cours...
if (appMODE == 3) {
CompteurFlexions();
}
/*
//BLE connecté
if (BLE.central()) {}
*/
//appMODE = CharacteristicAPP.value(); //Acquisition du mode par BLE
if (digitalRead(2)) {
appMODE = 0;
Serial.println("0_ok!");
}
if (digitalRead(3)) {
appMODE = 1;
Serial.println("1_ok!");
}
if (digitalRead(4)) {
appMODE = 2;
Serial.println("2_ok!");
}
if (digitalRead(5)) {
appMODE = 3;
Serial.println("3_ok!");
}
if (digitalRead(6)) {
appMODE = 4;
Serial.println("4_ok!");
}
//===== Pas d'analyse =====
if (appMODE == 0) {
//LedRGB(255, 255, 255);
FlexMIN = 1025;
FlexMAX = 0;
}
//===== Calibrate =====
else if (appMODE == 1) {
//LedRGB(255, 0, 255);
if (val < FlexMIN) {
FlexMIN = val;
}
if (val > FlexMAX) {
FlexMAX = val;
}
CALIB = completeInt(val, 4) + "_" + completeInt(FlexMIN, 4) + "_" + completeInt(FlexMAX, 4);
NivL1 = FlexMIN + (FlexMAX - FlexMIN) * 1 / 4;
NivL2 = FlexMIN + (FlexMAX - FlexMIN) * 1 / 2;
NivL3 = FlexMIN + (FlexMAX - FlexMIN) * 3 / 4;
FlexNiv = NivL1;
//CharacteristicCALIB.writeValue(CALIB);
}
//===== Start =====
else if (appMODE == 2) {
//DATEandType = CharacteristicDATE.value();
DATEandType = "DATEandType";
millis0 = millis();
date = DATEandType.substring(0, 19);
type = DATEandType.substring(20, 30);
size = DATEandType.substring(31, 32);
//Rapport
nbL1 = 0;
nbL2 = 0;
nbL3 = 0;
nbFlexion = 0;
previousZone = 0;
Zone = 0;
memZone = 0;
mass = 0;
//Temps et %
TimeL0 = 0;
TimeL1 = 0;
TimeL2 = 0;
TimeL3 = 0;
TIMETotal = 0;
PercentL0 = 0;
PercentL1 = 0;
PercentL2 = 0;
PercentL3 = 0;
PercentActif = 0;
//CharacteristicAPP.writeValue(3);
appMODE = 3;
}
//===== Analyse =====
else if (appMODE == 3) {
//LedRGB(255, 255, 0);
//data = "3_" + DATEandType + "_" + completeInt(millis0, 9) + "_" + completeInt(millis1, 9) + "_" + completeInt(nbFlexion, 6) + "_" + completeInt(mass, 7) + "_" + completeInt(PercentActif, 3) + "_" + completeInt(nbL1, 6) + "_" + completeInt(nbL2, 6) + "_" + completeInt(nbL3, 6) + "_" + completeInt(PercentL1, 3) + "_" + completeInt(PercentL2, 3) + "_" + completeInt(PercentL3, 3) + "_" + completeInt(val, 4);
//CharacteristicSD.writeValue(data);
//Serial.println(data);
//Serial.println(val);
data = completeInt(nbL1, 6) + "_" + completeInt(nbL2, 6) + "_" + completeInt(nbL3, 6) + "_" + completeInt(val, 4)+ "_" + completeInt(mass, 7);
Serial.println(data);
}
//===== Stop =====
else if (appMODE == 4) {
//LedRGB(0, 255, 255);
//Capteur
val = 0;
val_brute = 0;
FlexMIN = 1025;
FlexMAX = 0;
NivL1 = 0;
NivL2 = 0;
NivL3 = 0;
FlexNiv = 1025;
//Enregistrement
millis0 = 0;
millis1 = 0;
delay(100);
//CharacteristicAPP.writeValue(0);
appMODE = 0;
}
delay(10);
}