Browse Source

All master/slave read & write examples Blink LED

main
PaulStoffregen 7 years ago
parent
commit
4e3317945a
4 changed files with 16 additions and 1 deletions
  1. +5
    -0
      examples/master_reader/master_reader.ino
  2. +5
    -0
      examples/master_writer/master_writer.ino
  3. +5
    -0
      examples/slave_receiver/slave_receiver.ino
  4. +1
    -1
      examples/slave_sender/slave_sender.ino

+ 5
- 0
examples/master_reader/master_reader.ino View File



#include <Wire.h> #include <Wire.h>


int led = LED_BUILTIN;

void setup() void setup()
{ {
pinMode(led, OUTPUT);
Wire.begin(); // join i2c bus (address optional for master) Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output Serial.begin(9600); // start serial for output
} }
{ {
Serial.print("read: "); Serial.print("read: ");


digitalWrite(led, HIGH); // briefly flash the LED
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8 Wire.requestFrom(8, 6); // request 6 bytes from slave device #8


while(Wire.available()) { // slave may send less than requested while(Wire.available()) { // slave may send less than requested
} }


Serial.println(); Serial.println();
digitalWrite(led, LOW);
delay(500); delay(500);
} }

+ 5
- 0
examples/master_writer/master_writer.ino View File



#include <Wire.h> #include <Wire.h>


int led = LED_BUILTIN;

void setup() void setup()
{ {
pinMode(led, OUTPUT);
Wire.begin(); // join i2c bus (address optional for master) Wire.begin(); // join i2c bus (address optional for master)
} }




void loop() void loop()
{ {
digitalWrite(led, HIGH); // briefly flash the LED
Wire.beginTransmission(9); // transmit to device #9 Wire.beginTransmission(9); // transmit to device #9
Wire.write("x is "); // sends five bytes Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting Wire.endTransmission(); // stop transmitting
digitalWrite(led, LOW);


x++; x++;
delay(500); delay(500);

+ 5
- 0
examples/slave_receiver/slave_receiver.ino View File



#include <Wire.h> #include <Wire.h>


int led = LED_BUILTIN;

void setup() void setup()
{ {
pinMode(led, OUTPUT);
Wire.begin(9); // join i2c bus with address #9 Wire.begin(9); // join i2c bus with address #9
Wire.onReceive(receiveEvent); // register event Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output Serial.begin(9600); // start serial for output
// this function is registered as an event, see setup() // this function is registered as an event, see setup()
void receiveEvent(int howMany) void receiveEvent(int howMany)
{ {
digitalWrite(led, HIGH); // briefly flash the LED
while(Wire.available() > 1) { // loop through all but the last while(Wire.available() > 1) { // loop through all but the last
char c = Wire.read(); // receive byte as a character char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character Serial.print(c); // print the character
} }
int x = Wire.read(); // receive byte as an integer int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer Serial.println(x); // print the integer
digitalWrite(led, LOW);
} }

+ 1
- 1
examples/slave_sender/slave_sender.ino View File



#include <Wire.h> #include <Wire.h>


int led = BUILTIN_LED;
int led = LED_BUILTIN;


void setup() void setup()
{ {

Loading…
Cancel
Save