Advertisement
CuriousScientist

Simple Arduino Serial Communication

Oct 16th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to the following tutorial video: https://youtu.be/feAcAiexzkM
  3.  
  4. bool enablerun = false;
  5. bool newData = false;
  6. String sin1, sin2; //sin = Serial INput
  7. float num1, num2; //num = number
  8. char receivedCommand; //received character as a command
  9. int cycles = 0;
  10.  
  11. void setup()
  12. {
  13.   Serial.begin(9600); //define baud rate
  14.   Serial.println("Testing Serial Commands"); //print a message
  15.  
  16.  
  17.   Serial.println("m = mirror");
  18.   Serial.println("a = add two numbers");
  19.   Serial.println("c = count");
  20.   Serial.println("d = send formatted data");
  21.   Serial.println("s = stop sending data");
  22.   Serial.println("----------------------------");
  23. }
  24.  
  25. void loop()
  26. {
  27.   //Comment out one of these below. Either use the Switch-Case or the if()
  28.   CheckSerialSwitchCase(); //This is with Switch - Case
  29.  
  30.   //CheckSerialIF(); //This is with IF()
  31.   //---------------------------------------------------
  32.  
  33.  
  34.   sendFormattedData(); //This is a function
  35.  
  36. }
  37.  
  38.  
  39. void CheckSerialIF() //Checking the serial port via IF statements
  40. {
  41.   if (Serial.available() > 0) //if something comes from the Serial. This part only checks the serial.
  42.   {    
  43.     receivedCommand = Serial.read(); // this will read the command character
  44.     newData = true; //this creates a flag and says there's a data coming
  45.   }
  46.  
  47.   if (newData == true) //if we received something (see above)
  48.   {
  49.    
  50.     if (receivedCommand == 'm') //this is the mirror part
  51.     {
  52.       sin1 = Serial.readString(); //Read one serial input
  53.  
  54.       Serial.print("Message was: ");
  55.       Serial.println(sin1);
  56.  
  57.     }
  58.    
  59.     if (receivedCommand == 'a') //addition
  60.     {      
  61.       //example: a 42 1
  62.       while (!Serial.available());
  63.       num1 = Serial.parseFloat();
  64.  
  65.       num2 = Serial.parseFloat();
  66.      
  67.       addTwoNumbers(num1, num2);
  68.  
  69.     }
  70.    
  71.     if (receivedCommand == 'c') //Counting
  72.     {
  73.       //example: c 5
  74.       while (!Serial.available());      
  75.       num1 = Serial.parseFloat();
  76.  
  77.       counterCode(num1);
  78.  
  79.     }
  80.  
  81.     if (receivedCommand == 'd') //Sending formatted data
  82.     {
  83.       enablerun = true; // this enables the function for sending formatted data
  84.     }
  85.  
  86.    
  87.     if (receivedCommand == 's') //immediately stops the code
  88.     {
  89.       enablerun = false;
  90.       Serial.println("Stopped!");
  91.  
  92.     }
  93.    
  94.   }
  95.   //after we went through the above tasks, newData becomes false again, so we are ready to receive new commands again.
  96.   newData = false;
  97. }
  98.  
  99. void CheckSerialSwitchCase() //Checking the serial port via Switch-Case
  100. {
  101.   if (Serial.available()) { //If there are bytes/characters coming through the serial port
  102.  
  103.     char commandcharacter = Serial.read(); //read the character for the command
  104.    
  105.     switch (commandcharacter) { //we read a character which can
  106.    
  107.     case 'm': //m = "mirror"; we send back the received stuff
  108.  
  109.       sin1 = Serial.readString(); //Read one serial input
  110.  
  111.       Serial.print("Message was: ");
  112.       Serial.println(sin1);
  113.      
  114.       break;
  115.  
  116.     case 'a': //a = "addition"; we add two numbers      
  117.       //example: a 42 1
  118.       while (!Serial.available());
  119.      
  120.       num1 = Serial.parseFloat();
  121.  
  122.       num2 = Serial.parseFloat();
  123.      
  124.       addTwoNumbers(num1, num2);
  125.      
  126.       break;
  127.      
  128.  
  129.     case 'c': //c as count
  130.       //example: c 5
  131.       while (!Serial.available());
  132.            
  133.       num1 = Serial.parseFloat();
  134.  
  135.       counterCode(num1);
  136.       break;
  137.    
  138.      
  139.     case 'd': //we request formatted data
  140.      
  141.       enablerun = true; // this enables the function for sending formatted data      
  142.  
  143.     break;
  144.  
  145.     case 's': //s = stop; we make enablerun false, so we stop receiving data.
  146.  
  147.       enablerun = false;
  148.       Serial.println("Stopped!");
  149.  
  150.     break;
  151.     }
  152.   }
  153. }
  154.  
  155.  
  156. void addTwoNumbers(float sin1, float sin2)//This function adds two numbers and prints it on the serial
  157. {
  158.   float result; //create a variable for storing the result
  159.  
  160.   result = num1 + num2; //adding the two numbers
  161.  
  162.   Serial.print("The result is: "); //text
  163.   Serial.println(result); //result
  164.  
  165.   return;
  166. }
  167.  
  168. void counterCode(double num1) //We count until a certain number and in every second we print a number
  169. {
  170.   for(int i = 0; i<num1; i++)
  171.   {
  172.     delay(1000); //wait a second
  173.     Serial.print(i+1);
  174.     Serial.println(" seconds elapsed");
  175.   }
  176.   Serial.println("Done!");
  177.   return;
  178. }
  179.  
  180. void sendFormattedData()// we create a random series of data which will be transferred to the serial terminal formatted
  181. {
  182.  
  183.  
  184.   if (enablerun == true)
  185.   {
  186.     Serial.print(cycles);  //we just print an increasing integer
  187.  
  188.     Serial.print('\t'); //separate it with a tab
  189.  
  190.     Serial.print(millis()); //we print some increasing number, such as the cpu's time
  191.  
  192.     Serial.print('\t'); //separation
  193.  
  194.     Serial.print(millis() * 3); //some number which increases proportional to the time
  195.  
  196.     Serial.println(); //new line
  197.    
  198.     //expected output format: 1 1000  3000
  199.  
  200.     delay(1000); //waiting a second
  201.     cycles++; //increase the number of loops
  202.   }  
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement