Advertisement
skizziks_53

Arduino serial monitor tester v1.0

Apr 2nd, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. /*
  2.    Reddit serial input tester
  3.    April 2, 2019
  4.    Works on any board, uses only serial monitor and pin 13 LED
  5.    to demonstrate serial monitor/keyboard input to Arduino.
  6. */
  7.  
  8.  
  9. int outPin = 13; // I switched this to pin 13 here, so I could test it without setting up more LEDs.
  10.  
  11. char character_sent = 0; // This can be declared as a byte or a char, it will work either way.​ A char variable just stores an unsigned integer with a range of zero to 255.
  12.  
  13. int temp_char = 0; // I found a bug in the Arduino IDE!.....
  14. // You should be able to print the character_sent as either the character, or the numeric value. But the IDE doesn't want to do that! See notes in the function print_stuff_back() at the bottom.
  15.  
  16.  
  17. void setup() {
  18.   pinMode(outPin, OUTPUT);
  19.   Serial.begin(9600);
  20.   digitalWrite(outPin, LOW);
  21.   Serial.println("Exiting setup()");
  22. }
  23.  
  24.  
  25. void loop() {
  26.  
  27.   while (Serial.available() != NULL) { //wait for input
  28.  
  29.     character_sent = Serial.read(); // Every time you perform a serial.read(), you remove that character from the input buffer.
  30.     //                                 So if you want to use it for more than one comparison, you need to save it into another variable.
  31.     temp_char = character_sent; // This line is copying the char value into a second variable (see notes at bottom).
  32.  
  33.     if (character_sent == '1') { // If you want to check for the character, you need to enclose the character value in single quotes.
  34.       digitalWrite(outPin, LOW);
  35.       print_stuff_back();
  36.     }
  37.     else if (character_sent == '2') {
  38.       digitalWrite(outPin, HIGH);
  39.       print_stuff_back();
  40.     }
  41.     else {
  42.       // If you write a PC program in some languages and send text, they send it as a string by default and so there is a terminiating character on it.
  43.       // So if you write a program like this, you need to have some way for it to tell you if there are non-printing characters that are getting sent.
  44.       // And you need some way to remove those non-printing characters from the Serial buffer as well.
  45.  
  46.       // To test this part: open Notepad, type the number 1 and then hit the [enter] key, and then copy and paste that text into the Arduino Serial Monitor and press [send].
  47.       // The Arduino should answer back that it got two characters: the number 49 (that represents the '1' digit) and the number 32 that represents the [enter] key.
  48.  
  49.       // Generally when you write these kinds of programs, you want to write it to catch all the specific characters you want, and it should skip/remove everything else.
  50.  
  51.       // This section will handle any character that is not '1' or '2', so that you can SEE what else gets sent...
  52.       Serial.println("Extra character sent:");
  53.       print_stuff_back();
  54.     }
  55.  
  56.     character_sent = NULL; // This 'erases' the current character value, to make sure that each character is shown only once.
  57.  
  58.   }
  59.  
  60. } // end of main loop
  61.  
  62.  
  63. void print_stuff_back() {
  64.   Serial.print("Character = [");
  65.   Serial.write(character_sent); // Serial.write() will print what the character looks like.
  66.   Serial.println("]");
  67.  
  68.   Serial.print("Char numeric value = [");
  69.   Serial.print(temp_char); // Serial.print() SHOULD print the character ASCII value,,,, -but if I used the character_sent variable, it isn't doing it--it is only printing the character itself...
  70.   //                          So I stored the character_sent value into another int variable named temp_char, BEFORE calling Serial.write() on it, and then it will display the value of temp_char as a number.
  71.   //                          This page shows how it is supposed to work:
  72.   //                                 https://stackoverflow.com/questions/46301534/in-arduino-how-to-print-character-for-givien-ascii-number
  73.   Serial.println("]");
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement