I've written a visual basic program that sends a value via serial to my Arduino. At that point, the program SHOULD write that value to an lcd screen and to a servo.
My question is how do I turn the Serial.read() input data into a usable string, something I can compare to, something that I can write to the lcd / the servo.
So i'm using a trackbar in visual basic, that produces an integer value.
I'm then using the Serial.Write function in vb to send the data.
Then, I want to take this data and write it to a servo. But if i use mysevo.write(serial.read()) it goes all wonky. This is because the Serial.read() command only yeilds one byte. I would like to make a way that yields a string so I
can do things like compare it to stuff / write it to different things.
something like
First, you need a buffer to put the characters in. You can do that like this:
char buf[32];
Of course, you will want to change 32 to a little over the maximum size of the input you're expecting.
Then, you need a variable to say what position in the buffer you are in, like this:
int bufpos = 0;
You also need some way to know when the string has been fully sent over serial. Let's use a newline:
const char EOPmarker = '\n';
and make sure to have your VB program send one!
Then you just need to read the bytes into the buffer when there's one available, and do something once you've received the EOP (end of packet) marker:
const char EOPmarker = '\n';
char serialbuf[32];
void loop() {
if (Serial.available() > 0) {
static int bufpos = 0;
char inchar = Serial.read();
if (inchar != EOPmarker) {
serialbuf[bufpos] = inchar;
bufpos++;
}
else {
serialbuf[bufpos] = 0; // null terminate
bufpos = 0;
// Do whatever with serialbuf, like print it or use the atoi function to convert to int
}
}
}
const char EOPmarker = '.'; // i changed this so i could use the keyboard and the serial monitor baked into the arduino ide to debug. My computer is slow and having to debug the vb program each time I want to test takes a long time.
char serialbuf[32];
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup(){
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
delay(100);
lcd.clear();
static int bufpos = 0;
char inchar = Serial.read();
if (inchar != EOPmarker) {
serialbuf[bufpos] = Serial.read();
}
else {
serialbuf[bufpos] = 0; // null terminate
lcd.write(serialbuf);
}
bufpos++;
}
}
What is "serialbuf" because when i type
1234.
into the input line in the serial monitor in the ide, the lcd only prints
24
Again, this is not why the project is meant to do, it is a proof of concept, but the project will eventually write serial inputs to the lcd, as mentioned in above posts.
Get rid of the delay(100). There's no real purpose for it. If it doesn't work without the delay, you should figure out what that is so.
lcd.clear() should be moved into the else clause -- After all, you only want to clear it when you've received an entire message, not when the first part of the message comes in.
You need to set bufpos back to 0 in the else clause.
You should only increment bufpos in the if (inchar != EOPmarker) clause.
You should set serialbuf[bufpos] to inchar, not Serial.read() (I'll update my previous post to include the last three, sorry)
I think you need to use lcd.print rather than lcd.write.
Good job for making a proof of concept before trying to glob everything together at once.
Thanks for everybody's help here. I've synthesized all of your suggestions as well as made some additions and I really think I've written something cool here.