Browse Source

Minor updates to examples

main
PaulStoffregen 7 years ago
parent
commit
88ec5e7b57
6 changed files with 31 additions and 23 deletions
  1. +3
    -4
      examples/SFRRanger_reader/SFRRanger_reader.ino
  2. +3
    -0
      examples/Scanner/Scanner.ino
  3. +9
    -7
      examples/master_reader/master_reader.ino
  4. +3
    -3
      examples/master_writer/master_writer.ino
  5. +6
    -7
      examples/slave_receiver/slave_receiver.ino
  6. +7
    -2
      examples/slave_sender/slave_sender.ino

+ 3
- 4
examples/SFRRanger_reader/SFRRanger_reader.ino View File

@@ -44,11 +44,10 @@ void loop()
Wire.requestFrom(112, 2); // request 2 bytes from slave device #112

// step 5: receive reading from sensor
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.read(); // receive high byte (overwrites previous reading)
if (Wire.available() >= 2) { // if two bytes were received
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
Serial.println(reading); // print the reading
}


+ 3
- 0
examples/Scanner/Scanner.ino View File

@@ -32,6 +32,9 @@


void setup() {
// uncomment these to use alternate pins
//Wire.setSCL(16);
//Wire.setSDA(17);
Wire.begin();
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor

+ 9
- 7
examples/master_reader/master_reader.ino View File

@@ -14,19 +14,21 @@

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}

void loop()
{
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
Serial.print("read: ");

while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8

while(Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}

Serial.println();
delay(500);
}

+ 3
- 3
examples/master_writer/master_writer.ino View File

@@ -21,10 +21,10 @@ byte x = 0;

void loop()
{
Wire.beginTransmission(8); // transmit to device #8
Wire.beginTransmission(9); // transmit to device #9
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting

x++;
delay(500);

+ 6
- 7
examples/slave_receiver/slave_receiver.ino View File

@@ -14,7 +14,7 @@

void setup()
{
Wire.begin(8); // join i2c bus with address #8
Wire.begin(9); // join i2c bus with address #9
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
@@ -28,11 +28,10 @@ void loop()
// 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
while(Wire.available() > 1) { // 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(x); // print the integer
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}

+ 7
- 2
examples/slave_sender/slave_sender.ino View File

@@ -12,8 +12,11 @@

#include <Wire.h>

int led = BUILTIN_LED;

void setup()
{
pinMode(led, OUTPUT);
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
@@ -27,6 +30,8 @@ void loop()
// this function is registered as an event, see setup()
void requestEvent()
{
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
digitalWrite(led, HIGH); // briefly flash the LED
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
digitalWrite(led, LOW);
}

Loading…
Cancel
Save