Hi, I'm a beginner and I'm experimenting and trying to learn how to send and receive data from my Arduino and my PC(mostly basic Python things, maybe some C# winforms stuff down the line as well).
I don't have any specific project in mind, I kinda just want to find out how stuff works at the moment.
But since the COM port is busy while I try out the code I write it is really hard for me to know what the Arduino actually receives and how it sees the data, since I cant do the usual Serial.println(whatever)
Is there any way I circumvent this problem and actually see what the data looks like in real time? Turning leds on if the "correct" data is received is so slow and I have no way of actually finding out what it actually looks like when it doesn't work the way I want it to. I've tried using an LCD screen but that was wonky as hell. Can I use my second Arduino somehow? Can I make the Arduino echo back to my Pythonprogram(or other program) what it actually read?
If the reason the COM port is busy is because it is being used by your Python program then the answer is simple - add some code to the Python program so it prints out the debug messages from the Arduino.
One of the SoftwareSerial libraries will let you talk to Python using different pins and keep hardware serial for debugging. Or you can use it to talk to your other arduino and have that one report back to the serial monitor.
Robin2:
If the reason the COM port is busy is because it is being used by your Python program then the answer is simple - add some code to the Python program so it prints out the debug messages from the Arduino.
...R
Thanks, I've been messing around a bit now and sending and writing "char"s is working fine. I've also tried do send and write some strings but that is either slow, I get two strings at once, or the string "arrives" as x different characters in a row.
Are there any reasons to ever use String? Is it faster to send and recieve chars(char val = Serial.read(); vs Serial.readString(); ) and then "make the x amount of chars" into a String? If so how would I do that?
Also when should I use Serial.println(val) and when should I use Serial.write(val)?
Sorry to bother you guys with all these questions but the String or Char stuff is kinda confusing to me and reading the reference just makes me more confused.
rarhara:
Are there any reasons to ever use String?
No. It's a crutch and its use exposes you to unnecessary issues with heap fragmentation.
Folks will tell you that it's convenient and that they've used it many times without issue, but personally, if you're asking for help with your sketch doing something peculiar and I see Strings in it, I'll be reluctant to look at it until the Strings are gone.
wildbill:
No. It's a crutch and its use exposes you to unnecessary issues with heap fragmentation.
Folks will tell you that it's convenient and that they've used it many times without issue, but personally, if you're asking for help with your sketch doing something peculiar and I see Strings in it, I'll be reluctant to look at it until the Strings are gone.
Ok, so say for example I want to input my name in the python console, send it to the Arduino then write that name on an LCD screen, how would I do that without storing the name as a string?
rarhara:
Ok, so say for example I want to input my name in the python console, send it to the Arduino then write that name on an LCD screen, how would I do that without storing the name as a string?
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.