Go Down

Topic: Wire harness test program (Read 91 times) previous topic - next topic

everumsas

Sep 18, 2019, 01:39 pm Last Edit: Sep 18, 2019, 02:56 pm by everumsas
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:



Code: [Select]

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

TomGeorge

#1
Sep 18, 2019, 01:47 pm Last Edit: Sep 18, 2019, 01:48 pm by TomGeorge
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What model Arduino are you using?

Thanks.. Tom.. :)

Everything runs on smoke, let the smoke out, it stops running....

johnwasser

Code: [Select]
void test(uint8_t outPins[], uint8_t inpPins[],int group_status) {
  for (uint8_t i = 0; i < sizeof(outPins); i++){  // Setup output pins

Arrays become pointers when you pass them to a function.  The sizeof operator will always return 2 in an Arduino MEGA because that is the size of the pointer.  You should calculate the number of elements in the array in the calling code and pass that to the function.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

johnwasser

Code: [Select]
  while (first_group_status != 1) {
    delay(2000);
    test(outPins_first_group, inpPins_first_group, first_group_status);
  }

Integers are passed to a function by value.  If you want the function to be able to change the value of the variable being passed, change it to a reference:
Code: [Select]
void test(uint8_t outPins[], uint8_t inpPins[],int &group_status) {
Note the '&' which makes the argument a 'reference'.

Note: There is a "do/while" statement that causes the body of the loop to be executed at least once.  That would be cleaner than using the same function call outside and inside the loop:
Code: [Select]
  do
  {
    delay(2000);
    test(outPins_first_group, inpPins_first_group, first_group_status);
  } while (first_group_status != 1);


Question: If you test by group, how do you test that there are no shorts between groups?  Wouldn't it be better to check all of the pins at the same time?
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

everumsas

thank you very much. This is only the beginning. At the end I will make the test for all pins. Because I need to check harness from 70 wires and many are connected with connectors in groups. So when I test in groups and it is something wrong, I can replace one group and continue, I don't need to replace everything. With do While you are correct, that make more sense. Only I have one problem. When I am making a group from one output and one input, the program makes errors...

johnwasser

Only I have one problem. When I am making a group from one output and one input, the program makes errors...
Did you miss the part of Reply #2 where I explained why "sizeof (outPins)" always returns 2 and what to do about it?
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Go Up