Arduino serial print - arduino

I know this sounds really silly, but I really can't figure it out. I'm loading the following arduino code on an arduino uno:
void setup() {
Serial.begin(9600);
Serial.println("HELLO");
}
void loop() {
// put your main code here, to run repeatedly:
}
I'm uploading the code on the board and if I open a serial monitor repeatedly, I get different outputs. I'm expecting the output HELLO, and I get that sometimes. But I also get outputs like: HELLHELLO or HHELLO. I also loaded the same code on an arduino nano and it behaves similar.
Can someone please explain why this is happening? Is this an issue?

This could be due to the usb to serial buffer still containing data from the previous reset.
Try storing the string in PROGMEM to keep the string in flash memory rather than in RAM and see if that helps.

You need to set the baud rate to match to 9600 in the serial monitor, mismatched rates can cause unexpected output like you saw.

Related

Problem with serial communication between Arduino Mega2560 and ESP32

Good morning everyone.
I am trying to establish serial communication between an arduino mega and an esp32, in both I am using hardware serials.
In the arduino the uart3 in the esp the uart2. I have checked the pin connections several times.
I also adapted the arduino's tx signal to the esp32 with a level shifter.
Essentially I need to send a string to the esp32.
The code of the arduino is as follows:
String InvioDatiESP() {
String da_passare = ("hello!!!");
return(da_passare);
}
void setup() {
Serial.begin(9600);
Serial3.begin(115200);
}
void loop() {
Serial3.println(InvioDatiESP());
Serial.println(InvioDatiESP());
delay(1000);
}
I create the string in a function since it is a reduced version of the actual code in which the string is to be composed.
The code of Esp32 is as follows:
#define RXp2 16
#define TXp2 17
void setup() {
Serial.begin(115200);
Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
}
void loop() {
Serial.println(Serial2.readString());
}
I correctly set the boudrate in both serial ports on the IDE to verify communication.
The thing I notice that makes me doubt that the problem is related only to the ESP32 reading the string is that in the serial port of the ESP32 in the IDE while the program is running, blank lines are printed on the screen exactly every 1000ms, as if the data is received but not interpreted correctly.
How could I solve this in your opinion?
Thanks in advance for the answers!
SOLVED, the lever shifter was disturbing the signal too much, seen with the oscilloscope, I used a resistive divider and everything works perfectly, thank you all the same!
EDIT: Can you try to lower the boudrate? Check to see if with a lower one it will start decoding properly.
Your problem is not with the code, it's with the hardware. The arduino Mega2560 is using 5V logic level and ESP32 is using 3.3V.
You need to do some level shifting to be able to communicate.
You can take a look at this article to learn more about it.
Hope it helps.

Why does Arduino require a void loop() even if it’s empty

Today i had my 10th, or 12th hour of Arduino in University, we were assigned a paper with only 2 parts, the questions and a picture of a TinkerCAD circuit, we had to wire this circuit, then wrote a code to light up the LED
TinkerCAD Circuit
i know void setup(){} runs only once, and that void loop(){} loops forever, i wrote the following code to light up the led
void setup(){
pinMode(7,OUTPUT);
digitalWrite(7,HIGH);
}
but Arduino expected a void loop()at the end, even if its empty, why is that?, why cant it run without it since an empty loop is essentially the same as no loop?, what is the reason the compiler cant ignore the lack of a void loop?
What you see in your Arduino sketch is not everything that will be executed in the Arduino board.
Instead they provide you with some friendly functions, so it's easier for makers to easily produce code for the Atmega microcontrollers present in the Arduinos.
You can imagine that there's the following pseudo-program executing in the background of what you see
setup();
while (true) {
loop();
}
So this program requires you to have a function called setup and a function called loop.
The setup will be called once at the beginning, and the loop will be called again and again in an infinite loop
Leaving loop empty if you don't need it to do anything is perfectly fine, but it's mandatory that you define such function.

Trouble speeding up arduino

I'm trying to speed up my Arduino code by direct writing to registers. I wrote a short test script, but it doesn't seem to do much. If I use the 'digitalWrite()' function I can see an output on the oscilloscope, but using this code it just stays 0.
I used this link as a reference. I can't quite grasp what I missed.
byte *outputRegister;
byte bitMask;
void setup() {
pinMode(8,OUTPUT);
outputRegister = portOutputRegister(8);
bitMask = digitalPinToBitMask(8);
}
void loop() {
*outputRegister |= bitMask;
delay(1);
*outputRegister &= ~bitMask;
delay(1);
}
EDIT: The portOutRegister should return the port where the output pins are set. The digitalPinToBitmask function returns a bitmask (something like 0b00000001 for the first pin on the respective port).
With some further testing, I concluded that the digitalWrite function doesn't seem to actually change the values in these registers, which does nothing more than confuse me.
Arduino, if its Model = Uno, then it is ATmega-328 AVR, the best way to solve critical timing issues and achieve better execution times and code optimizations you should program via direct register modifications. That being said, use AVR-GCC, include and then do bit twiddling. Your code is not readable and hard to understand.
IMO you should either write in C++ inside Arduino IDE >> slower but easier option, or
start programming outside IDE inC and directly the AVR, >> orders of magnitude performance increase. Get to know the AVR Freaks Forum.

