Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit serial input tester
- April 2, 2019
- Works on any board, uses only serial monitor and pin 13 LED
- to demonstrate serial monitor/keyboard input to Arduino.
- */
- int outPin = 13; // I switched this to pin 13 here, so I could test it without setting up more LEDs.
- 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.
- int temp_char = 0; // I found a bug in the Arduino IDE!.....
- // 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.
- void setup() {
- pinMode(outPin, OUTPUT);
- Serial.begin(9600);
- digitalWrite(outPin, LOW);
- Serial.println("Exiting setup()");
- }
- void loop() {
- while (Serial.available() != NULL) { //wait for input
- character_sent = Serial.read(); // Every time you perform a serial.read(), you remove that character from the input buffer.
- // So if you want to use it for more than one comparison, you need to save it into another variable.
- temp_char = character_sent; // This line is copying the char value into a second variable (see notes at bottom).
- if (character_sent == '1') { // If you want to check for the character, you need to enclose the character value in single quotes.
- digitalWrite(outPin, LOW);
- print_stuff_back();
- }
- else if (character_sent == '2') {
- digitalWrite(outPin, HIGH);
- print_stuff_back();
- }
- else {
- // 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.
- // 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.
- // And you need some way to remove those non-printing characters from the Serial buffer as well.
- // 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].
- // 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.
- // 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.
- // This section will handle any character that is not '1' or '2', so that you can SEE what else gets sent...
- Serial.println("Extra character sent:");
- print_stuff_back();
- }
- character_sent = NULL; // This 'erases' the current character value, to make sure that each character is shown only once.
- }
- } // end of main loop
- void print_stuff_back() {
- Serial.print("Character = [");
- Serial.write(character_sent); // Serial.write() will print what the character looks like.
- Serial.println("]");
- Serial.print("Char numeric value = [");
- 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...
- // 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.
- // This page shows how it is supposed to work:
- // https://stackoverflow.com/questions/46301534/in-arduino-how-to-print-character-for-givien-ascii-number
- Serial.println("]");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement