I'm confused using i2c for a model railway turntable project

Hi all

I have setup the master slave on 2 uno's and this works fine using the example code on here

I have setup a single uno to run a stepper to turn the turntable and another to read a 4x4 keypad

all these work as expected - based on modified code examples on here

The bit I am struggling with is which chip sends or receives ie which is master and which is slave

Obviously the turntable doesn't move until I enter a number(code) on the keypad, except for the original setup to find a starting position, which runs on first power up of the chips, this is setup and working on the stepper uno.

below is what I envisage as to how it works

on power up startup routine runs to reset turntable to a predefined starting position then power down the stepper (set all outputs low)

then

1, wait until a code is entered into the keypad uno

2, if code number is valid send to stepper uno, else goto 1

3, stepper uno receives code and moves turntable to desired position

4, wait x time for turntable to be emptied then power down the stepper (set all outputs low)

4, clear all entries and goto 1

so until a valid code is input nothing happens, so how do I notify either uno that a code is due to be sent

does the master (stepper uno) wait for the slave (keypad uno) to send it the code or does the master run the keypad and send the keypad number to the stepper. Examples on here all have the master requesting a code from the slave, which doesn't fit with what I am doing, or am I miss-reading the way i2c works?

any help appreciated as I'd like to use the keypad uno to send codes to other arduino boards to control more accessories e.g. to point and signal settings

First of all, this could be accomplished with a single Arduino, but if you want to use two, fine.

Using I2C, only the master gets to choose when to send. The slave only gets to send whenever and whatever the master requests. Presumably, in your situation, the Arduino with the keypad is the master and the Arduino with the stepper motor and the turntable is the slave.

:slight_smile: Good news: Even if you want to swap roles, it is easy to do.

@vaj4088

thanks for the reply - sort of helps

you say i can do this on 1 uno chip - please let me know how as I need 8 pins for the keypad input 4 for the stepper output and 1 input for the start position sensor, which when i looked at the circuit diags and coding examples couldn't see how it worked on 1 uno

BB AKA David

A0 to A3 can be used for digital I/O too.

TheMemberFormerlyKnownAsAWOL:
A0 to A3 can be used for digital I/O too.

need to review pin connections and see where I am going wrong -- but its now beer time here

..and if you don't now need I2C, A4 and A5 can be digital pins too.

You need 13 digital pins. I am most familiar with the Uno, which has 14 digital pins. Some Arduinos have more than this. However, it sounds as if you want to have a single shared keypad and several accessories (such as a turntable). If this is the case, then multiple Arduinos may make sense.

vaj4088:
You need 13 digital pins.

So, pins 2 to 13, and A0 to A5, leaving pins 0 and 1 to talk to serial to debug.

Sounds plenty to me.