Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #define ADDR 0x22 // the double in the NXT (e.g. Arduino: 0x13, NXT: 0x26)
- byte SensorName[9] = "Arduino ";
- byte SensorVersion[9] = "v1.0 ";
- byte SensorType[9] = "amazin ";
- void requestEvent(void);
- void receiveEvent(int howMany)
- byte bufferReceived[16];
- byte bufferToSend[16];
- byte receivedCount = 0;
- void setup()
- {
- Serial.begin(115200);
- Wire.begin(ADDR);
- Wire.onRequest(requestEvent);
- Wire.onReceive(receiveEvent);
- Serial.println("NXTDuino Inited");
- }
- void loop()
- {
- delay(100);
- }
- void requestEvent(void)
- {
- Serial.println("Data request!\n");
- if(receivedCount == 0) //What does this guy want?
- {
- Wire.write(0x0);
- return;
- }
- //First byte (aka Register)
- switch(bufferReceived[0])
- {
- //"Sensor" stuff
- case 0x0: //Version
- Wire.write(SensorVersion, 8);
- break;
- case 0x08: //Name
- Wire.write(SensorName, 8);
- break;
- case 0x10: //Type
- Wire.write(SensorType, 8);
- break;
- //Commands, from 0x11 to 0xFF
- case 0x11:
- Wire.write(0x1);
- break;
- //What? Nothing!?
- default:
- Wire.write(0x0);
- break;
- }
- }
- void receiveEvent(int howMany)
- {
- Serial.print("-->Received: ");
- Serial.print(howMany, HEX);
- Serial.println(" byte(s)\n");
- for(receivedCount = 0; Wire.available() > 0; receivedCount++)
- {
- bufferReceived[receivedCount] = Wire.read();
- //Serial.println(bufferReceived[receivedCount], HEX);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement