ES ordening
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
#include <Wire.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
uint8_t count = 0;
|
||||
|
||||
void setup() {
|
||||
Wire.begin(); // Master
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// Send count to slave
|
||||
Wire.beginTransmission(0x42);
|
||||
Wire.write(count);
|
||||
Wire.endTransmission();
|
||||
|
||||
Serial.print("Sent: ");
|
||||
Serial.println(count);
|
||||
|
||||
// Request response from slave
|
||||
Wire.requestFrom(0x42, 1);
|
||||
if (Wire.available()) {
|
||||
uint8_t response = Wire.read();
|
||||
Serial.print("Received: ");
|
||||
Serial.println(response);
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
volatile uint8_t receivedValue = 0;
|
||||
|
||||
void setup() {
|
||||
Wire.begin(0x42);
|
||||
Wire.onReceive(receiveEvent);
|
||||
Wire.onRequest(requestEvent);
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
|
||||
void receiveEvent(int howMany) {
|
||||
if (howMany > 0) {
|
||||
receivedValue = Wire.read();
|
||||
Serial.print("Received from master: ");
|
||||
Serial.println(receivedValue);
|
||||
}
|
||||
}
|
||||
|
||||
void requestEvent() {
|
||||
uint8_t reply = (receivedValue > 100) ? 2 : 4;
|
||||
Wire.write(reply);
|
||||
Serial.print("Sent to master: ");
|
||||
Serial.println(reply);
|
||||
}
|
||||
Reference in New Issue
Block a user