Created
May 15, 2017 12:11
-
-
Save trevery/5a44c6f51e189308db0a16fe98315ac5 to your computer and use it in GitHub Desktop.
arduino i2c slave_read and do something in loop()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Include the required Wire library for I2C<br>#include <Wire.h> | |
int LED = 13; | |
int x = 0; | |
void setup() { | |
// Define the LED pin as Output | |
pinMode (LED, OUTPUT); | |
// Start the I2C Bus as Slave on address 9 | |
Wire.begin(9); | |
// Attach a function to trigger when something is received. | |
Wire.onReceive(receiveEvent); | |
} | |
void receiveEvent(int bytes) { | |
x = Wire.read(); // read one character from the I2C | |
} | |
void loop() { | |
//If value received is 0 blink LED for 200 ms | |
if (x == '0') { | |
digitalWrite(LED, HIGH); | |
delay(200); | |
digitalWrite(LED, LOW); | |
delay(200); | |
} | |
//If value received is 3 blink LED for 400 ms | |
if (x == '3') { | |
digitalWrite(LED, HIGH); | |
delay(400); | |
digitalWrite(LED, LOW); | |
delay(400); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment