Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Serial Message Spy ver. 1.0 -------- 26 January 2018
- Arduino Uno sketch for 16x2 LCD shield with keypad
- This sketch is for capturing and displaying inbound and outbound serial messages from another Arduino board.
- Based on example sketch provided at:
- https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)#Example_use_of_LiquidCrystal_library
- Notes:
- 1. I used a generic 16x2 LCD button keypad shield I bought online, manufacturer unknown.
- These use the normal Arduino 16x2 LCD library and five resistive buttons monitored by one analog input pin.
- Other LCD+keypad boards that use I2C may require code editing.
- 2. Note that I had to use the second set of button (adc_key) values provided, not the first set.
- 3. The sketch at the link above should be loaded and run first, to make sure the button adc levels are set correctly.
- */
- /*
- INSTRUCTIONS FOR USE
- This sketch is for monitoring incoming and outgoing serial messages.
- The incoming messages can be received from the normal USB port.
- This allows partial testing directly on the connected computer, as you are loading the sketch.
- If you want to monitor USB messages of another Arduino board:
- While the boards are unpowered,
- 1. connect the grounds of both boards together.
- 2. connect the pins D0 on both boards together, and
- 3. connect pin D2 of this board to D1 of the other board.
- Use short wires if possible.
- This will allow the Arduino that this sketch is running on, to monitor the messages that the other Arduino sends and receives.
- If the arduino that is monitoring is connected to a PC while it is used for monitoring another board,
- do not send any USB messages to it because there is no way to discriminate between the source of the message.
- There are four different screens. The UP and DOWN buttons scroll through the separate screens.
- The first screen allows you to set the baud rate to any of the standard Arduino rates. LEFT and RIGHT changes the baud rate.
- The serial ports reset to the selected speed whenever you change the speed.
- The message storage capacity for incoming and outgoing messages is 128 characters for each.
- If a message goes past the 128-character limit, the sketch will automatically stop detecting additional characters
- and change to the associated message display screen.
- The second screen shows the characters from any incoming message that are received. At the top is the total count of the characters received.
- The message array index number and character ASCII code is displayed on the left, and the character is visually displayed on the right inside brackets.
- Note that many ASCII characters are non-printing and may not be recognizable visually.
- The LEFT and RIGHT buttons scroll backward and forward through the 128-character storage array.
- The SELECT button will erase the storage array (set all the values back to zero).
- The third screen is for displaying the outgoing messages. It works just like the second screen.
- The fourth screen is for enabling and disabling monitoring. LEFT and RIGHT disables and enables the serial monitoring function.
- By default the monitoring begins disabled when the sketch starts.
- */
- //Sample using LiquidCrystal library ------------ (Note: this is the standard Arduino liquidcrystal library)
- #include <LiquidCrystal.h>
- #include <SoftwareSerial.h>
- // The hardware Serial communicates on digital pins 0 (RX) and 1 (TX).
- // We are using the normal serial input pin 0 [digital zero] to catch outgoing messages from the normal USB-connected computer.
- SoftwareSerial mySerial(2, 3); // RX, TX -->pin D2 is for messages sent from the other Arduino board.
- // To use this to monitor the hardware serial connection of another Arduino board:
- // The pin D0 of this board should be connected to the pin D0 of the other Arduino board.
- // The pin D2 of this board should be connected to the pin D1 of the other Arduino board.
- // select the pins used on the LCD panel
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- // define some values used by the panel and buttons
- int lcd_key = 0;
- int adc_key_in = 0;
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- long baud_rates[] = {300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 115200};
- int selected_baud_rate = 5; // default = 5 (9600), maximum = 11
- byte incoming_charCodes[128]; // This is an array for storing up to 128 incoming message characters.
- int incoming_index = 0; // the index into the above array (also indicates how many bytes have been received)
- int incoming_index_display = 0; // the index that is being displayed on screen #2
- bool incoming_limit_reached = false;
- byte outgoing_charCodes[128]; // This is an array for storing up to 128 outgoing message characters.
- int outgoing_index = 0; // the index into the above array (also indicates how many bytes have been received)
- int outgoing_index_display = 0; // the index that is being displayed on screen #3
- bool outgoing_limit_reached = false;
- bool monitor_enabled = false; // This is a switch to allow turning the monitoring functions on and off.
- int screen_ID = 1; // the selected screen to view
- // 1 = set baud rate, 2 = view incoming messages, 3 = view outgoing messages
- int button_debounce = 500; // the time in milliseconds to debounce the buttons
- unsigned long buttonPress_beginTime = 0;
- unsigned long buttonPress_currentTime = 0;
- bool buttons_enabled = true;
- // function declarations
- void screen_draw();
- void start_button_debouncer();
- void reset_serialPort_speeds();
- void clear_LCD();
- int read_LCD_buttons();
- void screen1_buttonPressed();
- void screen1_draw();
- void screen2_buttonPressed();
- void screen2_draw();
- void screen3_buttonPressed();
- void screen3_draw();
- void screen4_buttonPressed();
- void screen4_draw();
- void setup()
- {
- lcd.begin(16, 2); // start the library
- Serial.begin(baud_rates[selected_baud_rate]);
- mySerial.begin(baud_rates[selected_baud_rate]);
- // reset message
- clear_LCD();
- lcd.setCursor(0, 0);
- lcd.print("Resetting...");
- delay(1000);
- // set menu on #1 and draw
- screen_ID = 1;
- screen1_draw();
- }
- void loop() {
- if (monitor_enabled == true) {
- if (incoming_limit_reached == false) {
- if (Serial.available()) {
- // This is to catch incoming messages to the other Arduino board.
- incoming_charCodes[incoming_index] = Serial.read();
- incoming_index = incoming_index + 1;
- if (incoming_index > 127) {
- incoming_limit_reached = true;
- // change to the screen to read incoming messages.
- screen_ID = 2;
- screen_draw();
- }
- }
- }
- if (outgoing_limit_reached == false) {
- if (mySerial.available()) {
- // This is to catch outgoing messages from the other Arduino board.
- outgoing_charCodes[outgoing_index] = mySerial.read();
- outgoing_index = outgoing_index + 1;
- if (outgoing_index > 127) {
- outgoing_limit_reached = true;
- // change to the screen to read outgoing messages.
- screen_ID = 3;
- screen_draw();
- }
- }
- }
- }
- if (buttons_enabled == true) {
- lcd_key = read_LCD_buttons();
- if (lcd_key != btnNONE) {
- switch (screen_ID) {
- case 1:
- screen1_buttonPressed();
- break;
- case 2:
- screen2_buttonPressed();
- break;
- case 3:
- screen3_buttonPressed();
- break;
- case 4:
- screen4_buttonPressed();
- break;
- }
- start_button_debouncer();
- }
- }
- else {
- buttonPress_currentTime = millis();
- if (buttonPress_currentTime > buttonPress_beginTime) {
- if (buttonPress_currentTime >= (buttonPress_beginTime + button_debounce)) {
- buttons_enabled = true;
- }
- }
- else {
- buttonPress_beginTime = millis(); // rollover condition
- }
- }
- } // end of main loop
- void screen_draw() {
- switch (screen_ID) {
- case 1:
- screen1_draw();
- break;
- case 2:
- screen2_draw();
- break;
- case 3:
- screen3_draw();
- break;
- case 4:
- screen4_draw();
- break;
- }
- }
- void start_button_debouncer() {
- buttonPress_beginTime = millis();
- buttons_enabled = false;
- }
- void reset_serialPort_speeds() {
- Serial.end();
- mySerial.end();
- Serial.begin(baud_rates[selected_baud_rate]);
- mySerial.begin(baud_rates[selected_baud_rate]);
- }
- void clear_LCD() {
- lcd.setCursor(0, 0);
- lcd.print(" ");
- lcd.setCursor(0, 1);
- lcd.print(" ");
- }
- // read the buttons
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0); // read the value from the sensor
- // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
- // we add approx 50 to those values and check to see if we are close
- if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
- // For V1.1 us this threshold
- /*
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 250) return btnUP;
- if (adc_key_in < 450) return btnDOWN;
- if (adc_key_in < 650) return btnLEFT;
- if (adc_key_in < 850) return btnSELECT;
- */
- // For V1.0 comment the other threshold and use the one below:
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 195) return btnUP;
- if (adc_key_in < 380) return btnDOWN;
- if (adc_key_in < 555) return btnLEFT;
- if (adc_key_in < 790) return btnSELECT;
- return btnNONE; // when all others fail, return this...
- }
- void screen1_buttonPressed() {
- switch (lcd_key)
- {
- case btnUP:
- // No function on screen #1.
- break;
- case btnDOWN:
- // change to screen #2.
- screen_ID = 2;
- screen_draw();
- break;
- case btnRIGHT:
- // set the baud rate one speed higher.
- if (selected_baud_rate < 11) {
- selected_baud_rate++;
- reset_serialPort_speeds();
- screen_draw();
- }
- break;
- case btnLEFT:
- // set the baud rate one speed lower.
- if (selected_baud_rate > 0) {
- selected_baud_rate--;
- reset_serialPort_speeds();
- screen_draw();
- }
- break;
- case btnSELECT:
- // no function on screen #1.
- break;
- }
- }
- void screen1_draw() {
- clear_LCD();
- lcd.setCursor(0, 0);
- lcd.print("Baud rate:");
- lcd.setCursor(0, 1);
- lcd.print(baud_rates[selected_baud_rate]);
- }
- void screen2_buttonPressed() {
- switch (lcd_key)
- {
- case btnUP:
- // change to screen #1.
- screen_ID = 1;
- screen_draw();
- break;
- case btnDOWN:
- // change to screen #3.
- screen_ID = 3;
- screen_draw();
- break;
- case btnRIGHT:
- // set the incoming message character display one place higher.
- if (incoming_index_display < 127) {
- incoming_index_display++;
- screen_draw();
- }
- break;
- case btnLEFT:
- // set the incoming message character display one place lower.
- if (incoming_index_display > 0) {
- incoming_index_display--;
- screen_draw();
- }
- break;
- case btnSELECT:
- // clear out the incoming message display array.
- for (int x = 0; x < 128; x++) {
- incoming_charCodes[x] = 0;
- }
- incoming_limit_reached = false;
- incoming_index = 0;
- incoming_index_display = 0;
- clear_LCD();
- lcd.setCursor(0, 0);
- lcd.print("inc message:");
- lcd.setCursor(0, 1);
- lcd.print("erasing");
- delay(500);
- screen_draw();
- break;
- }
- }
- void screen2_draw() {
- clear_LCD();
- lcd.setCursor(0, 0);
- lcd.print("inc mess= ");
- lcd.print(incoming_index);
- lcd.setCursor(0, 1);
- lcd.print("#");
- lcd.print(incoming_index_display);
- lcd.print("=");
- lcd.print(incoming_charCodes[incoming_index_display]);
- lcd.setCursor(8, 1);
- lcd.print("[");
- char myChar = incoming_charCodes[incoming_index_display];
- lcd.print(myChar);
- lcd.print("]");
- }
- void screen3_buttonPressed() {
- switch (lcd_key) {
- case btnUP:
- // change to screen #2.
- screen_ID = 2;
- screen_draw();
- break;
- case btnDOWN:
- // change to screen #4.
- screen_ID = 4;
- screen_draw();
- break;
- case btnRIGHT:
- // set the incoming message character display one place higher.
- if (outgoing_index_display < 127) {
- outgoing_index_display++;
- screen_draw();
- }
- break;
- case btnLEFT:
- // set the incoming message character display one place lower.
- if (outgoing_index_display > 0) {
- outgoing_index_display--;
- screen_draw();
- }
- break;
- case btnSELECT:
- // clear out the incoming message display array.
- for (int x = 0; x < 128; x++) {
- outgoing_charCodes[x] = 0;
- }
- outgoing_limit_reached = false;
- outgoing_index = 0;
- outgoing_index_display = 0;
- clear_LCD();
- lcd.setCursor(0, 0);
- lcd.print("inc message:");
- lcd.setCursor(0, 1);
- lcd.print("erasing");
- delay(500);
- screen_draw();
- break;
- }
- }
- void screen3_draw() {
- clear_LCD();
- lcd.setCursor(0, 0);
- lcd.print("out mess= ");
- lcd.print(outgoing_index);
- lcd.setCursor(0, 1);
- lcd.print("#");
- lcd.print(outgoing_index_display);
- lcd.print("=");
- lcd.print(outgoing_charCodes[outgoing_index_display]);
- lcd.setCursor(8, 1);
- lcd.print("[");
- char myChar = outgoing_charCodes[outgoing_index_display];
- lcd.print(myChar);
- lcd.print("]");
- }
- void screen4_buttonPressed() {
- switch (lcd_key) {
- case btnUP:
- // change to screen #3.
- screen_ID = 3;
- screen_draw();
- break;
- case btnDOWN:
- // no function.
- break;
- case btnRIGHT:
- // enable the message monitor.
- monitor_enabled = true;
- screen_draw();
- break;
- case btnLEFT:
- // disable the message monitor.
- monitor_enabled = false;
- screen_draw();
- break;
- case btnSELECT:
- // no function.
- break;
- }
- }
- void screen4_draw() {
- clear_LCD();
- lcd.setCursor(0, 0);
- lcd.print("monitor:");
- lcd.setCursor(0, 1);
- if (monitor_enabled == false) {
- lcd.print("disabled");
- }
- else {
- lcd.print("enabled");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement