// // Program name: i2Cslave04 // from http://www.instructables.com/id/I2C-between-Arduinos/ // By Cornel Amariei for Packt Publishing // // Purpose: // Program two Arduinos to communicate via i2C // this is slave node // // Edit History: // rev. 01: orig // rev. 02: added serial print to monitor status // rev. 03: '#' turns on LED, '*' turns it off // rev. 04: when requestFrom is rcvd from master, single char is sent back to master // // // Program operation/flow: // intended for receiving 3x4 membrane keypad keypresses // '#' turns on LED, '*' turns it off // when request for data rcvd from master, last valid numeric char sent from master is returned, // when chars are received from master, if valid numeric, z (char to be returned on request) is set // // // Hardware configuration: // 13 is LED on protoboard // A4 is SDA (data) // A5 is SCL (clock) // both i2C lines need a pullup, however the internal pullups in Unos are sufficient // // ------------------------------------------------------ // ************** INCLUDES ******************* #include // ---------- END includes ------------ // ************** GLOBALS ******************* int LEDonboard = 13; char x = 'x'; // dummy initial value for incoming char char z = 'z'; // dummy initial value for char to be sent back to master upon request // ---------------- END globals ----------------- // ************** SETUP ******************* void setup() { Serial.begin (9600); pinMode (LEDonboard, OUTPUT); Wire.begin(9); // Start the I2C Bus as Slave on address 9 Wire.onReceive(receiveEvent); // Attach a function to trigger when something is received. Wire.onRequest(answerRequest); // used in slave to specify function to run when request is received from master // at end of setup: blink LEDonboard // blinkOnboard (prePause, numBlinks, blinkOnTime, betweenBlinks, postPause); blinkOnboard (500, 1, 150, 160, 500); // some# slow blinkOnboard (500, 3, 90, 160, 500); // some# fast } // -------------- END setup -------------- // ************** LOOP ******************* void loop() { // this section toggles onboard LED with '#' and '*' if (x == '#') { //If value received is '#', digitalWrite(LEDonboard, HIGH); } // endif '#' if (x == '*') { //If value received is '#', digitalWrite(LEDonboard, LOW); } // endif '*' } // --------------- END loop --------------- // **************** FUNCTION DEFS ***************** void answerRequest() { // sends data back to master Wire.write(z); Serial.print("sending: "); Serial.println(z); Serial.println(" "); } // ----------------- END answerRequest ---------------------- void receiveEvent(int bytes) { x = Wire.read(); // read one character from the I2C // if it's valid numeric, set z = x if ( (x == '0') || (x == '1') || (x == '2') || (x == '3') || (x == '4') || (x == '5') || (x == '6') || (x == '7') || (x == '8') || (x == '9') ) { z = x; } Serial.print("char rcvd: "); Serial.println(x); Serial.println(" "); } // ----------------- END receiveEvent ---------------------- void blinkOnboard (int prePause, int numBlinks, int blinkOn, int betweenBlinks, int postPause) { // blinks onboard LED-13 // takes parameters: pre-pause; # blinks; blinkOn duration; betweenBlink pause duration; post-pause delay(prePause); for (int n = 0; n < numBlinks; n++) { digitalWrite(LEDonboard, HIGH); delay(blinkOn); digitalWrite(LEDonboard, LOW); delay(betweenBlinks); } // endfor delay(postPause); } // ----------------- END blinkOnboard ---------------------- // --------------- END function defs ------------ // --------------- END program ------------------