Guest User

USB-Serial Converter Arduino Mega

a guest
Jan 20th, 2013
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. /*
  2. Config begin
  3. */
  4. #define SerialSpeed 9600
  5. /*
  6. Config end
  7. */
  8.  
  9.  
  10. #define
  11.  
  12. String inputString0 = "";
  13. boolean string0Complete = false;
  14.  
  15. String inputString1 = "";
  16. boolean string1Complete = false;
  17.  
  18. void setup() {
  19. Serial.begin(SerialSpeed);
  20. Serial1.begin(SerialSpeed);
  21. inputString0.reserve(200);
  22. inputString1.reserve(200);
  23. }
  24.  
  25. void loop() {
  26. // print the string when a newline arrives:
  27. if (string0Complete) {
  28. Serial1.println(inputString0);
  29. // clear the string:
  30. inputString0 = "";
  31. string0Complete = false;
  32. }
  33.  
  34. if (string1Complete) {
  35. Serial.println(inputString1);
  36. // clear the string:
  37. inputString1 = "";
  38. string1Complete = false;
  39. }
  40. }
  41.  
  42. /*
  43. SerialEvent occurs whenever a new data comes in the
  44. hardware serial RX. This routine is run between each
  45. time loop() runs, so using delay inside loop can delay
  46. response. Multiple bytes of data may be available.
  47. */
  48. void serialEvent() {
  49. while (Serial.available()) {
  50. // get the new byte:
  51. char inChar = (char)Serial.read();
  52. // add it to the inputString:
  53. inputString0 += inChar;
  54. // if the incoming character is a newline, set a flag
  55. // so the main loop can do something about it:
  56. if (inChar == '\n') {
  57. string0Complete = true;
  58. }
  59. }
  60. }
  61.  
  62. void serialEvent1() {
  63. while (Serial1.available()) {
  64. // get the new byte:
  65. char inChar = (char)Serial1.read();
  66. // add it to the inputString:
  67. inputString1 += inChar;
  68. // if the incoming character is a newline, set a flag
  69. // so the main loop can do something about it:
  70. if (inChar == '\n') {
  71. string1Complete = true;
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment