tommasta

Arduino-Rainbowduino I2C Master - PulseIn input replicator

Apr 24th, 2012
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. /*I haven't gotten around to properly commenting this yet.  If I don't soon, yell at me.  This code takes pulseIn input values and translates them into single-byte characters for easy transfer over I2C.*/
  2.  
  3.  
  4. #include <Wire.h>
  5. int rx1 = 0;  //throttle
  6. int rx2 = 0;  //aileron
  7. int rx3 = 0;  //elevation
  8. int rx4 = 0;  //rudder
  9. int rx5 = 0;  //AUX
  10. int rx6 = 0;  //Trainer
  11. char bx1 = 0;
  12. char bx2 = 0;
  13. char bx3 = 0;
  14. char bx4 = 0;
  15. char bx5 = 0;
  16. char bx6 = 0;
  17.  
  18.  
  19.  
  20. void setup()
  21. {
  22.   Serial.begin(115200);      //Open the serial port at 115200bps
  23.   Wire.begin();              //Join I2C bus (no address for master)
  24. }
  25.  
  26.  
  27. void loop()
  28. {
  29.   rx1 = pulseIn(2,HIGH,20000);
  30.  rx1 = constrain (rx1, 1001, 2000);
  31.  rx1 = map(rx1, 1100, 1890, 1, 125);
  32.  bx1 = char(rx1);
  33.   rx2 = pulseIn(3,HIGH,20000);
  34.  rx2 = constrain (rx2, 1001, 2000);
  35.  rx2 = map(rx2, 1100, 1890, 1, 125);
  36.  bx2 = char(rx2);
  37.   rx3 = pulseIn(4,HIGH,20000);
  38.  rx3 = constrain (rx3, 1001, 2000);
  39.  rx3 = map(rx3, 1100, 1890, 1, 125);
  40.  bx3 = char(rx3);
  41.   rx4 = pulseIn(5,HIGH,20000);
  42.  rx4 = constrain (rx4, 1001, 2000);
  43.  rx4 = map(rx4, 1100, 1890, 1, 125);
  44.  bx4 = char(rx4);
  45.   rx5 = pulseIn(6,HIGH,20000);
  46.  rx5 = constrain (rx5, 1001, 2000);
  47.  rx5 = map(rx5, 1100, 1890, 1, 125);
  48.  bx5 = char(rx5);
  49.   rx6 = pulseIn(7,HIGH,20000);
  50.  rx6 = constrain (rx6, 1001, 2000);
  51.  rx6 = map(rx6, 1100, 1890, 1, 125);
  52.  bx6 = char(rx6);
  53.     Serial.print(bx1);
  54.     Serial.print("\t");
  55.     Serial.print(bx2);
  56.     Serial.print("\t");
  57.     Serial.print(bx3);
  58.     Serial.print("\t");
  59.     Serial.print(bx4);
  60.     Serial.print("\t");
  61.     Serial.print(bx5);
  62.     Serial.print("\t");
  63.     Serial.print(bx6);
  64.     Serial.println("\t");
  65.  
  66.  
  67.  
  68.  
  69.  {
  70.     expansionWrite(bx1, bx2, bx3, bx4, bx5, bx6);  //Whenever you want to write to the
  71. }}
  72.  
  73. void expansionWrite(int value1, int value2, int value3, int value4, int value5, int value6)
  74. {
  75.   //pin = pin-100;             substracts 100 to it maps to the real ports on the expansion arduino
  76.   Wire.beginTransmission(2); //Transmit to device #2
  77. //  Wire.write(pin);            //Sends one byte stating the pin to be addressed
  78.   Wire.write((int)value1);
  79.   Wire.write((int)value2);
  80.   Wire.write((int)value3);
  81.   Wire.write((int)value4);
  82.   Wire.write((int)value5);
  83.   Wire.write((int)value6);    //Sends the value to be transmitted to the pin selected
  84.   Wire.endTransmission();    //Stop transmitting
  85. }
Advertisement
Add Comment
Please, Sign In to add comment