Advertisement
Guest User

Arduino pro micro serial pass through

a guest
Jul 11th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3.  
  4.  
  5.  
  6.  
  7. void setup() {
  8.   Wire.begin(8);                // join i2c bus with address #8
  9.   Wire.onReceive(receiveEvent); // register event
  10.   Wire.onRequest(requestEvent);
  11.   Serial.begin(9600);           // start serial for output
  12.   Serial1.begin(250000);
  13.  
  14.   delay(3000);
  15.   Serial.println("hello");
  16.  
  17. }
  18.  
  19. void loop() {
  20.   delay(500);
  21.   while(Serial1.available()){
  22.     Serial.print("THERE IS INCOMING");
  23.     Serial.write(Serial1.read());
  24.   }
  25.   //Serial1.write(8);
  26.   Serial1.write(114);
  27.   Serial.println("Still running");
  28.  
  29. }
  30.  
  31. void receiveEvent(int howMany) {
  32.  
  33.   Serial.print("size of data: ");
  34.   Serial.print(howMany);
  35.  
  36.   uint8_t prefix = Wire.read(); //first byte
  37.  
  38.  
  39.   switch(prefix) {
  40.  
  41.    
  42.     case 0xAA : {
  43.      Serial.print(". string : ");
  44.      while (0 < Wire.available()) { // loop through all but the last
  45.         Serial.write(Wire.read());         // print the character
  46.      }
  47.      Serial.println();
  48.      break;
  49.     }
  50.  
  51.     case 0x55: {
  52.      
  53.       Serial.print(". binary : ");
  54.        Serial.print(prefix);
  55.       Serial.print(" ");
  56.         while (0 < Wire.available()) { // loop through all but the last
  57.           int x = Wire.read();         // receive byte as an integer
  58.           Serial.print(x, BIN);         // print the integer
  59.           Serial.print(" ");
  60.        }
  61.        Serial.println();
  62.        break;
  63.     }
  64.     default:{
  65.       Serial.print(". other : ");
  66.       Serial.print(prefix);
  67.       Serial.print(" ");
  68.       while (0 < Wire.available()) { // loop through all but the last
  69.           int x = Wire.read();         // receive byte as an integer
  70.           Serial.print(x, BIN);         // print the integer
  71.           Serial.print(" ");
  72.        }
  73.       Serial.println();
  74.     }
  75.   }
  76. }
  77.  
  78. void requestEvent() {
  79.  
  80.   Serial.println("requested");  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement