this is the current code
#include <SoftwareSerial.h>
//*****Hardware settings*****
//BT module
const unsigned long BT_BAUD_RATE = 9600; //BT module baud speed
const int BLUETOOTH_TX = 16;
const int BLUETOOTH_RX = 17;
const int READ_TIME = 500; //BT read time
//Gallagher connection
const unsigned long GAL_BAUD_RATE = 9600;
//IO pins mapped to relays
#define relay_1 36
#define relay_2 34
#define relay_3 32
#define relay_4 30
#define relay_5 28
#define relay_6 26
#define relay_7 24
#define relay_8 22
#define main_gate_sensor 13 //output is 1 when sensor is NOT activated, 0 when sensor is ACTIVATED
#define enable_drafting_gallagher_sensor 10 //output 1 when switch is NOT activated, 0 when switch is ACTIVATED
//*****Software******
//Minimal weight to close main gate
#define min_weight 4.0 //15.0
//Weight of empty scale (to detect no animal left)
#define empty_weight 4.0
//Main gate sensor timeout
//Max time for the main gate to close (in miliseconds)
#define main_gate_timeout 2000
//Time the gate should briefly open when a animal gets stuck (in miliseconds)
#define animal_stuck_time 500
//Wait time to open main gate after all gates are closed (in miliseconds)
#define open_main_gate_delay 500
//Wait time between no animal on scale and closing all gates (in miliseconds)
#define close_exit_gate_delay 250
//Gates mapped to relays
#define main_gate relay_1
#define sorting_gate_1 relay_2
#define sorting_gate_2 relay_3
#define sorting_gate_3 relay_4
//#define sorting_gate_4 relay_5
//#define sorting_gate_5 relay_6
//#define sorting_gate_6 relay_7
//#define sorting_gate_7 relay_8
//State machine settings
enum State_enum {INIT, WAITING, WEIGH, DRAFTER};
uint8_t state = INIT;
int exit_open = 0;
//BT
#define BT_LEN 14
char inBT[BT_LEN];
boolean newBTData = false;
//Gallagher
#define GAL_LEN 4
char inGAL[GAL_LEN];
boolean newGALData = false;
SoftwareSerial bt(BLUETOOTH_RX, BLUETOOTH_TX);
void setup() {
//Setup serial ports
Serial2.begin(BT_BAUD_RATE);
Serial1.begin(GAL_BAUD_RATE, SERIAL_8N1);
Serial.begin(9600);
Serial.println("Init OK");
pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(relay_3, OUTPUT);
pinMode(relay_4, OUTPUT);
pinMode(relay_5, OUTPUT);
pinMode(relay_6, OUTPUT);
pinMode(relay_7, OUTPUT);
pinMode(relay_8, OUTPUT);
pinMode(main_gate_sensor, INPUT_PULLUP);
pinMode(enable_drafting_gallagher_sensor, INPUT_PULLUP);
}
void loop() {
state_machine();
delay(100);
}
void state_machine()
{
switch (state)
{
case INIT:
{
//STARTUP state
//Wait for Gallagher to be connected
Serial.println("STATE = INIT"); //for debug
//ONLY main gate is open
digitalWrite(main_gate, LOW); //Init -> main door is closed
digitalWrite(relay_2, LOW);
digitalWrite(relay_3, LOW);
digitalWrite(relay_4, LOW);
digitalWrite(relay_5, LOW);
digitalWrite(relay_6, LOW);
digitalWrite(relay_7, LOW);
digitalWrite(relay_8, LOW);
delay(100);
if (Serial1.available() > 0) {
Serial.println("Gallagher CONNECTED");
state = WAITING;
}
if (Serial2.available()) {
if (process_bluetooth() == 1) {
inBT[0] = '\0';
exit_open = 1;
}
}
break;
}
case WAITING:
{
//Open MAIN GATE for animals to walk in
digitalWrite(main_gate, HIGH);
//Serial.println("STATE = WAITING"); //for debug
if (read_Gallagher_weight() > min_weight) {
close_main_gate();
state = WEIGH;
}
if (Serial2.available()) {
if (process_bluetooth() == 1) {
inBT[0] = '\0';
exit_open = 1;
}
}
break;
}
case WEIGH:
{
// Serial.println("STATE = WEIGH"); //for debug
// Serial.println ("state = WAITING"), //for sorting commands
// drafting by bluetooth
Serial.println(exit_open);
if ((digitalRead(enable_drafting_gallagher_sensor) == 0) && !exit_open) {
recvGallagher();
if (newGALData == true) {
Serial.println(inGAL);
if (strcmp(inGAL, "W1") == 0) {
digitalWrite(sorting_gate_1, HIGH);
exit_open = 1;
} else if (strcmp(inGAL, "W2") == 0) {
digitalWrite(sorting_gate_2, HIGH);
exit_open = 1;
} else if (strcmp(inGAL, "W3") == 0) {
digitalWrite(sorting_gate_3, HIGH);
exit_open = 1;
}
inGAL[0] = '\0';
newGALData = false;
}
}
if (Serial2.available()) {
if (process_bluetooth() == 1) {
inBT[0] = '\0';
exit_open = 1;
}
}
if (exit_open == 1) {
// Serial.print("Poids Mesuré :");
// Serial.println(read_Gallagher_weight());
if (read_Gallagher_weight() < empty_weight) {
delay(close_exit_gate_delay);
close_all_gates();
delay(open_main_gate_delay);
state = WAITING;
}
}
break;
}
}
}
float read_Gallagher_weight() {
float weight = 0.0;
if (Serial1.available() > 0) {
weight = Serial1.parseFloat();
}
return weight;
}
void recvBTWithMarkers()
{
static boolean RecvInProgress = false;
static byte ndx = 0; // index
char StartMarker = 'r';
char EndMarker = '\r';
char rc; // received data
while (Serial2.available() > 0 && newBTData == false)
{
rc = Serial2.read(); // test for received data
if (RecvInProgress == true)
{ // found some!!
if (rc != EndMarker) //
{
inBT[ndx] = rc; // 1st array position=data
ndx++; // next index
if (ndx >= BT_LEN) // if index>= number of chars
{
ndx = BT_LEN - 1; // index -1
}
}
else // end marker found
{
inBT[ndx] = '\0'; // terminate the string
RecvInProgress = false;
ndx = 0; // reset index
newBTData = true; // new data received flag
}
}
else if (rc == StartMarker) // signal start of new data
{
inBT[ndx++] = rc; // 1st array position=data
RecvInProgress = true;
}
}
}
void recvGallagher()
{
static boolean RecvInProgress = false;
static byte ndx = 0; // index
char StartMarker = 'W';
char EndMarker = '\r';
char rc; // received data
while (Serial1.available() > 0 && newGALData == false)
{
rc = Serial1.read(); // test for received data
if (RecvInProgress == true)
{ // found some!!
if (rc != EndMarker) //
{
inGAL[ndx] = rc; // 1st array position=data
ndx++; // next index
if (ndx >= GAL_LEN) // if index>= number of chars
{
ndx = GAL_LEN - 1; // index -1
}
}
else // end marker found
{
inGAL[ndx] = '\0'; // terminate the string
Serial.println(inGAL);
RecvInProgress = false;
ndx = 0; // reset index
newGALData = true; // new data received flag
}
}
else if (rc == StartMarker) // signal start of new data
{
inGAL[ndx++] = rc; // 1st array position=data
RecvInProgress = true;
}
}
}
int process_bluetooth() {
recvBTWithMarkers();
newBTData = false;
int gate_actuated = 0;
Serial.println(inBT);
if (strcmp(inBT, "relay on 1") == 0) {
digitalWrite(main_gate, HIGH);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay off 1") == 0) {
digitalWrite(main_gate, LOW);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay on 2 ") == 0) {
digitalWrite(relay_2, HIGH);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay off 2") == 0) {
digitalWrite(relay_2, LOW);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay on 3 ") == 0) {
digitalWrite(relay_3, HIGH);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay off 3") == 0) {
digitalWrite(relay_3, LOW);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay on 4 ") == 0) {
digitalWrite(relay_4, HIGH);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay off 4") == 0) {
digitalWrite(relay_4, LOW);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay on 5") == 0) {
digitalWrite(relay_5, HIGH);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay off 5") == 0) {
digitalWrite(relay_5, LOW);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay on 6") == 0) {
digitalWrite(relay_6, HIGH);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay off 6") == 0) {
digitalWrite(relay_6, LOW);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay on 7") == 0) {
digitalWrite(relay_7, HIGH);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay off 7") == 0) {
digitalWrite(relay_7, LOW);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay on 8") == 0) {
digitalWrite(relay_8, HIGH);
gate_actuated = 1;
}
else if (strcmp(inBT, "relay off 8") == 0) {
digitalWrite(relay_8, LOW);
gate_actuated = 1;
}
return gate_actuated;
}
void close_main_gate() {
int times = 0;
while (times < 3)
{
digitalWrite(main_gate, LOW);
delay(main_gate_timeout);
if (digitalRead(main_gate_sensor) == 0) {
return;
} else {
digitalWrite(main_gate, HIGH);
delay(animal_stuck_time);
digitalWrite(main_gate, LOW);
times++;
}
}
digitalWrite(main_gate, HIGH);
}
void close_all_gates() {
digitalWrite(relay_1, LOW);
digitalWrite(relay_2, LOW);
digitalWrite(relay_3, LOW);
digitalWrite(relay_4, LOW);
digitalWrite(relay_5, LOW);
digitalWrite(relay_6, LOW);
digitalWrite(relay_7, LOW);
digitalWrite(relay_8, LOW);
exit_open = 0;
}