0

I can't figure out why my Arduino is infinitely sending the same message to the Serial Monitor.

The goal of this project is to monitor and control a basic switching algorithm to redirect power through a power "grid" when a fault is detected in one particular branch to minimize the number of customers that are affected during a power outage. Neither of the two modules (each monitoring its own branch in the "grid") are designed to be a master or client. They will each receive the exact same code ("sketch"). Whoever sees a fault first will start a dialogue and begin the algorithm for redirecting power.

The basic premise is to have one module detect a fault and ask its neighbor if it also has a fault. If it doesn't, it will ask if it is okay to redirect power to that module's branch from the neighbor's branch. The fault-detecting module will send the neighboring module its last known power flow information. The neighbor will determine whether or not its "transmission lines" can handle the increased power flow, and then redirect power (via a relay) accordingly.

However, I can't get passed the first message. Once the "fault" is detected, the fault-detecting module sends its message infinitely. What I need is for it to send a message, then wait for a response, and then react accordingly.

Serial Monitor Message Display

Below is the Fault-Detecting Module portion of code.

I am new to Arduino and StackOverflow. I apologize if I am breaking any of the community's norms or guidelines. But, I can't find any forums that explicitly address this issue and none of the "fixes" have resolved the problem.

Thanks

int StatusPin = 13; //Assigns Pin 13 to be called "StatusPin"
int Relay = 12;     //Relay Signal Pin
int Feeder = 11;    //Feeder Control Switch Signal Pin
int readPin = A0;   //Assigns Pin A) to be called "readPin"
int msg = 0;        //Used for case declaration
//Default = 0
//Yes = 1
//No = 2
//Are You OK? = 3
//Switching Possible? = 4
int Pcheck = 0;       //Used for Power Calculation
float val = 0;        //Creates an empty variable to store future readings
float volts = 0;      //Same as above
char statTX = "Good"; //Current Status for this module

void setup() {
  char statTX = "Good"; //Default Status for this module
  pinMode(StatusPin, OUTPUT);
  pinMode(Relay, OUTPUT);
  pinMode(Feeder, OUTPUT);
  Serial.begin(9600);
}
  
void loop() {
  val = analogRead(readPin); //Stores the input value from the A0 pin into a variable
  volts = (val/1024)*5;      //Converts ADC reading to volts
  digitalWrite(Feeder, LOW); //Feeder Switch is Default OPEN

  if (val <= 676){
   digitalWrite(StatusPin, HIGH); //Green light status turns on when greater than 3.3V
   digitalWrite(Relay, HIGH); //Breaker switch is closed
  }

 //Transmission Code for Fault Detection Side
  else{
  statTX = "Fault";
  Serial.println("Fault");      //Sent Fault Status Message
  digitalWrite(Relay, LOW);     //Breaker switch is opened
  digitalWrite(StatusPin, LOW); //Green light status turns off when greater than 3.3V
  Serial.write(3);              //Send "Are You Okay?" message
  if (Serial.available() == 0){ //Creates a pause to wait for a response
  }
  else{
    msg = Serial.read();
      if (msg == 1) {
        statTX = "Isolated";           //Declares fault isolated
        Serial.println("Isolated");
        Serial.write(4);               //Send Message to Request Switch
        if (Serial.available() == 0){ //Creates a pause to wait for a response
        }
        else{
          msg = Serial.read();
           if (msg == 1){
              Serial.write(20);       //Sends tentative current draw
              if (Serial.available() == 0){
              }
              else {
                  msg = Serial.read();
                  if (msg == 1 && val < 676){
                  statTX = "SystemRestored";
                  Serial.println("SystemRestored");
                  digitalWrite(StatusPin, HIGH); //Green light status turns on when greater than 3.3V
                   }
              else {
                  statTX = "Failure";
                  Serial.println("Failure");
                   }
                }
           }
                else {
                  statTX = "Failure";
                  Serial.println("Failure");
                }
            }
          }
      }
  }

1 Answers1

0

Thing is, that the void loop() {...} procedure is called repeatedly. So if none of the conditions have changed, the output stay the same.

In your case, the

if (val <= 676){...

is not taken. And so, the else block executes the following commands and are printed to serial.:

Serial.println("Fault");      //Sent Fault Status Message
...
Serial.write(3);              //Send "Are You Okay?" message

This is not visible in your output image, but I guess that the (U+25A1) at the beginning of the line is from a preceding iteration - from Serial.write(3);. But this cannot be confirmed by the output image given. And if if (Serial.available() == 0){... will never be taken, the above commands will be executed in a permanent loop.

And this output repeats infinitely, because the void loop() function does not execute any other code paths, repeatedly.

zx485
  • 28,498
  • 28
  • 50
  • 59
  • Do you have a recommendation on how to achieve the desired outcome of "send message, wait for a response, respond based on response"? Or do you know what I have done wrong so that my code doesn't get stuck on my "do nothing" loop that is supposed to be triggered by 'if (serial.available() == 0) { }' ? I need to program in a pause where my module waits for a response from the other module. – SgtTicklePants Nov 05 '21 at 22:20
  • And, I'm not necessarily asking you for a full line of code even. Since I am new to Arduino, perhaps there is a useful function or library I could use that I simply haven't discovered through my Google searches yet. But, I am curious as to why Arduino doesn't complete the `Serial.write(3);` line and then get hung up and pause at `if (Serial.available() == 0) { }` until it receives a message at the RX pin. – SgtTicklePants Nov 05 '21 at 22:45