Advertisement
tommasta

Arduino I2C Port Replicator Master **Not working**

Apr 15th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <Wire.h>
  2. int rx1 = 0;  //throttle
  3. int rx2 = 0;  //aileron
  4. int rx3 = 0;  //elevation
  5. int rx4 = 0;  //rudder
  6. int rx5 = 0;  //AUX
  7. int rx6 = 0;  //Trainer
  8.  
  9.  
  10. void integerToBytes(long val, byte b[4]) {    //This is the formula for conversion
  11.   b[0] = (byte )((val >> 24) & 0xff);         //from BYTE array to INT
  12.   b[1] = (byte )((val >> 16) & 0xff);         //
  13.   b[2] = (byte )((val >> 8) & 0xff);          //
  14.   b[3] = (byte )(val & 0xff);                 //
  15. }
  16.  
  17. void expansionWrite(int pin, int value)
  18. {
  19.   //pin = pin-100;             substracts 100 to it maps to the real ports on the expansion arduino
  20.   Wire.beginTransmission(2); //Transmit to device #2
  21.   Wire.write(pin);            //Sends one byte stating the pin to be addressed
  22.   Wire.write((int)value);          //Sends the value to be transmitted to the pin selected
  23.   Wire.endTransmission();    //Stop transmitting
  24. }
  25.  
  26. void setup()
  27. {
  28.   Serial.begin(115200);      //Open the serial port at 115200bps
  29.   Wire.begin();              //Join I2C bus (no address for master)
  30. }
  31.  
  32.  
  33. void loop()
  34. {
  35.  rx1 = pulseIn(2,HIGH,20000);
  36.  rx2 = pulseIn(3,HIGH,20000);
  37.  rx3 = pulseIn(4,HIGH,20000);
  38.  rx4 = pulseIn(5,HIGH,20000);
  39.  rx5 = pulseIn(6,HIGH,20000);
  40.  rx6 = pulseIn(7,HIGH,20000);
  41.     Serial.print(rx1);
  42.     Serial.print("\t");
  43.     Serial.print(rx2);
  44.     Serial.print("\t");
  45.     Serial.print(rx3);
  46.     Serial.print("\t");
  47.     Serial.print(rx4);
  48.     Serial.print("\t");
  49.     Serial.print(rx5);
  50.     Serial.print("\t");
  51.     Serial.print(rx6);
  52.     Serial.print("\t");
  53.     Serial.println("\t");
  54.  
  55.  
  56.  
  57.  
  58.   {
  59.     expansionWrite(14, rx1);  //Whenever you want to write to the
  60.     expansionWrite(15, rx2);  //other Arduino just go with something
  61.     expansionWrite(16, rx3);  //like expansionWrite(102,HIGH);
  62.     expansionWrite(17, rx4);  // That will -for example- set the pin 2
  63.     expansionWrite(18, rx5);  //on the second arduino to high. I used
  64.     expansionWrite(19, rx6);  //the 102 nomenclature only to differentiate
  65.                              //the two boards. It makes it easier and
  66.                              //avoids confusion. You will see how the
  67.                              //exceeding 100 is substracted when the time comes.
  68.   }
  69. }
  70.  
  71.  
  72. /*
  73.  
  74.   byte b[4];
  75.   integerToBytes(1056964608, b);    //This is the in-code portion
  76.   for (int i=0; i<4; ++i) {         //
  77.     Serial.println((int )b[i]);     //
  78.   }
  79.  
  80. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement