radar speed display arduino mega and a seven segment display

hi everyone,

i have a project im working on and have hit a roadblock so to say.

i am trying to read from a speed radar device and display the speed on a seven segment display.

the radar outputs at 115200 baud; 8 data bits; no parity and the format is: nnn.d[cr][lf]

where n is the whole number of the speed, d is the number to the right of the decimal place

i am using SevSeg library and the harware ports on the mega with a max 232 conversion chip

the problem i am having is i dont know how to get the nnn.d into a dat type i can use with the seven segment library

i am pretty sure the problem is reading the whole ascii string, i believe my code is just grabbing one char at a time

#include "SevSeg.h"

//Create an instance of the object.
SevSeg sevseg;
 float output;
 long previousMillis = 0;
 long interval = 1000;

void setup() {
  //I am using a common anode display, with the digit pins connected
  //from 2-5 and the segment pins connected from 6-13
  sevseg.Begin(0,2,3,4,5,6,7,8,9,10,11,12,13);
  //Set the desired brightness (0 to 100);
  sevseg.Brightness(100);
  Serial.begin(115200);
  Serial2.begin(115200);
  
  
 
}

void loop() {


  // read from port 2, send to port 0:
  if (Serial2.available()) {
    output = Serial2.read();
    Serial.write(output); 
  }

{
    //Update the number to be displayed, with a decimal
    //place in the correct position.
    unsigned long currentMillis=millis();
    
   if(currentMillis - previousMillis > interval) {//sort of delay so it isnt changing the display so fast
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    sevseg.NewNum((output*10),3);
    
}

   {
  //Produce an output on the display
  sevseg.PrintOutput();
  }


}}

i am trying to figure out a way to read the whole string and change it to an variable integer i can use with the seven segment library

i am fairly new to arduino and any help would be much appreciated

thanks in advance

Try something like this - start storing the data when you see a good number, ignoring the cf/lf so it gets in sync.

  // read from port 2, send to port 0:
  if (Serial2.available()>4) {  // 5 bytes received?    
  inByte[0] = Serial.read2();  // declare inByte[4] at the top
  if ( (inByte >=0x30) && (inByte <=0x39) ){ // check 0-9 against www.asciitable.com
    // read the rest
    inByte[1] = Serial2.read();
    inByte[2] =Serial2.read();
    dummyByte = Serial2.read(); '// for the period
    inByte[3] = Serial2.read();
    for (x=0; x<4; x=x+1){ // declare byte x at the top
    Serial.write(inBye[x]); // 
    }
  }

i think i see what you are getting at. this puts the incomming data starting with a 0 into an array

i am having a little trouble compiling

im getting an error that says :
ISO C++ forbids comparison between pointer and integer

whiich im not sure is causing it it is pointing to the line:

if ( (inByte >=0x30) && (inByte <=0x39) ){ // check 0-9 against www.asciitable.com

sorry if this seem obvious

Ah - make that

if ( (inByte[0] >=0x30) && (inByte[0] <=0x39) ){

It would be nicer to use '0' and '9' rather than the ascii character codes.

thanks so much that worked great to eliminate the decimal point. and i learned alot about char, arrays and atoi.

it seems to be working mostly correct, when watching the serial monitor it looks as though things get a little out of sync though

heres my code

#include "SevSeg.h"

//Create an instance of the object.
 SevSeg sevseg; 


 char inByte [4];
 int x   ;
 int val ;
 
void setup() {
  //I am using a common anode display, with the digit pins connected
  //from 2-5 and the segment pins connected from 6-13
  sevseg.Begin(0,2,3,4,5,6,7,8,9,10,11,12,13);
  //Set the desired brightness (0 to 100);
  sevseg.Brightness(100);
  Serial.begin(115200);
  Serial2.begin(115200);
 
  
 
}

void loop() {
  
    {
    sevseg.PrintOutput();//Produce an output on the display

  }

  // read from port 2, send to port 0 and send variable to array:
  if (Serial2.available()>4) {  // 5 bytes received?    
  inByte[0] = Serial2.read();

  if ( (inByte[0] >= 0x30 ) && (inByte[0] <= 0x39 )){ // check 0-9 against www.asciitable.com
    // read the rest
    inByte[1] = Serial2.read();  // declare inByte[4] at the top
    inByte[2] = Serial2.read();
    byte dummyByte = Serial2.read(); // for the period
    inByte[3] = Serial2.read();
  }
             for (x=0; x<4; x=x+1){ // declare byte x at the top
    Serial.write(inByte[x]);
    
     
    }
int val = atoi(inByte);// combine array into int


    //Update the number to be displayed, with a decimal
    //place in the correct position.
  
        sevseg.NewNum(val,1);
    
}

 
}

the first one comes through as expected a 4 digit number with no caraige return and no new line but the next set is on the same line missing the two zeros and the the third shows up on a new line. somehow the [cr]and [lf] ar getting into the array somewhere

here is what i saw on the serial monitor.

0016016
0160016016
0160000000
0000008008
0080008008

my device sent 0016 6 times
0000 2 times
and 0008 5 times

i assumed they would all be on the same line as im ignoring the end of line codes

forgive me for the noobish understanding of this but i just dont see where the error would be coming from

You are probably getting out of sync because you are not dealing with the cr/lf. You are making assumptions about the data you are receiving.. In a case like this, it is better to receive individual bytes, one at a time, append them to a char array (c-style string), and check each character as it comes in. In this way, You can gather numbers and ignore '\r' and '.' when you get a '\n' , do whatever you want with the data.

Here's a simple illustration. It simply send back the extracted data that is sent from the Serial Monitor.

char input[10];
char temp;
byte index = 0;
bool got_one = false;

void setup(){
  Serial.begin(19200);
  input[10] = '\0';         /clears the char array to all zeros
}

void loop(){

  if (Serial.available() > 0) {
    temp = (char)Serial.read();
    if (temp >= '0' && temp <= '9') {
      input[index] = temp;    // It's a number! grab it
      index++;
    }
    else if (temp  == '\n') {
      got_one = true;            // Its a linefeed! Set a flag
    }
  }
  if (got_one) {

    Serial.println(input);
    index = 0;
    input[10] = '\0';
    got_one = false;
  }
}

thanks for the help guys. the project has been prototyped. now to build 2 more

i'll post pics when i can

How about a pic of the prototype? Or a short video on youtube.

here are a few photos and a short video.

thanks again for all the help, the community here is great.

IMG_5850.jpeg

IMG_5851.jpeg

IMG_1411.MOV (896 KB)

Sorry, I know this is a bit old...

I'm interested in building something similar to this. Can you tell us what the sensor you used was for the radar?