It’s a Mega 2560, and in my experience the errors are far from rare. I’ll go ahead and post a version of my main detection loop.
@void READ_BLOCK_DATA() {
while (Adaptd == true) {
if (i == 0) {
TFT_BL_ON;
bmpFile = SD.open(__Gsbmp_files[6]); ///////////////////////////THIS SELECTS THE .bmp FILE TO OPEN. CHANGE THE INDEX NUMBER.
if (!bmpFile) { Serial.println("didnt find image"); while (1); } ////This loads the measurement screen/////////
if (!bmpReadHeader(bmpFile)) { Serial.println("bad bmp"); return; } ////This loads the measurement screen/////////
bmpdraw(bmpFile, 0, 0); ////This loads the measurement screen/////////
bmpFile.close(); ////This loads the measurement screen/////////
Serial.println("Now taking measurements! (uncomment serial print lines to see)");
++i;
}
BUTTON_STUFF();
blocks = pixy.getBlocks();
W = pixy.blocks[0].width;
H = pixy.blocks[0].height;
x = pixy.blocks[0].x;
inchpos = InPix*(-(160.0 - x)); //Converts to inches using empirically found inches per pixel
Output.add(inchpos); // appends output to running median buffer
MedianOut = f_round(Output.getMedian(), 3);
float hashpos = f_round((MedianOut / 0.064), 2); //inches divided by inches per hashmark = hashmarks
dotpos = -(hashpos * 14) + 160;//POSITION INDICATOR DOT//
if (blocks && x > 0 && x < 320) {
if (W <= (StdWidth + widthpad2) && W >= (StdWidth - widthpad2) && H > 150) { //****GOT SOME GOOD BLOCKS!
ledgreen();
if (abs(lastdot - dotpos) >= 2) { //////////////////////POSITION INDICATOR DOT///////////////////////////
Tft.fillCircle(152, lastdot, 20, BLACK);
q = 0;
}
if (q == 0 || q %20 == 0 && dotpos >= 6 && dotpos <= 313 ) { //q = 0 means that there is either a new value to write or that the measurements have started or restarted. This inproves speed by not writing the value to the screen at every loop.
Tft.fillCircle(152, dotpos, 12, WHITE); //Draws the circle at the new location
Tft.drawTriangle(152 - 6, dotpos - 10, 152 - 6, dotpos + 10, 152 + 12, dotpos, RED); //(int poX1, int poY1, int poX2, int poY2, int poX3, int poY3, INT16U color)
}
lastdot = dotpos;
if (outFormat == 0) {/////////////////////////////////////////////////HASHMARKS MODE (2 decimals) ///////////////////////////
dtostrf(hashpos, 5, 2, stringval); //Transpose hashpos into string format. //dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
stringval[5] = 0; //make sure buffer is null-terminated to prevent buffer overflow
if (abs(lasthashpos - hashpos) >= 0.01) {
Tft.drawString(laststringval, 70, 215, 5, BLACK); //screen overwrite if new value different from last
q = 0;
}
if (q == 0 || q % 20 == 0) { //q = 0 means that there is either a new value to write or that the measurements have started or restarted. This inproves speed by not writing the value to the screen at every loop.
Tft.drawString("Hashmarks:", 30, 182, 2, WHITE);
Tft.drawString(stringval, 70, 215, 5, WHITE); //print output to screen
}
lasthashpos = hashpos; //Save hashpos for next comparison to determine if screen overwrite needed
dtostrf(hashpos, 5, 2, laststringval);//Save string value for next overwrite
laststringval[5] = 0; //make sure buffer is null-terminated to prevent buffer overflow
}
else { //////////////////////////////////////////////////////////INCHES MODE (3 decimals) ////////////////////////////////////
dtostrf(MedianOut, 6, 3, stringvalin); //Transpose inhpos into string format. //dtostrf(floatVar, minStringWidthIncDecimalPoint, numVarsAfterDecimal, charBuf);
stringvalin[6] = 0; //make sure buffer is null-terminated to prevent buffer overflow
if (abs(MedianOut - lastinchpos) >= 0.001) {
Tft.drawString(laststringvalin, 70, 215, 4, BLACK); //screen overwrite if new value different from last
q = 0;
}
if (q == 0 || q % 20 == 0) { //q = 0 means that there is either a new value to write or that the measurements have started or restarted. This improves speed by not writing the value to the screen at every loop.
Tft.drawString("Inches:", 30, 165, 2, WHITE);
Tft.drawString(stringvalin, 70, 215, 4, WHITE);
}
lastinchpos = MedianOut; //Save inchpos for next comparison to determine if screen overwrite needed
dtostrf(MedianOut, 6, 3, laststringvalin);//Save string value for next overwrite
laststringvalin[6] = 0; //make sure buffer is null-terminated to prevent buffer overflow
}
}
else { // no good blocks :(
ERROR();
}
//SerialOut(); ///print bocks to seial for debug
//Serial.println(millis()); //for debug
i++;
g = 0;
++q; ////iterate q to stop new readouts from being printed to screen unless they are new
}
else {
delay(D);
g++;
if (g >= 7) { //block disappearance detector: if blocks == 0 for 7 consecutive loops, then the block has disappeared from view faster than the camera could react.
x = 281; //forces "RANGE" error. x updates appropriately on next loop.
g = 0;
ERROR();
}
}
PayloadSendit();
}
}@