![](/uploads/1/2/5/3/125391842/474291458.jpg)
Serial Call and Response Language: Wiring/Arduino This program sends an ASCII A (byte of value 65) on startup and repeats that until it gets some data in. Feb 25, 2018 - Contents • • • • • • • • • • • • In the, you sent data from one sensor to a personal computer. In this lab, you'll send data from multiple sensors to a.
Serial reading in multiple bytes and integer values
serial_read_integers.ino
/* |
http://www.baldengineer.com/blog/2012/07/30/arduino-multi-digit-integers/ |
*/ |
unsignedint controllerInputVal=0; // Max value is 65535 |
char incomingByte; |
char controllerInput; |
voidsetup() |
{ |
// start serial port at 9600 bps: |
Serial.begin(9600); |
while (!Serial) { |
; // wait for serial port to connect. Needed for Leonardo only |
} |
pinMode(2, INPUT); // digital sensor is on digital pin 2 |
// establishContact(); // send a byte to establish contact until receiver responds |
} |
voidloop() |
{ |
} |
/* |
SerialEvent occurs whenever a new data comes in the |
hardware serial RX. This routine is run between each |
time loop() runs, so using delay inside loop can delay |
response. Multiple bytes of data may be available. |
*/ |
voidserialEvent() { |
if (Serial.available() > 0) { // something came across serial |
controllerInputVal = 0; // throw away previous integerValue |
controllerInput = '0'; |
int byteCount = 0; |
while(1) { // force into a loop until 'n' is received |
incomingByte = Serial.read(); |
byteCount++; |
if (incomingByte 'n') break; // exit the while(1), we're done receiving |
if (incomingByte -1) continue; // if no characters are in the buffer read() returns -1 |
if (byteCount 1) { |
controllerInput = (char) incomingByte; |
} |
else { |
controllerInputVal *= 10; // shift left 1 decimal place |
controllerInputVal = ((incomingByte - 48) + controllerInputVal); // convert ASCII to integer, add, and shift left 1 decimal place |
} |
} |
setController(controllerInput ,controllerInputVal); |
} |
} |
voidsetController(char input, int val) { |
Serial.print('controller val: '); |
Serial.println(input); |
Serial.println(val*2); // Do something with the value |
} |
// |
//void establishContact() { |
// while (Serial.available() <= 0) { |
// Serial.print('A'); // send a capital A |
// delay(300); |
// } |
//} |
![Arduino multiple serial ports Arduino multiple serial ports](/uploads/1/2/5/3/125391842/349463688.png)
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
![](/uploads/1/2/5/3/125391842/474291458.jpg)