Hello everyone, this is my first time posting on any forum so I hope I'm doing things right.
I need some help as to how I should continue my Arduino code while following my flowchart. I am doing this for an internship but I have almost zero prior experience with Arduino or any kind of programming language in general.
The program is meant to control 3 relays by detecting and comparing current values of 3 devices.
I have tried looking up many 'if else' and 'switch' cases online but I have yet to find any information that can help. Another possible way that looks like it will work is a nested-if but my flowchart will require 2 nested-ifs.
My main question is how do I code the part of the flowchart circled in red, so that after [ K2=1 ], the program will continue to the [ I(L)=I(PV) ] decision?
Here is what I have:
int K1=22; //relay switch K1
int K2=24;
int K3=26;
byte Power=23; //Virtual switches
byte Charging=25;
byte Manual=27;
byte GetValues=29;
//Variables that are not defined here are defined in another sketch:
//current_mA_PV, current_mA_Load, current
void setup() {
pinMode(K1, OUTPUT);
pinMode(K2, OUTPUT);
pinMode(K3, OUTPUT);
pinMode(Power, INPUT);
pinMode(Charging, INPUT);
pinMode(Manual, INPUT);
pinMode(GetValues, INPUT);
Serial.begin(9600);
}
void loop() {
int valPower = digitalRead(Power); //Read state of Power button
int valCharging = digitalRead(Charging); //Read state of Charging button
int valManual = digitalRead(Manual); //Read state of Manual mode button
int valGetValues = digitalRead(GetValues); //Read state of GetValues button
if (valCharging > 0) { // if the Charging is on, turn on K3 only:
digitalWrite(K1, LOW);
digitalWrite(K2, LOW);
digitalWrite(K3, HIGH);
} else if (valManual > 0) { //if Manual mode on, reset relay switches to 0, for manual operation.
digitalWrite(K1, LOW);
digitalWrite(K2, LOW);
digitalWrite(K3, LOW);
} else if (current_mA_PV > 0) { //if I(pv)>0A, turn on K2 only:
digitalWrite(K2, HIGH);
} else if (current_mA_Load <= current) { //if I(Load)<=I(batt), turn on K2 and K3 only.
digitalWrite(K1, LOW);
digitalWrite(K2, HIGH);
digitalWrite(K3, HIGH);
} else if (current_mA_Load > current) { //if I(Load)>I(batt), turn on K1 only.
digitalWrite(K1, HIGH);
digitalWrite(K2, LOW);
digitalWrite(K3, LOW);
} else {
continue; //if all condition above are false, return back to Charging stage.
} //rest of the code which I do not know where to insert
}
Attached is a picture of my flowchart for your reference.
Any suggestions to make the code cleaner or better are welcome!
Thanks for all your help!