tommasta

Arduino-Rainbowduino I2C Slave - PulseIn input replicator

Apr 24th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 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 as single-byte characters from I2C and translates them into usable integers.  This is writen specifically for the Rainbowduino, who doesn't have any inputs.*/
  2.  
  3. #include <Wire.h>
  4. #include <Rainbowduino.h>
  5. char bx1 = 0;
  6. char bx2 = 0;
  7. char bx3 = 0;
  8. char bx4 = 0;
  9. char bx5 = 0;
  10. char bx6 = 0;
  11. int rx1 = 0;  //throttle
  12. int rx2 = 0;  //aileron
  13. int rx3 = 0;  //elevation
  14. int rx4 = 0;  //rudder
  15. int rx5 = 0;  //AUX
  16. int rx6 = 0;  //Trainer
  17.  
  18.  
  19.  
  20. void setup()
  21. {
  22.   Rb.init();
  23.   Wire.begin(2);                //Join I2C bus with address #2
  24.   Wire.onReceive(expansionReceive); //Register event
  25.   Serial.begin(57600);
  26. }
  27.  
  28.  
  29. void loop()
  30. {
  31.  
  32.     Serial.print(rx1);
  33.     Serial.print("\t");
  34.     Serial.print(rx2);
  35.     Serial.print("\t");
  36.     Serial.print(rx3);
  37.     Serial.print("\t");
  38.     Serial.print(rx4);
  39.     Serial.print("\t");
  40.     Serial.print(rx5);
  41.     Serial.print("\t");
  42.     Serial.print(rx6);
  43.     Serial.println("\t");
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. void expansionReceive(int howMany)
  52. {
  53. //  int port = Wire.read();    // receive byte as an integer
  54.   char bx1 = Wire.read();   // receives the byte with the value
  55.   char bx2 = Wire.read();
  56.   char bx3 = Wire.read();
  57.   char bx4 = Wire.read();
  58.   char bx5 = Wire.read();
  59.   char bx6 = Wire.read();
  60.   rx1 = int(bx1);
  61.   rx1 = constrain (rx1, 1, 125);
  62.   rx2 = int(bx2);
  63.   rx2 = constrain (rx2, 1, 125);
  64.   rx3 = int(bx3);
  65.   rx3 = constrain (rx3, 1, 125);
  66.   rx4 = int(bx4);
  67.   rx4 = constrain (rx4, 1, 125);
  68.   rx5 = int(bx5);
  69.   rx5 = constrain (rx5, 1, 125);
  70.   rx6 = int(bx6);
  71.   rx6 = constrain (rx6, 1, 125);
  72. //  analogWrite(port,value);         // sets the pin to the desired value
  73. }
Advertisement
Add Comment
Please, Sign In to add comment