// 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" ekample for use with this // Created 29 March 2006 // This ekample code is in the public domain. #include int k; int con; int onoff; void setup() { Wire.begin(8); // join i2c bus with address #8 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); pinMode(14, OUTPUT); pinMode(15, OUTPUT); pinMode(16, OUTPUT); } void loop() { delay(100); } // function that ekecutes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while (1 < Wire.available()) { // loop through all but the last char c = Wire.read(); // receive byte as a character // Serial.print(c); // print the character } int x = Wire.read(); // receive byte as an integer //Serial.println(k); // print the integer k=x; test1(); } void test1() { if ((k >=65 && k<=77 )|| k==79 || k == 80) { con = k; } else if (k ==89 || k == 78) { onoff = k; } else { } if (con == 65 && onoff == 89) { digitalWrite(16, HIGH); } if (con == 65 && onoff == 78) { digitalWrite(16, LOW); } if (con == 66 && onoff == 89) { digitalWrite(15, HIGH); } if (con == 66 && onoff == 78) { digitalWrite(15, LOW); } if (con == 67 && onoff == 89) { digitalWrite(14, HIGH); } if (con == 67 && onoff == 78) { digitalWrite(14, LOW); } if (con == 68 && onoff == 89) { digitalWrite(13, HIGH); } if (con == 68 && onoff == 78) { digitalWrite(13, LOW); } if (con == 69 && onoff == 89) { digitalWrite(12, HIGH); } if (con == 69 && onoff == 78) { digitalWrite(12, LOW); } if (con == 70 && onoff == 89) { digitalWrite(11, HIGH); } if (con == 70 && onoff == 78) { digitalWrite(11, LOW); } if (con == 71 && onoff == 89) { digitalWrite(10, HIGH); } if (con == 71 && onoff == 78) { digitalWrite(10, LOW); } if (con == 72 && onoff == 89) { digitalWrite(9, HIGH); } if (con == 72 && onoff == 78) { digitalWrite(9, LOW); } if (con == 73 && onoff == 89) { digitalWrite(8, HIGH); } if (con == 73 && onoff == 78) { digitalWrite(8, LOW); } if (con == 74 && onoff == 89) { digitalWrite(7, HIGH); } if (con == 74 && onoff == 78) { digitalWrite(7, LOW); } if (con == 75 && onoff == 89) { digitalWrite(6, HIGH); } if (con == 75 && onoff == 78) { digitalWrite(6, LOW); } if (con == 76 && onoff == 89) { digitalWrite(5, HIGH); } if (con == 76 && onoff == 78) { digitalWrite(5, LOW); } if (con == 77 && onoff == 89) { digitalWrite(4, HIGH); } if (con == 77 && onoff == 78) { digitalWrite(4, LOW); } if (con == 79 && onoff == 89) { digitalWrite(3, HIGH); } if (con == 79 && onoff == 78) { digitalWrite(3, LOW); } if (con == 80 && onoff == 89) { digitalWrite(2, HIGH); } if (con == 80 && onoff == 78) { digitalWrite(2, LOW); } Serial.print(con); Serial.print(" "); Serial.println(onoff); int con=0; int onoff=0; k=0; }