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