Hi groundFungus,
I'm building a very similar project to KayJay using the code that was available here. I have modified some of the code but come across some problems due to my lack of under standing of coding.
(Please Note. I have note been firing a firearm in the house, I have been blowing 6mm wooden dowel pieces in a brass tube across the Chroney, it is far too dangerous and wasteful)
Some background to the Shooting Chrony:
The Chrony has two optical gates that the projectile must pass across to calculate the velocity.
If the Chrony detects the projectile at gate 2 but, not at gate 1 it displays Err-1
If the Chrony detects the projectile at gate 1 but, not at gate 2 it displays Err-2
If the projectile passes over both, the velocity is displayed.
I would like my Arduino Nano project to display either Error 1, Error 2 or Shot number and Velocity.
I'm having two problems:
-
The program is unable to determine the difference between Err-1 and Err-2
Code from the Chrony via serial is eg: -02-Err-1 or -02-Err-2
-02- is for the shot number and is irrelevant here.
The LCD and Serial monitor is displaying: ERROR 1
-
Once the system is powered up and shot across it I get from the Serial monitor:
Data available
Data available
Data available
10-, 140570V, 86.11Vf
found char E
ERROR 1
Data available
Data available
Error 1 is displayed, take the next shot
Data available
Data available
Data available
10-, 140570V, 86.11Vf10-,
found char E
ERROR 1
Data available
Error 1 is displayed, take the next shot
Data available
Data available
Data available
10-, 140570V, 86.11Vf10-,
found char E
ERROR 1
Data available
There is a problem with the if statements and the fact the data (10-, 140570V, 86.11Vf) is not being updated, even thou the Chrony is working correctly.
Code:
// modified serial input basic code (http://forum.aruino.cc/index.php?topic=396450).
// Example 5 - Receive with start- and end - markers combined with parsing
// modifies to read Chrony shot velocity unit
// include the library code:
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
const byte numChars = 32;
char recievedChars[numChars];
char tempChars[numChars]; //temporary array for use when parsing
// variables to hold the parsed data
int shotNumber = 0;
char notUsed[12];
//float shotVelocity = 0.0;
int shotVelocity = 0;
boolean newData =false; //set at start of program
//===========================================================
void setup() {
Serial.begin(4800); // put your setup code here, to run once:
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
Serial.println("This sketch will read shot number and velocity from Chrony"); //Write text to serial console
Serial.println("Will print ERROR for a read error"); //Write text to serial console
Serial.println();
lcd.print("Ready");
}
void loop()
{
// put your main code here, to run repeatedly:
recvWithStartEndMarkers();
if (newData == true) //Boolean test
{
strcpy(tempChars, recievedChars); //this tempoary copy is necessary to protect the original data because strtok()
// used in parseData() replaces the commas with \0
// copy data in recievedChars to tempChars
parseData();
// Serial.println("Data available");
showParsedData();
newData = false; // re-set newData Flag
// Serial.println("No Data available");
}
}
//============================================================
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx =0;
char startMarker = '-'; //**** changed start marker to -
char endMarker = '\r'; //**** changed end marker to \r
char rc;
while (Serial.available() > 0 && newData == false) // read all through and we have New data then exit
{
Serial.println("Data available");
rc = Serial.read(); // this is our data, re4ad serial data and stick it in rc
if (recvInProgress == true)
{
if (rc != endMarker) // is the rc character Not Equal to \r our end marker?
{
recievedChars[ndx] = rc;
ndx++; // increment a counter ndx
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
recievedChars[ndx] = '\0'; //terminate the string
recvInProgress = false;
newData = true;
Serial.println(recievedChars); // shows what was Rx __--[]-10-, 152116V, 79.57V
}
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
}
//===============================================
void parseData()
{
char * strtokIndx; // this is used by strtok90 as an index
strtokIndx = strtok(tempChars, "-"); // get the first part - the shot number or error
if (strchr (tempChars,"E")); // does it have the 'Err' word in it? check
{
Serial.println("found char E");
if(strchr (tempChars,"1")); // if it has a 1 it is Err-1 print this
{
Serial.println("ERROR 1");
lcd.clear();
lcd.setCursor(6,0);
lcd.print("ERROR 1");
delay(2500);
// lcd.clear();
return;
}
if(strchr (tempChars,"2")); // if it has a 2 it is Err-2 print this
{
Serial.println("ERROR 2");
lcd.clear();
lcd.setCursor(6,0);
lcd.print("ERROR 2");
delay(2500);
lcd.clear();
return;
}
}
Serial.println("found No char E, 1, or 2");
shotNumber = atoi(strtokIndx); // get numeric part
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off to get and ignor middle part to keep place in the string
strtokIndx = strtok(NULL, ","); // get last part of string
shotVelocity = atof(strtokIndx); // convert this part to a float
// shotVelocity = atoi(strtokIndx); // convert this part to a int
}
//==============================================
void showParsedData()
{
if(shotNumber == 0)
return;
Serial.print("Shot Number ");
Serial.print(shotNumber);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Shot ");
lcd.print(shotNumber);
Serial.print(" Shot Velocity ");
Serial.println(shotVelocity);
lcd.setCursor(0,1);
lcd.print("Vel. ");
lcd.print(shotVelocity);
}
Could you please help?