Hello, I'm new here. I'm learning Arduino recently, and I'm still a beginner. I need it for a project I'm designing as an architecture engineer. I've designed the system, but I'm not sure how to make the code work.
I'm sure the code is doable, and I've done my research, but I'm still learning, and it will take time to find the solution. I have the concept ready for the program, but I want to translate it into code.
First, I'm using the I2C protocol with a master device (Arduino Uno) and many slaves (Arduino Nano). Each slave has a unique address from 1-127. All the slaves are connected to the master with 5V, GND, SDA, and SCL wires. They are also connected to each other with at least one location pin (2, 3, 4, 5) like a grid system. The master has only one location pin (pin 2 = HIGH) connected to one of the slaves.
Scanning for Devices:
I want the master to first scan the devices and assign all connected devices to the ConnectedDevices[] array so I can count, display, detect changes in the system, and test locations later.
Finding the First Slave:
After scanning, I ask the master to go through the ConnectedDevices[] and ask them one by one if one of the 4 location pins reads (HIGH), until it finds the one connected directly to the master. This will be the first slave, and I will assign it to RegisteredDevice[0], marking its location on the grid as row[0]=0 and column[0]=0. Then I turn pin (2) to LOW.
Finding Other Slaves’ Locations:
After that, the code will go through RegisteredDevice[0] and ask it to make pin (2) an output (HIGH). Then the master will go through all ConnectedDevices[] that are not registered and see if any of their pins are connected to that pin. If none are connected, the slave will return 0, and the master will test the next pin (3) for all devices again, then pin (4) and pin (5) . If none read a value, the loop will go to the next ConnectedDevices[] and repeat the tests for the 4 pins in the RegisteredDevice[]. If any of the pins read a value, the device is connected, and the slave will return the pin number.
For example, if slave 0x3 gets a value from Pin 4, the master will know this slave is connected to the first address with pin 4, so the new location will be Row[i+1]=row[i] (no change in row), Column[i+1]=Column[i]+1 (the device is on top of the reference slave), and assign its address to RegisteredDevice[i+1].
Note: There may be none or more than one connected device (maximum 3), so it will register them one after another.
After that, it will go to the next RegisteredDevice[] and set one of their pins to HIGH at a time, testing all the connected devices again until finding all the slave locations.
Detection mode
Once all locations are found, the program will return to detection mode for any changes in the system (if any device is added or removed). The system will reset and find all locations again.
Here is the digram for the connected devices ( the slaves can be arranged in any different positions)
If anyone could guide me on how to implement this in code, I would greatly appreciate it! Please let me know if you need any other information. Thank you!
