Hello. Can anyone say what I am doing wrong, where I have an error in my code?
My point is to check the cable in small groups. test program is working, but after test group status
doesn't change. I want that test goes to first group, when everything is ok, then test goes to second and
so on. Here is my code:
const int white_out = 33;
const int white_in = 32;
const int grey_out = 31;
const int grey_in = 30;
const int black_out = 29;
const int black_in = 28;
const int blue_out = 27;
const int blue_in = 26;
const int green_out = 25;
const int green_in = 24;
const int red_out = 23;
const int red_in = 22;
uint8_t outPins_first_group[] = {red_out}; // Output pins
uint8_t inpPins_first_group[] = {red_in}; // Input pins
uint8_t outPins_second_group[] = {green_out, blue_out}; // Output pins
uint8_t inpPins_second_group[] = {green_in, blue_in}; // Input pins
int first_group_status = 0;
int second_group_status = 0;
int total_count = 0;
void(* resetFunc) (void) = 0;
int green_led_pin = 14;
void setup(){
Serial.begin(9600);
pinMode(green_led_pin, OUTPUT);
}
void loop(){
Serial.println("Testing first group");
delay(2000);
test(outPins_first_group, inpPins_first_group, first_group_status);
while (first_group_status != 1) {
delay(2000);
test(outPins_first_group, inpPins_first_group, first_group_status);
}
Serial.println("First group OK");
if (first_group_status == 1) {
delay(2000);
Serial.println("Testing second group");
delay(2000);
test(outPins_second_group, inpPins_second_group, second_group_status);
while (second_group_status != 1) {
delay(2000);
test(outPins_second_group, inpPins_second_group, second_group_status);
}
Serial.println("Second group OK");
second_group_status = 1;
}
if (first_group_status == 1 && second_group_status == 1) {
total_count += 1;
if (total_count % 10 == 0) {
Serial.println("The cable is good");
Serial.println("Should come label");
Serial.println("Box Label");
Serial.print("Total count: ");
Serial.println(total_count);
delay(3000);
} else {
Serial.println("The cable is good");
Serial.println("Should come label");
Serial.print("Total count: ");
Serial.println(total_count);
delay(3000);
}
}
Serial.println();
delay(3000);
}
void test(uint8_t outPins[], uint8_t inpPins[],int group_status) {
for (uint8_t i = 0; i < sizeof(outPins); i++){ // Setup output pins
pinMode(outPins[i],OUTPUT);
digitalWrite(i,LOW);
}
for (uint8_t i = 0; i < sizeof(inpPins); i++){ // Setup input pins
pinMode(inpPins[i],INPUT); // Turn off internap pullup resistor
}
if (testCable(outPins, inpPins) == true) {
group_status = 1;
Serial.println(group_status);
digitalWrite(green_led_pin, HIGH);
delay(100);
} else {
group_status = 0;
Serial.println(group_status);
digitalWrite(green_led_pin, LOW);
delay(100);
}
}
bool testCable(uint8_t outPins[], uint8_t inpPins[]){
bool testResult = true; // Assume cable is good
uint8_t count = 0; // Count of correct connections
if (sizeof(outPins) != sizeof(inpPins)) { // Array sizes match?
Serial.println(F("ERROR: Array Sizes Do Not Match."));
return false; // Exit now
}
for (uint8_t o = 0; o < sizeof(outPins); o++){ // Loop though output pins
digitalWrite(outPins[o],HIGH); // Set pin high
if (digitalRead(inpPins[o]) == HIGH){ // Is correct pin connected?
count++; // Increment count
}
for (uint8_t i = 0; i < sizeof(inpPins); i++){// Scan input pins
if (digitalRead(inpPins[i]) == HIGH){ // If pin is high must be connected to output
if (o != i){ // If array numbers don't match then must be error
testResult = false; // Bad wire
}
}
}
digitalWrite(outPins[o],LOW); // Set output back to low
}
if (count != sizeof(outPins)){ // Have we had the correct number of correct pins?
testResult = false; // If not then must be error
}
return testResult;
}