Hot answers tagged

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); }


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

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

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

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

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 (...


2

Is it possible to program chips like the Arduino uno's Atmega328P directly with USB serial from your computer? The bare chip? Generally, no. Some chips that include USB hardware also may include a DFU bootloader in ROM. If your chosen chip has this, then yes you can - you just need to wire it up and use a suitable DFU firmware upload program to install ...


2

You can indeed use a serial monitor on your PC on the Mega's first serial port, in fact you will have to if you are connecting to one on the PC via USB port.. And just as with the Uno, you can connect a TTL serial device (like RS-232 serial, but using logic levels of 0 and +5v instead of [-3v - -25v] and [+3v - +25v] (which most PCs implemented as -12v and +...


2

if we are in level 1, both the levels 2 and 3 are pressed it should go to level 2 even if level 3 was pressed first. For this to work, the program should remember which buttons (mind the plural!) have been pressed. Thus, instead of remembering only one request, as in int required_level =-1; it should have a boolean variable for each of the possible ...


2

The line while (Serial.availableForWrite() <= 0) {} waits until the serial output buffer is not full. Whereas Serial.flush(); waits until the serial output buffer is empty.


2

https://www.arduino.cc/reference/en/language/functions/communication/serial/availableforwrite/ availableForWrite Get the number of bytes (characters) available for writing in the serial buffer without blocking the write operation Write operations on Arduino streams wait if they can't write the data to output. You can use the count returned by ...


1

The Timer1 library only supports... Timer 1! So you only have one hardware timer available. If you are using an Arduino Uno or anything similar, you have a couple of other timers. However, unlike Timer 1, which is a 16-bit timer, the others are 8-bit only. This means they are very limited in the range and precision of the frequencies they can ...


1

Check your wiring! My guess is the twisted pair (A-B swap). I've checked your code, it seems right. Please note that /RE and DE were designed that way so you can simply connect them and control with only one pin. I don't have the exact same hardware, I had MAX485 and Arduino Nano at hand. Nano only has one hardware serial, so I've used the SoftwareSerial ...


1

Just to complement JRobert's answer: yes, the four serial ports are independent. They can be activated independently, they can be set to different baud rates if needed, and they do have separate buffers.


1

Is it possible to program chips like the Arduino uno's Atmega328P directly with USB serial from your computer? Put a bootloader on the chip first, then an offboard USB/Serial adapter can be used for the serial programming. That's basically what a ProMini is - a bootloaded '328P (in SMD form) with a resonator & decoupling caps and a small voltage ...


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