Skip to content

Instantly share code, notes, and snippets.

@biskandar
Created October 6, 2012 06:17
Show Gist options
  • Save biskandar/3844187 to your computer and use it in GitHub Desktop.
Save biskandar/3844187 to your computer and use it in GitHub Desktop.
Using I2C as Communication Link between Arduinos 2012010401
#include <Wire.h>
// preparing for which slave device
// need to be connected
int slaveId = 0 ;
void setup() {
Serial.begin( 9600 ) ;
while ( !Serial ) ;
// Run as I2C Master
Wire.begin() ;
}
void loop() {
if ( Serial.available() ) {
// console terminal read per character
char ch = Serial.read() ;
// map key '1' for slave device id #1
// map key '2' for slave device id #2
switch ( ch ) {
case '1' :
slaveId = 1 ;
Serial.println( " > connected to slave id #1" ) ;
return ;
case '2' :
slaveId = 2 ;
Serial.println( " > connected to slave id #2" ) ;
return ;
}
// try to send the character to
// the specific slave id
if ( slaveId > 0 ) {
Wire.beginTransmission( slaveId ) ;
Wire.write( ch ) ;
Wire.endTransmission() ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment