// Wire Slave Receiver // by Nicholas Zambetti // Demonstrates use of the Wire library // Receives data as an I2C/TWI slave device // Refer to the "Wire Master Writer" example for use with this // Created 29 March 2006 // This example code is in the public domain. #include byte ledPin = 13; boolean ledState = 0; byte sensorValue = 0; byte thresholdInc = 15; byte thresholdBase = 124; byte thresholds[] = { thresholdBase+0*thresholdInc, thresholdBase+1*thresholdInc, thresholdBase+2*thresholdInc, thresholdBase+3*thresholdInc, thresholdBase+4*thresholdInc, thresholdBase+5*thresholdInc, thresholdBase+6*thresholdInc}; void setup() { Wire.begin(10); // join i2c bus with address #8 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output pinMode(ledPin, OUTPUT); digitalWrite(ledPin, ledState); pinMode (2, OUTPUT); pinMode (3, OUTPUT); pinMode (4, OUTPUT); pinMode (5, OUTPUT); pinMode (6, OUTPUT); pinMode (7, OUTPUT); } void loop() { delay(100); } // function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while(Wire.available()){ sensorValue = Wire.read(); } if (sensorValue > thresholds[0] && sensorValue <= thresholds[1]){ digitalWrite (2, HIGH); digitalWrite (3, LOW); digitalWrite (4, LOW); digitalWrite (5, LOW); digitalWrite (6, LOW); digitalWrite (7, LOW); } if (sensorValue > thresholds[1] && sensorValue <= thresholds[2]){ digitalWrite (2, HIGH); digitalWrite (3, HIGH); digitalWrite (4, LOW); digitalWrite (5, LOW); digitalWrite (6, LOW); digitalWrite (7, LOW); } if (sensorValue > thresholds[2] && sensorValue <= thresholds[3]){ digitalWrite (2, HIGH); digitalWrite (3, HIGH); digitalWrite (4, HIGH); digitalWrite (5, LOW); digitalWrite (6, LOW); digitalWrite (7, LOW); } if (sensorValue > thresholds[3] && sensorValue <= thresholds[4]){ digitalWrite (2, HIGH); digitalWrite (3, HIGH); digitalWrite (4, HIGH); digitalWrite (5, HIGH); digitalWrite (6, LOW); digitalWrite (7, LOW); } if (sensorValue > thresholds[4] && sensorValue <= thresholds[5]){ digitalWrite (2, HIGH); digitalWrite (3, HIGH); digitalWrite (4, HIGH); digitalWrite (5, HIGH); digitalWrite (6, HIGH); digitalWrite (7, LOW); } if (sensorValue > thresholds[5] && sensorValue <= thresholds[6]){ digitalWrite (2, HIGH); digitalWrite (3, HIGH); digitalWrite (4, HIGH); digitalWrite (5, HIGH); digitalWrite (6, HIGH); digitalWrite (7, HIGH); } }