bipedalRobot_leg_i2c-spitter_Master.ino (1489B)
1 // Arduino Serial Example #2 Remote Control Blink - Master 2 #include <Wire.h> 3 4 #define i2cAddress 0x64 // 0x61 for Right leg, 0x62 for Left leg, 0x64 for both knees 5 bool flagRx = false; 6 7 const unsigned int MAX_MESSAGE_LENGTH = 125; 8 static char message[MAX_MESSAGE_LENGTH]; 9 bool requestCommand = false; 10 bool messageReceived = false; 11 12 void setup() 13 { 14 Wire.begin(i2cAddress); 15 Wire.onReceive(receiveEvent); 16 Wire.onRequest(requestEvent); 17 18 Serial.begin(115200); 19 // wait for the serial port to connect. Required for Leonardo/Micro native USB port only 20 while (!Serial) { ; } 21 } 22 23 void receiveEvent(int byteCount) { 24 int i; 25 requestCommand = false; 26 for (i = 0; i < byteCount; i++) { 27 char c = Wire.read(); 28 if (c == '@'){ requestCommand = true; } 29 Serial.print(c); 30 } 31 Serial.println("\0"); 32 } 33 34 void requestEvent() { 35 if (messageReceived == true){ 36 Wire.write(message); 37 messageReceived = false; 38 } 39 } 40 41 void loop() { 42 if (requestCommand == true){ 43 while (Serial.available() > 0) 44 { 45 //static char message[MAX_MESSAGE_LENGTH]; 46 static unsigned int message_pos = 0; 47 48 char inByte = Serial.read(); 49 if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) ) 50 { 51 message[message_pos] = inByte; 52 message_pos++; 53 } 54 //Full message received... 55 else 56 { 57 message[message_pos] = '\0'; 58 message_pos = 0; 59 messageReceived = true; 60 requestCommand = false; 61 } 62 } 63 } 64 }