Bluetooth Problem Arduino mega

Hello Everyone,

i have an problem with my current project where i don't see the error or mistake i am doing.

So to explain in bref the project to understood what is asked from the Board.

Hardware:

Arduino Mega 2560
HC-05 blutooth transmitter
Android bluetooth serial controller
External hardware scale head communicating wiht the Board

So i want to use my board to command some relays connected on pneumatic doors, Where the commands needs to come from the Android serial controller or the External scale head.

So far so good no problem, i can send commands and they are reconigized from both, they are separated by a switch so or 1 is used or the other

The problem apears when i want to use my scale head as sensor so i am requesting info to close an entry door, and command from the android application, this can work 2-10 times in a row with out a problem, where the board can plant completly on the next one, and cuts down the bluetooth signal.

I hope to be clear enough if not request som more info.

uhh i dont have megas but connection with UNO is RX to RX TX to TX

The code and a picture of the circuit and how things are powered would help

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;
}

this is the circuit, (maybe a few minor changes) but i guess the problem is more in the code

connections are good as the communication is there , but hangs whenever it wants

A schematic would be better, and may be what J-M-L was requesting.
C

i don't see "circuit" in the picture. PCB image is almost useless without of connection diagram.

hopes this will help to understand the functions, The Scale hed can manage the Crush at anytime, and is always connectect as it works as entry sensor aswell, the Smartphone is there to activate (open) only the exit gates

so for know it works really smooth with commands froms the scale head, but it hangs once i switch on to the bleutooth communication

thats why my old project doesnt work :frowning:

I dont have thé complete wiring but i don't thibk this os the problem as every single function is working

@jannes_hvw You would be surprised how many times on this forum, "perfect hardware" is revealed as the culprit due to assumptions about wiring, power supplies, etc. etc. But, we can play along, suggesting software changes for another 40-50 messages, only to find that something basic IS wrong on the hardware side. Many will drop off as this continues, but some consider it sport.
If you read the "How to get" information at the top of the category, you'll see the recommendations. Whether you heed them is up to you.
Your choice.
For example, are you powering all your devices from one supply, or multiple. Have you got grounds established between all power supplies? Is each power supply capable of powering all the devices attached to it? I ask, because it's when you turn something on that things go wrong, and that's a changing power scenario.

C

I understand what you mean with the basics, i am not a really professional, but i try to do what i can, i don't have a schematic on each connection for now, i could work this out off course.

About the power, the scale head is running on a rechargeable internal battery, so only the arduino and yhe pneumatic valvles are on the same power supply 12v transformator.

However there is an rfid reader on the same power supply

Is this used?

SoftwareSerial bt(BLUETOOTH_RX, BLUETOOTH_TX);

Otherwise, nothing jumps out at me in your code; the delays, as usual, are unwelcome, but I don't see any that are long enough to result in serial overruns, or that sort of problem. I presume you actually are not using the Software Serial instance? I see no references to bt(.
C

Clarification. Is the scale head always powered, just ignored, or do you turn it on in this instance?
It sounds like your somehow clobbering your BT with the solenoid command, but I may be misunderstanding the paragraph above.
C

scalehead is always powerd on and is contuniues sending information over his serial port, so the arsuino can manipulate the entry gate.

The bt is giving commands only when all doors are closed and to open 1 i the exit gates

The rest of the code doesn't merit reading.... The above implies you are using software serial on hardware serial pins, which is a seriously bad idea.

You woyld suggest to change the pins ?

no, it will be better using HardwareSerial

yeah B404 says true use HardwareSerial. SoftwareSerial is pretty buggy and there would be always a problem.