skizziks_53

Serial Number Builder 1.0

Feb 22nd, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.59 KB | None | 0 0
  1. /*
  2.   Serial Number Builder --- 22 February 2018
  3.   Runs on Uno/Mega/Nano/whatever because only the PC-USB connection is used.
  4.  
  5.   The purpose of this sketch is to allow sending a numerical int or float value over the Arduino IDE Serial Monitor.
  6.   The sketch adds 1 to the value, and returns it.
  7.   The maximum number value is limited to four digits, plus the positive or negative sign, and the decimal.
  8. */
  9.  
  10. byte byteRead; // This is used for reading the incoming byte.
  11. int character_counter = 0; // This is used for counting the incoming characters.
  12. int input_number_chars[4]; // This is used for storing the input characters.
  13. int decimal_counter = 0; // This is to indicate when the decimal point was entered, if one was sent.
  14. bool plusMinus_sign_entered = false; // This is used to make sure the user only enters one sign.
  15. int plusMinus_sign_counter = 0; // This is to make sure that the user entered the positive/negative sign first.
  16. int plusMinus_modifier = 1; // This is used to produce the positive/negative result number. This must be either 1 or -1.
  17. bool decimal_entered = false; // This is used to make sure the user only enters one decimal point.
  18. float final_value = 0; // This is where the final value gets put.
  19.  
  20. void setup() {
  21.   // Turn the Serial Protocol ON
  22.   Serial.begin(9600);
  23.   Serial.println("Type a positive or negative number up to four digits long");
  24.   Serial.println("The number will be returned with 1 added to it");
  25.  
  26. }
  27.  
  28. void loop() {
  29.   while (Serial.available()) {
  30.  
  31.     byteRead = Serial.read();
  32.  
  33.     // This sketch only accepts digits, the + and - sign and the decimal as inputs, and returns the value when the enter key char[10] is encountered.
  34.     //Serial.println(byteRead);
  35.     if (byteRead > 47 && byteRead < 58) {
  36.       // This section catches all the digits.
  37.       if (character_counter < 5) { // This check is to make sure that the user didn't enter more than four digits.
  38.         // Only the first four digits get used.
  39.         input_number_chars[character_counter] = (byteRead - 48);
  40.         // Note: 48 is the numeric value of the [zero] character
  41.         // So this way, all of the digit characters entered get saved as their numeric value
  42.         character_counter++;
  43.       }
  44.       else {
  45.         Serial.println("Error: more than four digits entered");
  46.       }
  47.     }
  48.     else if (byteRead == 43) {
  49.       // This is to catch the + sign, if the user entered it.
  50.       if (character_counter == 0) { // If they don't enter the +/- sign first, then it will be ignored.
  51.         if (plusMinus_sign_entered == false) {
  52.           plusMinus_sign_entered = true;
  53.           plusMinus_sign_counter = character_counter;
  54.           plusMinus_modifier = 1;
  55.         }
  56.         else {
  57.           Serial.println("Error: +/- sign already entered");
  58.         }
  59.       }
  60.       else {
  61.         Serial.println("Error: +/- sign must be entered first");
  62.       }
  63.  
  64.     }
  65.     else if (byteRead == 45) {
  66.       // This is to catch the - sign, if the user entered a negative number.
  67.       if (character_counter == 0) { // If they don't enter the +/- sign first, then it will be ignored.
  68.         if (plusMinus_sign_entered == false) {
  69.           plusMinus_sign_entered = true;
  70.           plusMinus_sign_counter = character_counter;
  71.           plusMinus_modifier = -1;
  72.         }
  73.         else {
  74.           Serial.println("Error: +/- sign already entered");
  75.         }
  76.       }
  77.       else {
  78.         Serial.println("Error: +/- sign must be entered first");
  79.       }
  80.     }
  81.     else if (byteRead == 46) {
  82.       // This is to catch the decimal point, if the user entered it.
  83.       if (decimal_entered == false) {
  84.         decimal_entered = true;
  85.         decimal_counter = character_counter;
  86.       }
  87.       else {
  88.         Serial.println("Error: decimal sign already entered");
  89.       }
  90.     }
  91.     else if (byteRead == 10) { // The [end-of-line] character is the [enter] key.
  92.       // The digits in the character array must be coverted to a numeric value.
  93.       // Note that the digits were put in the array "backwards", with the most-significant-digit first.
  94.       float thousands_place = 0;
  95.       float hundreds_place = 0;
  96.       float tens_place = 0;
  97.       float ones_place = 0;
  98.       switch (character_counter) {
  99.         case 0:
  100.           // No numbers were entered!
  101.           break;
  102.         case 1:
  103.           // One digit was entered:
  104.           ones_place = input_number_chars[0];
  105.           break;
  106.         case 2:
  107.           // Two digits were entered:
  108.           ones_place = input_number_chars[1];
  109.           tens_place = input_number_chars[0] * 10;
  110.           break;
  111.         case 3:
  112.           ones_place = input_number_chars[2];
  113.           tens_place = input_number_chars[1] * 10;
  114.           hundreds_place = input_number_chars[0] * 100;
  115.           break;
  116.         case 4:
  117.           ones_place = input_number_chars[3];
  118.           tens_place = input_number_chars[2] * 10;
  119.           hundreds_place = input_number_chars[1] * 100;
  120.           thousands_place = input_number_chars[0] * 1000;
  121.           break;
  122.       }
  123.  
  124.       if (decimal_entered == false) {
  125.         // If no decimal point was entered, then it must be set to the same count as the digit count.
  126.         decimal_counter = character_counter;
  127.       }
  128.       float decimal_adjust = pow(10, (character_counter - decimal_counter)); // This line creates a value to set the decimal place with.
  129.  
  130.       final_value = (thousands_place + hundreds_place + tens_place + ones_place); // This line adds the digits up.
  131.  
  132.       final_value = final_value / decimal_adjust; // This line adjusts the decimal place properly.
  133.  
  134.       final_value = final_value * plusMinus_modifier; // This line sets the positive/negative value properly.
  135.  
  136.       // Note: this is the point at which the user-entered number is now stored in [final_value].
  137.  
  138.       Serial.println(" ");
  139.       Serial.print("Input value was: ");
  140.       Serial.println(final_value, 4);
  141.  
  142.       final_value = final_value + 1; // Here we add 1 to it,,,,
  143.      
  144.       Serial.print("New value is: ");
  145.       Serial.println(final_value, 4);
  146.  
  147.       // Do it over: reset the necessary values to work again.
  148.       for (int x = 0; x < 4; x++) {
  149.         input_number_chars[x] = 0; // Might not be necessary but anyway...
  150.       }
  151.       character_counter = 0;
  152.       decimal_counter = 0;
  153.       plusMinus_modifier = 1;
  154.       plusMinus_sign_entered = false;
  155.       decimal_entered = false;
  156.       Serial.println(" ");
  157.       Serial.println("Type a positive or negative number up to four digits long");
  158.       Serial.println("The number will be returned with 1 added to it");
  159.     }
  160.  
  161.   }
  162.  
  163. } // end of main loop
Advertisement
Add Comment
Please, Sign In to add comment