Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Serial Number Builder --- 22 February 2018
- Runs on Uno/Mega/Nano/whatever because only the PC-USB connection is used.
- The purpose of this sketch is to allow sending a numerical int or float value over the Arduino IDE Serial Monitor.
- The sketch adds 1 to the value, and returns it.
- The maximum number value is limited to four digits, plus the positive or negative sign, and the decimal.
- */
- byte byteRead; // This is used for reading the incoming byte.
- int character_counter = 0; // This is used for counting the incoming characters.
- int input_number_chars[4]; // This is used for storing the input characters.
- int decimal_counter = 0; // This is to indicate when the decimal point was entered, if one was sent.
- bool plusMinus_sign_entered = false; // This is used to make sure the user only enters one sign.
- int plusMinus_sign_counter = 0; // This is to make sure that the user entered the positive/negative sign first.
- int plusMinus_modifier = 1; // This is used to produce the positive/negative result number. This must be either 1 or -1.
- bool decimal_entered = false; // This is used to make sure the user only enters one decimal point.
- float final_value = 0; // This is where the final value gets put.
- void setup() {
- // Turn the Serial Protocol ON
- Serial.begin(9600);
- Serial.println("Type a positive or negative number up to four digits long");
- Serial.println("The number will be returned with 1 added to it");
- }
- void loop() {
- while (Serial.available()) {
- byteRead = Serial.read();
- // 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.
- //Serial.println(byteRead);
- if (byteRead > 47 && byteRead < 58) {
- // This section catches all the digits.
- if (character_counter < 5) { // This check is to make sure that the user didn't enter more than four digits.
- // Only the first four digits get used.
- input_number_chars[character_counter] = (byteRead - 48);
- // Note: 48 is the numeric value of the [zero] character
- // So this way, all of the digit characters entered get saved as their numeric value
- character_counter++;
- }
- else {
- Serial.println("Error: more than four digits entered");
- }
- }
- else if (byteRead == 43) {
- // This is to catch the + sign, if the user entered it.
- if (character_counter == 0) { // If they don't enter the +/- sign first, then it will be ignored.
- if (plusMinus_sign_entered == false) {
- plusMinus_sign_entered = true;
- plusMinus_sign_counter = character_counter;
- plusMinus_modifier = 1;
- }
- else {
- Serial.println("Error: +/- sign already entered");
- }
- }
- else {
- Serial.println("Error: +/- sign must be entered first");
- }
- }
- else if (byteRead == 45) {
- // This is to catch the - sign, if the user entered a negative number.
- if (character_counter == 0) { // If they don't enter the +/- sign first, then it will be ignored.
- if (plusMinus_sign_entered == false) {
- plusMinus_sign_entered = true;
- plusMinus_sign_counter = character_counter;
- plusMinus_modifier = -1;
- }
- else {
- Serial.println("Error: +/- sign already entered");
- }
- }
- else {
- Serial.println("Error: +/- sign must be entered first");
- }
- }
- else if (byteRead == 46) {
- // This is to catch the decimal point, if the user entered it.
- if (decimal_entered == false) {
- decimal_entered = true;
- decimal_counter = character_counter;
- }
- else {
- Serial.println("Error: decimal sign already entered");
- }
- }
- else if (byteRead == 10) { // The [end-of-line] character is the [enter] key.
- // The digits in the character array must be coverted to a numeric value.
- // Note that the digits were put in the array "backwards", with the most-significant-digit first.
- float thousands_place = 0;
- float hundreds_place = 0;
- float tens_place = 0;
- float ones_place = 0;
- switch (character_counter) {
- case 0:
- // No numbers were entered!
- break;
- case 1:
- // One digit was entered:
- ones_place = input_number_chars[0];
- break;
- case 2:
- // Two digits were entered:
- ones_place = input_number_chars[1];
- tens_place = input_number_chars[0] * 10;
- break;
- case 3:
- ones_place = input_number_chars[2];
- tens_place = input_number_chars[1] * 10;
- hundreds_place = input_number_chars[0] * 100;
- break;
- case 4:
- ones_place = input_number_chars[3];
- tens_place = input_number_chars[2] * 10;
- hundreds_place = input_number_chars[1] * 100;
- thousands_place = input_number_chars[0] * 1000;
- break;
- }
- if (decimal_entered == false) {
- // If no decimal point was entered, then it must be set to the same count as the digit count.
- decimal_counter = character_counter;
- }
- float decimal_adjust = pow(10, (character_counter - decimal_counter)); // This line creates a value to set the decimal place with.
- final_value = (thousands_place + hundreds_place + tens_place + ones_place); // This line adds the digits up.
- final_value = final_value / decimal_adjust; // This line adjusts the decimal place properly.
- final_value = final_value * plusMinus_modifier; // This line sets the positive/negative value properly.
- // Note: this is the point at which the user-entered number is now stored in [final_value].
- Serial.println(" ");
- Serial.print("Input value was: ");
- Serial.println(final_value, 4);
- final_value = final_value + 1; // Here we add 1 to it,,,,
- Serial.print("New value is: ");
- Serial.println(final_value, 4);
- // Do it over: reset the necessary values to work again.
- for (int x = 0; x < 4; x++) {
- input_number_chars[x] = 0; // Might not be necessary but anyway...
- }
- character_counter = 0;
- decimal_counter = 0;
- plusMinus_modifier = 1;
- plusMinus_sign_entered = false;
- decimal_entered = false;
- Serial.println(" ");
- Serial.println("Type a positive or negative number up to four digits long");
- Serial.println("The number will be returned with 1 added to it");
- }
- }
- } // end of main loop
Advertisement
Add Comment
Please, Sign In to add comment