Need help with a code (MasterWriter)

So basically i’m doing a project in school and i’m kinda stuck. Basically we got 2 arduinos, one which consists with 2 Led-lights, and the other one consisting 2 buttons which I wanna control the lights with. I’m trying to somehow use the MasterWriter code with one slave but I really don’t know how to write the code for it, is there anyone who has some knwledge about this who can help me? :slight_smile: Atm i’m stuck with this:

// Wire Master Writer
// by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the “Wire Slave Receiver” example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

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

byte x = 0;

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

x++;
delay(500);
}

// Wire Slave Receiver
// by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the “Wire Master Writer” example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop()
{
delay(100);
}

// function that executes whenever data is received from master
// 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
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}

Are you stuck with using I2C?

I would program the controller with the leds to have 2 pins work as INPUT_PULLUP and assign 1 led to each. The code would just read pin1 and set led1 opposite and the same for pin2/led2.

Then wire both Arduinos ground to ground.

Then have the button Arduino set 2 pins according to the 2 buttons and wires from those pins to the matching pins on the leds Arduino either grounds the wires or not.

That way you don't have to analyze text at all.

And no, I won't give you the code or a page of details because this is a SCHOOL ASSIGNMENT.
You can ask about what I have posted and I'll give you more depth if there is any but you have to make it work or you will just screw yourself later by not learning yourself ready for the next step.

It's December. Didn't they teach you some code already? I am thinking that your school could maybe use more Arduinos if you can't get access except by short turns. Do you have a computer at home or are you posting from somewhere else, school or library? Do you have an Arduino of your own? If you have then I can recommend you go through some examples from in your IDE as a way to catch up or even get ahead.

while(1 < Wire.available()) // loop through all but the last

This assumes, incorrectly, that all the bytes arrive at once.

The function is called with the number of bytes to expect. Use a for loop to read all but one byte, not a while loop based on a function return value.

The code you posted does something. You didn’t explain what.

That appears to differ from what you want. You didn’t explain how.

You've written the function "void receiveEvent(int howMany)" but you never call it, except in setup().

Henry_Best:
You've written the function "void receiveEvent(int howMany)" but you never call it, except in setup().

Because it's probably supposed to be an IRQ?

That's some example code that is starting to be modified and got lost in terra incognito.

I’m trying to somehow use the MasterWriter code with one slave but I really don’t know how to write the code for it, is there anyone who has some knwledge about this who can help me? :slight_smile: Atm i’m stuck with this:

Just skip the masterwriter stuff and connect the two arduinos together using the arduino serial ports.

Why bother with serial? This is to turn 2 leds on and off.