Arduino shows bad-shape characters in its system monitor

i want to code a simple Hello World code in Arduino to show it on its system monitor, my code is:
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Hello world!"); // prints hello with ending line break
}
void loop() // run over and over again
{
// do nothing!
}
but when i run this code, i get This Image characters on the system monitor.Also, i change the Serial.begin() from 9600 to other things but it does not work!How can i fix the problem?
Check your BAUD Rate on the Serial Monitor. Make sure its 9600, as you told it should be in your code:
Serial.begin(9600);
In the bottom of the Serial Monitor, there is a drop down to select the baud rate:
Your linked < This Image > shows a lot of garbage.
Wrong baud rate would rarely produce that much (mow many resets are seen there?
What type of arduino do you use?
Can you assure SerialMonitor in the Arduino IDE is connected to Serial on your Arduino?

Two port receive using software serial on Arduino

i am having trouble getting data from two sensors using two software serial ports with an arduino board. I noticed a similar question might have been asked before but the answers suggest it can't be done and I know fully well it can based on the example here (http://arduino.cc/en/Tutorial/TwoPortReceive)!
I am using an arduino ethernet. The devices I am trying to get data from include a GPS and an IMU both from sparkfun.
I can get data from either devices using just on software serial port but as soon as I add the second software serial port, neither ports will work. I can't use the hardware serial port because that is being used byt another device.
My code is exactly similar to the example:
#include <SoftwareSerial.h>
SoftwareSerial portOne(7,8);
SoftwareSerial portTwo(5,6);
void setup()
{
Serial.begin(9600);
portOne.begin(9600);
portTwo.begin(9600);
}
void loop()
{
portOne.listen();
while (portOne.available() > 0) {
char inByte = portOne.read();
Serial.write(inByte);
}
delay(500);
portTwo.listen();
while (portTwo.available() > 0) {
char inByte = portTwo.read();
Serial.write(inByte);
}
Serial.println();
}
Anyone with any ideas?
This code will not work, or will work poorly if it works at all. SoftwareSerial only has one internal buffer. Yes, you can have multiple SoftwareSerial objects in existence, but only one of them controls the internal buffer. When any RX pin gets asserted, that generates an interrupt, but only the listen()ing RX pin gets checked for a start bit.
What's really needed is the ability to check on multiple pins when an interrupt comes along from the start bit. Then you'd have to set up pointers to the appropriate data structures. It would be complicated, but possible.
Or maybe just give up on interrupt-driven reception, and spin on checking both/all of the RX pins, and start the receive based on the pin you see. Be forwarned that this code has much hair, and you WILL need an oscilloscope to make it work.
I'm having a similar problem, which is why I found your sensor. After talking it over with my co-workers, we've decided to read our sensors in rotating order. Our sensors report the current state of the sensor, and not specific events, so it's okay if we lose some reports. So we'll read from port 1, then read from port 2, then port 1, etc. Our sensors spit out lines of text, so we know when to switch to the next sensor.
The referenced example only actively listens to one port at a time. The recommended solution would be to upgrade to an Arduino Mega (https://www.sparkfun.com/products/11061) which has 4 hardware serial ports.
In order to simultaneously support two software serial ports is going to require a lot of the CPU resources. It also be a difficult design and excessive programming time far outweighing the cost of $58 + shipping.
Looking at you code again it occurs to me that you are immediately checking for characters after your portOne.listen command. At 9600 baud it will take approximately 1ms for the first character to arrive, your while test will have been completed and the portTwo.listen command executed long before the first character arrives.
For testing purposes try adding a 1-2 ms delay after the portOne.listen command and see if you get a character.
As an example (untested and note, if port one is sending characters with no intercharacter gaps, the first while will never fail, preventing reading portTwo characters):
void loop()
{
portOne.listen();
delay(2);
while (portOne.available() > 0) {
char inByte = portOne.read();
Serial.write(inByte);
delay(1);
}
portTwo.listen();
delay(2);
while (portTwo.available() > 0) {
char inByte = portTwo.read();
Serial.write(inByte);
delay(1);
}
Serial.println();
}
Don't use while ......
Use:
{ portOne.listen();
if (PortOne.available() ) {
ricevo = myPort1.read(); }
// delay(2); // ridiculos waiting time
// delay(1); // extra ridiculos waiting time
Than 500 ms is a too big time for switching, no time.....

Resources