SHARE
TWEET

TWI Slave

a guest Feb 1st, 2014 40 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Written by Nick Gammon
  2. // Date: 18th February 2011
  3. // Modified by Synthetic Physical Computing
  4. // Date: 1st Febuary 2014
  5.  
  6. #include <Wire.h>
  7.  
  8. const byte MY_ADDRESS = 0x42;
  9.  
  10. // various commands we might get
  11.  
  12. enum {
  13.     CMD_ID = 1,
  14.     CMD_READ_A0  = 2,
  15.     CMD_READ_A1  = 3,
  16.     CMD_READ_A2  = 4,
  17.     CMD_READ_A3  = 5,
  18.     CMD_READ_D2  = 6,
  19.     CMD_READ_D3  = 7,
  20.     CMD_READ_D4  = 8,
  21.     CMD_READ_D5  = 9,
  22.     };
  23.  
  24. char command;
  25.  
  26. void setup()
  27.   {
  28.   command = 0;
  29.  
  30.   pinMode (2, INPUT);
  31.   digitalWrite (2, HIGH);  // enable pull-up
  32.   pinMode (3, INPUT);
  33.   digitalWrite (3, HIGH);  // enable pull-up
  34.   pinMode (4, INPUT);
  35.   digitalWrite (4, HIGH);  // enable pull-up
  36.   pinMode (5, INPUT);
  37.   digitalWrite (5, HIGH);  // enable pull-up
  38.  
  39.   pinMode (A0, INPUT);
  40.   digitalWrite (A0, LOW);  // disable pull-up
  41.   pinMode (A1, INPUT);
  42.   digitalWrite (A1, LOW);  // disable pull-up
  43.   pinMode (A2, INPUT);
  44.   digitalWrite (A2, LOW);  // disable pull-up
  45.   pinMode (A3, INPUT);
  46.   digitalWrite (A3, LOW);  // disable pull-up
  47.  
  48.   Wire.begin (MY_ADDRESS);
  49.   Wire.onReceive (receiveEvent);  // interrupt handler for incoming messages
  50.   Wire.onRequest (requestEvent);  // interrupt handler for when data is wanted
  51.  
  52.   }  // end of setup
  53.  
  54.  
  55. void loop()
  56.   {
  57.   // all done by interrupts
  58.   }  // end of loop
  59.  
  60. void receiveEvent (int howMany)
  61.   {
  62.   command = Wire.read ();  // remember command for when we get request
  63.   } // end of receiveEvent
  64.  
  65.  
  66. void sendSensor (const byte which)
  67.   {
  68.   int val = analogRead (which);
  69.   byte buf [2];
  70.  
  71.     buf [0] = val >> 8;
  72.     buf [1] = val & 0xFF;
  73.     Wire.write (buf, 2);
  74.   }  // end of sendSensor
  75.  
  76. void requestEvent ()
  77.   {
  78.   switch (command)
  79.      {
  80.      case CMD_ID:      Wire.write (MY_ADDRESS); break;   // send our ID
  81.      case CMD_READ_A0: sendSensor (A0); break;  // send A0 value
  82.      case CMD_READ_A1: sendSensor (A1); break;  // send A0 value
  83.      case CMD_READ_A2: sendSensor (A2); break;  // send A0 value
  84.      case CMD_READ_A3: sendSensor (A3); break;  // send A0 value
  85.                
  86.      case CMD_READ_D2: Wire.write (digitalRead (2)); break;   // send D2 value
  87.      case CMD_READ_D3: Wire.write (digitalRead (3)); break;   // send D3 value
  88.      case CMD_READ_D4: Wire.write (digitalRead (4)); break;   // send D4 value
  89.      case CMD_READ_D5: Wire.write (digitalRead (5)); break;   // send D5 value
  90.    
  91.      }  // end of switch
  92.  
  93.   }  // end of requestEvent
RAW Paste Data
Top