Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Written by Nick Gammon
- // Date: 18th February 2011
- // Modified by Synthetic Physical Computing
- // Date: 1st Febuary 2014
- #include <Wire.h>
- const int SLAVE_ADDRESS = 0x42;
- // various commands we might send
- enum {
- CMD_ID = 1,
- CMD_READ_A0 = 2,
- CMD_READ_A1 = 3,
- CMD_READ_A2 = 4,
- CMD_READ_A3 = 5,
- CMD_READ_D2 = 6,
- CMD_READ_D3 = 7,
- CMD_READ_D4 = 8,
- CMD_READ_D5 = 9,
- };
- void sendCommand (const byte cmd, const int responseSize)
- {
- Wire.beginTransmission (SLAVE_ADDRESS);
- Wire.write (cmd);
- Wire.endTransmission ();
- Wire.requestFrom (SLAVE_ADDRESS, responseSize);
- } // end of sendCommand
- void setup ()
- {
- Wire.begin ();
- //TWBR = 12;
- Serial.begin (9600); // start serial for output
- sendCommand (CMD_ID, 1);
- if (Wire.available ())
- {
- Serial.print ("Slave is ID: ");
- Serial.println (Wire.read (), HEX);
- }
- else
- Serial.println ("No response to ID request");
- } // end of setup
- void loop()
- {
- int val;
- sendCommand (CMD_READ_A0, 2);
- val = Wire.read ();
- val <<= 8;
- val |= Wire.read ();
- Serial.print ("Value of A0: ");
- Serial.println (val, DEC);
- sendCommand (CMD_READ_A1, 2);
- val = Wire.read ();
- val <<= 8;
- val |= Wire.read ();
- Serial.print ("Value of A1: ");
- Serial.println (val, DEC);
- sendCommand (CMD_READ_A2, 2);
- val = Wire.read ();
- val <<= 8;
- val |= Wire.read ();
- Serial.print ("Value of A2: ");
- Serial.println (val, DEC);
- sendCommand (CMD_READ_A3, 2);
- val = Wire.read ();
- val <<= 8;
- val |= Wire.read ();
- Serial.print ("Value of A3: ");
- Serial.println (val, DEC);
- sendCommand (CMD_READ_D2, 1);
- val = Wire.read ();
- Serial.print ("Value of D2: ");
- Serial.println (val, DEC);
- sendCommand (CMD_READ_D3, 1);
- val = Wire.read ();
- Serial.print ("Value of D3: ");
- Serial.println (val, DEC);
- sendCommand (CMD_READ_D4, 1);
- val = Wire.read ();
- Serial.print ("Value of D4: ");
- Serial.println (val, DEC);
- sendCommand (CMD_READ_D5, 1);
- val = Wire.read ();
- Serial.print ("Value of D5: ");
- Serial.println (val, DEC);
- delay (500);
- } // end of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement