Thank you for your response. I uploaded the code and then the LED blinks but it is not controlled by the button in this way. The blink example is not using I2C right? I think something is going wrong with the communication.
That helped a bit, it does turn on and off now. But after pressing the button for a couple of times (around 10 times) the LED will not turn on anymore. I have to reupload the code in order for the LED to react to the button. Does someone know why this is the case?
No, using the blink sketch was only to test that there is no problem with your led or the Arduino pin or your wiring. You might say "of course I tested those things, in other ways" but on this forum we get questions from posters with a huge range of experience levels and often they have not thought to test the most basic components and aspects of their circuits.
Here is a more important question than any we have asked you so far: why are you doing this? Why are you using 2 Arduino and communicating between them using i²c? If the reason is to increase the number of input/output pins, analog pins, PWM pins etc above what a single Arduino can provide, then we can suggest better solutions than using multiple Arduinos.
One thing I found out was that your switch needs a pull up resistor. It may seem like it is a 1, but is is bouncing all over the place between a 1 and 0. Fortunately the Uno has built in pull up resistors. Try adding this pinMode( buttonPin, INPUT_PULLUP);
It worked for me
Eventually I want to have a 3 by 3 square matrix form connected graph and each node represents an Arduino. All of the Arduino's have to communicate with each other, so I want to use multimaster (which I read that it is quite hard to do with Arduino's). But before I am going to test the multimaster I wanted to test the communication between a slave and a master with a button and a LED. So it is more for test purposes.
Hi, @thomnik
Welcome to the forum.
This where quite a few posts would have been saved if you had posted a circuit diagram.
Can you please post one?
A hand drawn schematic would be fine.
How far apart will each node be?
If it is going to be a 3 x 3 matrix, there are easier and less hardware intense solutions.
Can you tell us your application/project?
Hi Tom, what I want to achieve is a lot different then the topic in this section, is that a problem to post here or should I make a new topic or send you a message?
Sure, I want to build a system connected in a 3 by 3 matrix form (later expand this to 5 by 5 or even bigger), each node is then an Arduino and every Arduino has to send and receive data. So for example I want to send data from Arduino 1 to the last one in the matrix, Arduino 9. It has to do this in the shortest path available and if a connection between Arduino's is not available the system has to rearrange and it has to take another route to Arduino 9. I already made a simulation in Python and wanted to test my findings with Arduino's, because they are versatile and quite "easy" to program. I thought that I2C was a good option, because I think that every node has to send and receive data. The picture maybe gives a better idea of what I am talking about.
Ok, bit that's still a solution to a problem, it's not the problem itself. Why are you wanting this matrix of Arduinos, what real-world problem is that a potential solution to? Perhaps it's just a hypothetical problem for teaching purposes.
I would suggest using UART (=serial) rather than i²c. Your image shows Arduino Megas, and they have 3 or 4 hardware UART interfaces each.
As they are all on the same bus, you don't have to send a signal to 1 then 2 then 3 to get to 9.
You can send it directly as all our UNOs will be on the same bus.
UNLESS
As your diagram "infers", you have dedicated I2C buses between adjacent UNOs.
If so you had better also see if the UNO can support four independent I2C comm ports.
If this whole project is just an experiment to see if you can do it, fair enough.
But putting all the UNOs on the single I2C bus would be simpler, or a single CanBus.
The CanBus needing some extra hardware.
It is indeed for teaching purposes, I want to see if it is possible to do and how it can be done.
There are software I2C libraries, so in theory the Arduinos can support multiple buses right? I don't know how it will work in practice, but we'll see.