Skip to content

Instantly share code, notes, and snippets.

@stefanherdy
Last active June 19, 2023 13:09
Show Gist options
  • Save stefanherdy/97859680517a9e53681252984ddd0ff4 to your computer and use it in GitHub Desktop.
Save stefanherdy/97859680517a9e53681252984ddd0ff4 to your computer and use it in GitHub Desktop.
This function can be used to read serial string that is passed to an arduino. In our project, we used a Raspberry Pi with a camera and performed some image processing on the captured images and passed the image information to the arduino to steer a car. This script was part of our project to create an autonomous driving car which can follow a li…
/**
Script Name: read-serial-string-with-arduino.c
Author: Stefan Herdy
Date: 23.10.2019
Description:
This function can be used to read serial string that is passed to an arduino.
In our project, we used a Raspberry Pi with a camera and performed some image processing on the captured images and passed the image information to the arduino to steer a car.
This script was part of our project to create an autonomous driving car which can follow a line with a certain color.
Usage:
- Use this function as part of your ino-file in your next Arduino-project.
**/
//Variables for the serial protocol
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
Serial.begin(115200);
void serialProtocol()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false)
{
rc = Serial.read();
if (recvInProgress == true)
{
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker)
{
recvInProgress = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment