Skip to content

Instantly share code, notes, and snippets.

@WestleyR
Created February 19, 2020 00:37
Show Gist options
  • Save WestleyR/8f6ec6da40a560f4a82a5c5b07c2e910 to your computer and use it in GitHub Desktop.
Save WestleyR/8f6ec6da40a560f4a82a5c5b07c2e910 to your computer and use it in GitHub Desktop.

Master code:

#include <Wire.h>

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

void led(int value) {
  Wire.beginTransmission(4);
  Wire.write(1);
  Wire.write(value);
  Wire.endTransmission();
}


void loop() {
  led(1);
  digitalWrite(LED_BUILTIN, HIGH);
  delay(500);
  digitalWrite(LED_BUILTIN, LOW);
  led(0);
  delay(500);
}

Slave code:

// Actions:
// 1 = Set ODO led on/off
// 2 = Set Mil led on/off
// 3 = Set Err led on/off
// 4 = Set C led on/off
// 5 = Set the 6X 7 segment display to a value

#include <Wire.h>

void setup() {
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
}

void led(int value) {
  digitalWrite(LED_BUILTIN, value);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int inBytes) {
  while (Wire.available() > 0) {
    int a = Wire.read();
    if (a == 1) {
      led(Wire.read());
      return;
    } else if (a == 2) {
      // action 2
      led(0);
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment