Hot answers tagged

7

For that sort of difference you are better using RS-485 instead of UART. UART is really only designed for short-distance communication. Converting UART to RS-232 will give an improvement, but switching to RS-485 instead will give you the ability to create a multi-drop bus for connecting more than two Arduinos together over a long distance. UART uses 5V ...


7

You are saving a String object in EEPROM, which is useless. A string object does not store the contents of your string. Instead, it just stores: the memory address where the actual contents (the characters) is stored the amount of memory allocated for this contents the number of characters that actually make the string This is what you are storing to, and ...


6

Serial.println() prints the value of one expression. The expression you passed it was `("Test " + index++). In C/C++ a character literal such as "Test " evaluates to the memory address its first character. Adding 1 to that address gives the address of the next (or 2nd) character. Adding 2 to it gives the address of the 3rd character, and so on. To get what ...


5

The Stream class has pure virtual methods which must be implemented in derived not abstract class. The pure virtual method from base class Print is: virtual size_t write(uint8_t) = 0; The pure virtual methods from Stream are: virtual int available() = 0; virtual int read() = 0; virtual int peek() = 0; additionally add in your class the line using Print:...


5

SoftwareSerial has a considerable overhead. It can often send at 115200 successfully but 9600 is about its limit for receiving, and you're trying both send and receive. In addition, for each character your code receives, it transmits considerably more than that 1 character (11 characters, by my count). It isn't too surprising that it would fall behind. You ...


5

Simply put, a serial port implemented in software requires the program to sample the input pin fast enough to detect every transition. To satisfy the Nyquist frequency rate we need the program to sample the data twice as fast as we expect the data to change. A serial port implemented in hardware might only need to be sampled at 1/16 this rate assuming ...


5

your code makes copies of Serials into items in the array of HardwareSerials. To use the original Serial objects, store and use pointers. HardwareSerial* Serials[]={&Serial,&Serial1,&Serial2,&Serial3}; void setup(){ for (int i=0;i<=3;i++){ Serials[i]->begin(38400); while (!*Serials[i]); } } void loop(){ ...


4

It would seem you failed to configure the port on the Linux side. By default, it is configured to who-knows-what baud rate, and to a “cooked” mode where the data is preprocessed by the kernel driver. You want instead to set it to the correct baud rate and to “raw” mode. The stty (meaning, “set terminal”) command does this: stty -F /dev/ttyACM0 raw 9600 ...


4

Roughly speaking, for a given protocol and medium (cable) the data rate and the distance are inversely related, that is, a connection with half the data rate can work across twice the distance. You will eventually hit the hard limit when the total capacitance of the cable will become too much for the driver. RS-232 standard provides this table of maximum ...


4

You may use an array of pointers to the actual serial ports. For example, on an Arduino Mega: const size_t PORTS_COUNT = 4; const HardwareSerial *ports[PORTS_COUNT] = { &Serial, &Serial1, &Serial2, &Serial3 }; void setup() { for (size_t i = 0; i < PORTS_COUNT; i++) ports[i]->begin(9600); }


4

Suppose one connected two of the Arduino Uno boards through Rx and Tx pins. I assume you will connect the TX of one Arduino to the RX of the other one and vice versa, which is the right way to connect them. As theSealion explained in his answer, you shouldn't connect two TX Pins together. if one or both of the Arduino boards were connected to computer, ...


3

As the author of the serial2mqtt gateway. Maybe it's time to have a new look. I've included the binaries for different platforms in the build : https://github.com/vortex314/serial2mqtt/tree/master/build So to get started : unzip the binary and update the serial2mqtt.json config. Should be piece of cake. Let me know otherwise via github.


3

The Arduino already reads from the 4 serial ports at the same time. Or at least as close to “at the same time” as possible with a single CPU: every time a byte is received at one port, an interrupt is triggered that reads the byte from the UART and puts it into a buffer. You can then get it from the buffer using Serial.read(). What you now have to do is ...


3

Here: CRGB leds[NUM_LEDS]; you are defining an array of NUM_LEDS objects of the CRGB type. Valid indices for this array range from 0 to NUM_LEDS-1. Here: for (int i = 0; i <= NUM_LEDS; i++) { leds[i].setRGB( r, g, b ); } you are accessing the array elements at indices 0 to NUM_LEDS. The last iteration is an out-of-bounds access, which is causing ...


3

This (now 3 year old) github / esp8266 thread suggests using an external RTC if time tracking is of importance. It also points at a dead NodeMCU document link. That document appears to have move here. This document describes the lengths taken to provide a better account for time passage during sleep. Which, apparently, includes guessing at the sleeping ...


3

You can create a class derived from Print that forwards its output to either or both Serial and Serial1. The only method you need to implement for this to work is write(uint8_t): class DualPrint : public Print { public: DualPrint() : use_Serial(false), use_Serial1(false) {} virtual size_t write(uint8_t c) { if (use_Serial) Serial.write(c); ...


3

If you take a look into the implementation: size_t HardwareSerial::write(uint8_t c) { _written = true; // If the buffer and the data register is empty, just write the byte // to the data register and be done. This shortcut helps // significantly improve the effective datarate at high (> // 500kbit/s) bitrates, where interrupt overhead becomes a ...


3

I ran a slightly modified version of your test code on a Mega 2560 and got no echo at all; Unless I totally misunderstood your problem description, there must be something else going on. Here's the code I ran: char test[15]; void setup() { Serial.begin(9600); Serial1.begin(115200); for (int i = 0; i < 15; i++) { //Serial.print(...


3

SoftwareSerial only works on a few select GPIO pins on the Mega. Specifically those pins that have PCINT on them. But that is irrelevant. There is no reason to use SoftwareSerial on a Mega except in exceptional circumstances. You have four hardware UARTs on the Mega, so there is no call to use SoftwareSerial. Use the real UART pins and objects: Serial -&...


3

You cannot use the + operator for that. However, since EEPROM.read only returns one byte and not a string, you can do a much easier implementation: void loop() { char result[] = "1:2:3"; for (int item = 0; item < 3; item++) { result[item * 2] = EEPROM.read(1 + item); } } This first create a string "1:2:3", where the digits are ...


3

When you type "100" you aren't sending the number 100. You're sending the characters "1", "0", "0", and whatever selected line ending you have (CR, LF, or CR and LF). So if you have CR+LF for your line ending you're reading the numbers 49, 48, 48, 13 then 10. You need to read the characters as they arrive and group them into a representation of the number (...


3

Here is my guess what happens: Once the code in the if statement have run, the loop() only contains the incrementation of i. This will run very fast. After a rather short time (maybe someone here can make the calculation to get the time, that is needed) i will overflow, going from it's most positive value to it's most negative value, then incrementing ...


3

There's two basic things wrong here. First is your way of opening the serial port and immediately sending data. The Arduino is reset when you open the serial port and the bootloader runs. This takes a second or so, and during that time any data that you send is lost. Secondly you're not checking for a flag of "W" but instead you're assigning "W" to the ...


3

You can use the C function sprintf: char text[5]; sprintf(text, "%04d", number); Serial.println(text); The leading 0 in 04d will add zero's for the length (4) is met, so 1 will become 0001, 10 will become 0010, 100 will become 0100 and 1000 will stay 1000.


3

Three if statements look a bit simplistic, but are much better in code size than sprintf, if that's the only usage for sprintf. if (number < 1000) Serial.write('0'); if (number < 100) Serial.write('0'); if (number < 10) Serial.write('0'); Serial.println(number); Saves about 1.5kB Flash, compiled with Arduino 1.8.9 for an Uno


3

The problem is that a serial connection usually connects two devices, in your case it's three (sensor arduino, receiving arduino and your pc). To get around this you have to use an additional software serial on different pins on the receiving arduino (unfortunately the atmega328 has only one hardware serial interface). As long as you don't have to send ...


3

Just to have the full working code, I ended up with this: #include <EEPROM.h> char serial_characters[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}; // 7 chars for serial and one for string end '\0' char serial_number[8]; void setup() { Serial.begin(115200); EEPROM.get(0, serial_number); // check found format if (serial_number[0] == 'S' &...


3

The whole concept of Baud Rate with USB communication is completely meaningless. There is no such thing as "baud rate" over USB. What there is, and what you are confusing with "baud rate" is a configuration item which the host can send to the device which is a "I would like you to communicate with other devices at this speed" configuration item. This is ...


3

This is because the output of the serial is buffered. It's still sending the first bit of data while you change the baud rate - and from then on it's just a complete mess. You need to force it to finish sending before you can change the baud rate by using Serial.flush(): void setup() { Serial.begin(115200); Serial.println("Hello"); Serial.flush(); } ...


3

The driver board for your "Lasersaur" appears to be based around the Adafruit Trinket Pro. That board, on Adafruit's website, is deprecated, with the following warning: Deprecation Warning: The Pro Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Pro Trinket ...


Only top voted, non community-wiki answers of a minimum length are eligible