# Serial communication through Arduino and concepts from IB diploma
###### tags: `IB` `Computer Science` `Arduino` `Serial Communication` `C++`
[ToC]
## Introduction and previous knowledge
For using this note the student should have:
* Basic understanding of Arduino IDE (already installed)
* A bit of knowledge of C++ (understand blink.ino)
We are going to introduce and dig up a bit into the Serial communication and specifically in Arduino.
## How to use it
Before digging up exactly of what it is, let's see what it does. Let's do a hello world
```C++
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Hello world. OwO");
}
```
Try it and to see if it works you need to go to here.

In the menus you need to go to Tools: Serial monitor (control+shift+m is the shorcut)
You will see a window similar to this one (you need to have defined your port!)

If you upload the sketch you will see something like this:

You can set if you want the TimeStamp or not with the check.
## A debugging tool
Before going into a deep dive (more) into the serial let's see what are the uses of Serial: **Debugging**
Debugging is a process where you ask your algorithm to give you information while it's happening, usually variables or information of what's running.
Imagine that we have a ultrasonic generator, controlled using an Arduino. We as humans will not be able to know when it should be working or not.

*source: https://www.kemo-electronic.de/en/Car/Modules/M048N-Ultrasonic-Generator.php*
So for that use we can say "Hey, Arduino send me a signal when you put it ON or you put it OFF"
This could be the code:
```C++
bool shouldTheGeneratorBeON = false;
void setup() {
setupFunctionsToPutTheGeneratorToWork();
Serial.begin(9600);
}
void loop() {
readConditions();
if(shouldTheGeneratorBeON) {
switchTheGeneratorON();
Serial.println("The generator is ON")
}
else {
switchTheGeneratorOFF();
Serial.println("The generator is OFF")
}
}
```
Remember that in this case you don't know the implementation of "setupFunctionsToPutTheGeneratorToWork", "switchTheGeneratorON", "switchTheGeneratorOFF"
### Serial echo
https://gist.github.com/Protoneer/96db95bfb87c3befe46e
### Next step
Making a first message (doesn't work)
```C++
int incomingByte = 0; // for incoming serial data
bool hasTheMessageStarted = false;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
if (hasTheMessageStarted == false){
Serial.print("You have sent: ");
hasTheMessageStarted = true;
}
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print((char)incomingByte);
}
else {
hasTheMessageStarted = false;
}
}
```
```C++
int incomingByte = 0; // for incoming serial data
bool hasTheMessageStarted = false;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
delay(100);
if (!hasTheMessageStarted){
Serial.print("You have sent: ");
hasTheMessageStarted = true;
}
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print((char)incomingByte);
}
else {
if (hasTheMessageStarted) {
hasTheMessageStarted = false;
Serial.println("This was the final part of the text");
}
}
}
```
```
int incomingByte = 0; // for incoming serial data
bool hasTheMessageStarted = false;
char incomingCharacter;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
delay(100);
if (!hasTheMessageStarted){
Serial.print("You have sent: ");
hasTheMessageStarted = true;
}
// read the incoming byte:
incomingByte = Serial.read();
incomingCharacter = (char)incomingByte;
if ( incomingCharacter == 'L') {
Serial.print("LLLLL");
}
// say what you got:
Serial.print((char)incomingByte);
}
else {
if (hasTheMessageStarted) {
hasTheMessageStarted = false;
Serial.println("This was the final part of the text");
}
}
}
```
```C++
int incomingByte = 0; // for incoming serial data
bool hasTheMessageStarted = false;
char incomingCharacter;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
if (!hasTheMessageStarted){
delay(100);
Serial.print("You have sent: ");
hasTheMessageStarted = true;
}
// read the incoming byte:
incomingByte = Serial.read();
incomingCharacter = (char)incomingByte;
if ( (String) incomingCharacter == "L") { // 'L'
Serial.print("LLLLL");
}
// say what you got:
Serial.print((char)incomingByte);
}
else {
if (hasTheMessageStarted) {
hasTheMessageStarted = false;
Serial.println("This was the final part of the text");
}
}
}
```
We're refactoring so the program is the same but we are change how
```C++
int incomingByte = 0; // for incoming serial data
bool hasTheMessageStarted = false;
char incomingCharacter;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
if (!hasTheMessageStarted){
delay(100);
Serial.print("You have sent: ");
hasTheMessageStarted = true;
}
// read the incoming byte:
incomingByte = Serial.read();
incomingCharacter = (char)incomingByte;
if ( incomingCharacter == '/') {
Serial.println("This was the final part of the mesage");
}
processLetter(incomingCharacter);
}
else {
if (hasTheMessageStarted) {
hasTheMessageStarted = false;
}
}
}
void processLetter(char incomingCharacter) {
// say what you got:
Serial.print(incomingCharacter);
}
```
David => tututututututututu
```C++
int incomingByte = 0; // for incoming serial data
bool hasTheMessageStarted = false;
char incomingCharacter;
bool theMessageHasBeenProcessed = false;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
if (!hasTheMessageStarted){
delay(100);
Serial.print("You have sent: ");
hasTheMessageStarted = true;
}
// read the incoming byte:
incomingByte = Serial.read();
incomingCharacter = (char)incomingByte;
if ( incomingCharacter == '/') {
Serial.println("This was the final part of the mesage");
theMessageHasBeenProcessed = true;
}
if (!theMessageHasBeenProcessed) {
processLetter(incomingCharacter);
}
}
else {
if (hasTheMessageStarted) {
hasTheMessageStarted = false;
theMessageHasBeenProcessed = false;
}
}
}
void processLetter(char incomingCharacter) {
// say what you got:
switch (incomingCharacter) {
case 'L': // l from lion upper case
Serial.print("You printed a L");
break;
case 'l': // l from lion lower case
Serial.print("lalala");
break;
default:
Serial.print("tutu");
break;
}
}
```
```C++
// C++ code
//
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
}
//Alphabet by Sofya Arakelyan tweaked by David Prieto
void morseA () {
dot();
dash();
}
void morseB () {
dash();
dot();
dot();
dot();
}
void morseC () {
dash();
dot();
dash();
dot();
}
void morseD () {
dash();
dot();
dot();
}
void morseE () {
dot();
}
void morseF () {
dot();
dot();
dash();
dot();
}
void morseG () {
dash();
dash();
dot();
}
void morseH () {
dot();
dot();
dot();
dot();
}
void morseI () {
dot();
dot();
}
void morseJ () {
dot();
dash();
dash();
dash();
}
void morseK() {
dash();
dot();
dash();
}
void morseL() {
dot();
dash();
dot();
dot();
}
void morseM() {
dash();
dash();
}
void morseN() {
dash();
dot();
}
void morseO() {
dash();
dash();
dash();
}
void morseP() {
dot();
dash();
dash();
dot();
}
void morseQ() {
dash();
dash();
dot();
dash();
}
void morseR() {
dot();
dash();
dot();
}
void morseS() {
dot();
dot();
dot();
}
void morseT() {
dash();
}
void morseU() {
dot();
dot();
dash();
dash();
}
void morseV() {
dot();
dot();
dot();
dash();
}
void morseW() {
dot();
dash();
dash();
}
void morseX() {
dash();
dot();
dot();
dash();
}
void morseY() {
dash();
dot();
dash();
dash();
}
void morseZ() {
dash();
dash();
dot();
dot();
}
void morseSlash() {
dash();
dot();
dot();
dash();
dot();
}
void dot()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
void dash()
{
digitalWrite(LED_BUILTIN, HIGH);
delay(750);
digitalWrite(LED_BUILTIN, LOW);
delay(250);
}
````