Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an integer serially in Arduino

I need to send an integer, say between 0-10000, to Arduino using serial communication. What's the best way of doing that?

I can think of breaking the number into an character array (eg: 500 as '5','0','0') and sending them as a byte stream (yeah, that's ugly). Then reconstruct at the other end. (anything is sent serially as a byte stream, right?)

Isn't there a better way? Somehow, it should be able to assign the value into an int type variable.

(really need to know the same thing about strings too, if possible)

like image 641
Anubis Avatar asked Dec 14 '12 08:12

Anubis


People also ask

How does serial data get transmitted to an Arduino?

When Serial data is transmitted to an Arduino, it is sent one byte at a time. Even though you might type “123” in the Serial Monitor, that’s not quite what is sent. Instead the bytes “1” then “2” then “3” are sent. Once received into a buffer on the Arduino, these individual bytes need to be reassembled into something useful.

How does Arduino send and receive multi digit data?

Arduino: Sending and Receiving Multi-Digit Integers. by. When Serial data is transmitted to an Arduino, it is sent one byte at a time. Even though you might type “123” in the Serial Monitor, that’s not quite what is sent. Instead the bytes “1” then “2” then “3” are sent.

How to use a serial monitor with Arduino?

The main motive of serial communication is to display the data or send the data to Arduino using a graphic interface. To enable the serial monitor we use the below function: The 9600 in the above function is a baud rate and you can change it according your need. LED Blinking Control by Potentiometer. How to use an LCD Display with Arduino.

How do I read an integer in Arduino?

Anyways, as can be seen in the Arduino Serial reference, you can read an integer using the Serial.parseInt () method call. You can read strings with eg.


2 Answers

If what you're looking for is speed, then instead of sending an ASCII encoded int, you can divide your number into two bytes, here's an example:

uint16_t number = 5703;               // 0001 0110 0100 0111
uint16_t mask   = B11111111;          // 0000 0000 1111 1111
uint8_t first_half   = number >> 8;   // >>>> >>>> 0001 0110
uint8_t sencond_half = number & mask; // ____ ____ 0100 0111
    
Serial.write(first_half);
Serial.write(sencond_half);
like image 104
NiñoScript Avatar answered Oct 23 '22 08:10

NiñoScript


You don't specify the from environment, so I assume your troubles is with reading serial data on an Arduino?

Anyways, as can be seen in the Arduino Serial reference, you can read an integer using the Serial.parseInt() method call. You can read strings with eg. Serial.readBytes(buffer, length) but your real issue is to know when to expect a string and when to expect an integer (and what to do if something else comes along, eg. noise or so...)

like image 24
Anders R. Bystrup Avatar answered Oct 23 '22 08:10

Anders R. Bystrup