Projet

Général

Profil

Feature #13630 » serial_communication.ino

outillage arduino - Anonyme, 20/11/2021 13:14

 
/*----------------Written by :-----------*
*--------------Mathis Morales-----------*
*---------------------------------------*/
#include <Wire.h>

const unsigned int MAX_MSG_LN = 1024;

void setup()
{
Serial.begin(115200);
pinMode(13,OUTPUT); //LED_BUILTIN activation

}
//Create a place to hold the incoming message
static char msg[MAX_MSG_LN];
static unsigned int msg_pos = 0;
void loop()
{
//Check to see if anything is available in the serial receive buffer
while (Serial.available())
{
//Read the next available byte in the serial receive buffer
char inByte = Serial.read();
//Message coming in (check not terminating character)
if ((inByte != '\n') && (msg_pos < MAX_MSG_LN - 1 ))
{
digitalWrite(13,HIGH); //visual identification
msg[msg_pos] = inByte;
msg_pos++;
}
//Full message received
else
{
digitalWrite(13,LOW); //visual identification
msg[msg_pos] = '\0';

//Print the message (or do other things)
Serial.println(msg);

//Reset for the next message
msg_pos = 0;
}
}
}
(4-4/5)