Two-way serial communication with Arduino
In this video I show you how to send and receive data between an Arduino and your computer. I show you how to use the switch-case and if() statement. I also show how to format your data to make it easier to handle on the computer.
Arduino source code
bool enablerun = false; bool newData = false; String sin1, sin2; //sin = Serial INput float num1, num2; //num = number char receivedCommand; //received character as a command int cycles = 0; void setup() { Serial.begin(9600); //define baud rate Serial.println("Testing Serial Commands"); //print a message Serial.println("m = mirror"); Serial.println("a = add two numbers"); Serial.println("c = count"); Serial.println("d = send formatted data"); Serial.println("s = stop sending data"); Serial.println("----------------------------"); } void loop() { //Comment out one of these below. Either use the Switch-Case or the if() CheckSerialSwitchCase(); //This is with Switch - Case //CheckSerialIF(); //This is with IF() //--------------------------------------------------- sendFormattedData(); //This is a function } void CheckSerialIF() //Checking the serial port via IF statements { if (Serial.available() > 0) //if something comes from the Serial. This part only checks the serial. { receivedCommand = Serial.read(); // this will read the command character newData = true; //this creates a flag and says there's a data coming } if (newData == true) //if we received something (see above) { if (receivedCommand == 'm') //this is the mirror part { sin1 = Serial.readString(); //Read one serial input Serial.print("Message was: "); Serial.println(sin1); } if (receivedCommand == 'a') //addition { //example: a 42 1 while (!Serial.available()); num1 = Serial.parseFloat(); num2 = Serial.parseFloat(); addTwoNumbers(num1, num2); } if (receivedCommand == 'c') //Counting { //example: c 5 while (!Serial.available()); num1 = Serial.parseFloat(); counterCode(num1); } if (receivedCommand == 'd') //Sending formatted data { enablerun = true; // this enables the function for sending formatted data } if (receivedCommand == 's') //immediately stops the code { enablerun = false; Serial.println("Stopped!"); } } //after we went through the above tasks, newData becomes false again, so we are ready to receive new commands again. newData = false; } void CheckSerialSwitchCase() //Checking the serial port via Switch-Case { if (Serial.available()) { //If there are bytes/characters coming through the serial port char commandcharacter = Serial.read(); //read the character for the command switch (commandcharacter) { //we read a character which can case 'm': //m = "mirror"; we send back the received stuff sin1 = Serial.readString(); //Read one serial input Serial.print("Message was: "); Serial.println(sin1); break; case 'a': //a = "addition"; we add two numbers //example: a 42 1 while (!Serial.available()); num1 = Serial.parseFloat(); num2 = Serial.parseFloat(); addTwoNumbers(num1, num2); break; case 'c': //c as count //example: c 5 while (!Serial.available()); num1 = Serial.parseFloat(); counterCode(num1); break; case 'd': //we request formatted data enablerun = true; // this enables the function for sending formatted data break; case 's': //s = stop; we make enablerun false, so we stop receiving data. enablerun = false; Serial.println("Stopped!"); break; } } } void addTwoNumbers(float sin1, float sin2)//This function adds two numbers and prints it on the serial { float result; //create a variable for storing the result result = num1 + num2; //adding the two numbers Serial.print("The result is: "); //text Serial.println(result); //result return; } void counterCode(double num1) //We count until a certain number and in every second we print a number { for(int i = 0; i<num1; i++) { delay(1000); //wait a second Serial.print(i+1); Serial.println(" seconds elapsed"); } Serial.println("Done!"); return; } void sendFormattedData()// we create a random series of data which will be transferred to the serial terminal formatted { if (enablerun == true) { Serial.print(cycles); //we just print an increasing integer Serial.print('\t'); //separate it with a tab Serial.print(millis()); //we print some increasing number, such as the cpu's time Serial.print('\t'); //separation Serial.print(millis() * 3); //some number which increases proportional to the time Serial.println(); //new line //expected output format: 1 1000 3000 delay(1000); //waiting a second cycles++; //increase the number of loops } }