Hello,
I'm trying to program an ATMega2560 to control a hypot tester for work. I've done things like this in the past but I haven't needed to do this many nested "if" statements and I think the logic is probably wrong somewhere.
I'd like the program to wait for a button press, "TB" before starting anything. Once it starts, I'd like it to go through the first set of relay commands. Once the program receives a "PASS" signal from the hypot tester, it is supposed to increment "PASSCounter" then proceed to the next set of relay commands.
Currently it is cycling through each of the "if" statements endlessly. It does this without a button being pressed, or even connected for that matter.
Any insight into this would be much appreciated.
/*
This program is designed to automate an Associated Research Hypot tester (Model 3870).
The user of the box need only to set the tester to the proper program, then press start on the box.
The box will run the attached part through IR/DWV in "criss-cross", "row to row", and "pin to shell".
If a failure occurs at any point during the testing, the tester will error out. The box will need to be reset
using the reset button prior to restarting the test.
*/
//These lines tell the controller the name of the variable and the pin it is assigned to.
//"Const int" are variables that do not change. "int" variables can change.
const int R2R = 44; //ROW TO ROW RELAY PIN
const int CC = 45; //CRISS-CROSS RELAY PIN
const int SL = 46; //SHELL RELAY PIN
const int TR = 47; //TEST RELAY PIN
const int RR = 48; //RESET RELAY PIN
const int TB = 22; //TEST BUTTON PIN
const int RB = 23; //RESET BUTTON PIN
const int PASS = 24; // PASS OUTPUT FROM TESTER
const int FAIL = 25; // FAIL OUTPUT FROM TESTER
int TB_State = 0;
int RB_State = 0;
int PASSCounter = 0;
int PASSState = 0;
int FAILState = 0;
void setup() {
// put your setup code here, to run once:
//These lines of code tell the controller what type of communication to expect from each variable.
pinMode(R2R, OUTPUT);
pinMode(CC, OUTPUT);
pinMode(SL, OUTPUT);
pinMode(TR, OUTPUT);
pinMode(RR, OUTPUT);
pinMode(TB, INPUT); //Buttons and outputs from the tester are defined as "INPUT" to the controller.
pinMode(RB, INPUT);
pinMode(PASS, INPUT);
pinMode(FAIL, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//these "states" are used to read the condition of the input variables
TB_State = digitalRead(TB);
RB_State = digitalRead(RB);
PASSState = digitalRead(PASS);
FAILState = digitalRead(FAIL);
//instructions to reset the hypot tester and turn off on test relays when "Reset" is pressed.
if (RB_State == LOW){
digitalWrite(RR, HIGH);
delay(100);
digitalWrite(R2R, LOW);
digitalWrite(CC, LOW);
digitalWrite(SL, LOW);
digitalWrite(TR, LOW);
digitalWrite(RR, LOW);
}
//These "if" statements tell the controller what to do when the test button is pressed.
if (TB_State == HIGH){
if (RB_State == HIGH){
digitalWrite(RR, HIGH);
delay(100);
digitalWrite(R2R, LOW);
digitalWrite(CC, LOW);
digitalWrite(SL, LOW);
digitalWrite(TR, LOW);
digitalWrite(RR, LOW);
}
//"PASSState" monitors how many times the hypot tester has passed. When the "PASSCounter"
//increases in count, it goes to the next "if" statement. This allows the controller to
//test Row to Row, Criss-cross, and shell automatically.
if (PASSState = HIGH){
PASSCounter++;
}
if (PASSCounter = 1){
digitalWrite(R2R, HIGH);
digitalWrite(CC, LOW);
digitalWrite(SL, LOW);
digitalWrite(RR, LOW);
delay(100);
digitalWrite(TR, HIGH);
delay(500);
digitalWrite(TR, LOW);
}
if (PASSCounter = 2){
digitalWrite(R2R, LOW);
digitalWrite(CC, HIGH);
digitalWrite(SL, LOW);
digitalWrite(RR, LOW);
delay(100);
digitalWrite(TR, HIGH);
delay(500);
digitalWrite(TR, LOW);
}
if (PASSCounter = 3){
digitalWrite(R2R, LOW);
digitalWrite(CC, LOW);
digitalWrite(SL, HIGH);
digitalWrite(RR, LOW);
delay(100);
digitalWrite(TR, HIGH);
delay(500);
digitalWrite(TR, LOW);
}
if (PASSCounter = 4){
digitalWrite(R2R, LOW);
digitalWrite(CC, LOW);
digitalWrite(SL, LOW);
digitalWrite(TR, LOW);
digitalWrite(RR, HIGH);
delay(500);
digitalWrite(RR, LOW);
}
//The controller will shut everything down if a "FAIL" is detected.
if (FAILState == HIGH){
digitalWrite(R2R, LOW);
digitalWrite(CC, LOW);
digitalWrite(SL, LOW);
digitalWrite(TR, LOW);
digitalWrite(RR, HIGH);
delay(500);
digitalWrite(RR, LOW);
}
}
}