Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction returns wrong value if I don't print char array
    text
    copied!<p>I have a very strange situation. I have two functions that I am using to read values of the analog inputs of a Beagle Bone Black. The first function opens the file and reads the pin value and SHOULD return it to another function that is comparing that value to the value of another pin. Everything works as it should only when I print the <code>BUFFER</code> that I am using to concatenate the pin number with the file path. As soon as I comment out the line, I am getting bad values. How can simply printing or not printing the <code>BUFFER</code> cause this?</p> <pre><code>float AIN_value(char AIN) { float value; char line[10] = {0}; char BUFFER[150]; sprintf(BUFFER, "%s%c", AIN_FILE, AIN); FILE *fp; //printf("%s\n", BUFFER); fp = fopen(BUFFER, "r"); if (! is_open(fp)) { printf("ERROR: could not open %s\n", BUFFER); exit(1); } fgets(line, sizeof(line), fp); value = atoi(line); fclose(fp); //printf("%d\n", value); return(value); } bool pins_within_threshold(photores *data) { float P0_value = AIN_value(data-&gt;AP_0); float P1_value = AIN_value(data-&gt;AP_1); float highest_pin_value; float lowest_pin_value; if (P0_value &gt; P1_value) { highest_pin_value = P0_value; lowest_pin_value = P1_value; } else if (P1_value &gt; P0_value) { highest_pin_value = P1_value; lowest_pin_value = P0_value; } else if (P0_value == P1_value){ return(true); } float numerator = highest_pin_value - lowest_pin_value; float denominator = (highest_pin_value + lowest_pin_value) / 2; float quotient = numerator/denominator; float threshold = quotient * 100; printf("P0_value: %f\n", P0_value); printf("P1_value: %f\n", P1_value); printf("Threshold: %f%%\n", threshold); if (threshold &lt;= data-&gt;move_threshold) { return(true); } else if (threshold &gt; data-&gt;move_threshold) { return(false); }`enter code here` else { return(true); } } </code></pre> <p>EDIT: By 'good' values I mean that <code>bool pins_within_threshold(photores *data)</code> will print the correct threshold. This 'threshold' is the percent difference of the values of two photoresistors. They are both getting equal amounts of light and the threshold will generally be within a 5% difference. The threshold will be a correct output only when I print the BUFFER in <code>float AIN_value(char AIN)</code>. When the printf is commented out, <code>bool pins_within_threshold(photores *data)</code> is printing that the pins are not within the threshold and that one pin gets no reading at all. I consider this a 'bad' value.</p> <p>EDIT 2: Here is the output after commenting out <code>snprintf(BUFFER, sizeof(BUFFER), "%s%c", AIN_FILE, AIN)</code>:</p> <pre><code>AIN5: 1696 AIN6: 0 Threshold: 200.000000% </code></pre> <p>Here is the output if I print:</p> <pre><code>AIN5: 1366 AIN6: 1379 Threshold: 0.947522% </code></pre> <p>EDIT 3: After some time chatting with chux, It is clear that there is a file read sync issue because the OS owns the file and the file is in a constant state of change. For some reason, a simple `printf('\n'); fixes the problem. Not a complete fix, would like a better understand as to why this happens and how I an avoid it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload