EDIT
Since discovering that I2C was an easier and more appropriate method I have altered the title to reflect the new direction
End of edit
Hi all new here and relatively new to Arduino stuff. I have been using Arduino Nanos to help me with my model railroad to change track switches, semaphores and stop lights for a while. Some are simple so say a toggle switch will set LED's in a certain way, or move a servo or stepper motor to move a track switch to a certain position then read microswitches to give visual feedback. Other more complicated sketches do a combination. However none of the sketches I have produced are particularly complicated compared to the stuff I see on the internet as I am really not that proficient. I'm retired, learning is trickier now
What I have managed to do works fine, but the downside is all the control switches and feedback LED's need wires from the control position to the bit of the layout where they perform their function. As my layout has grown, so have the number of wires!
Reading online I have seen that RS485 can allow me to use four wires connected from a master to slaves, so if I understand correctly I can connect the control switches and feedback LED's to a master, and then have the slaves connect to the servos, steppers and semaphores and stop lights. The master can then read feedback from the slave to light the feedback LED's.
Firstly am I right in that this is possible?
If it is, then I'm hoping that you can help me learn how to implement it in my simple sketches
I have read the Arduino tutorials and have become more confused rather than less. Unfortunately that is a function of my age and the way my brain processes things. However one way that I have been able to learn previously is to see sketches doing something simple, then seeing another sketch with the same function but with extra functions.
In that way I have been able to figure out how to put stuff like switch commands, stepper control into my sketches.
What I don't want is for someone to tell me how to do everything, as I will never learn anything that way, so what I want to do is to get a simple sketch I made, and then see how that sketch would look separated into a master and slave sketch respectively
This sketch is just a test to use as my example.
It uses an ON-ON SPDT switch as the control switch, and for these purposes would light LED's 1 and 2 dependent on the switch position. A microswitch is connected so that a third LED will be lit if it is closed to provide feedback
bool buttonState1, buttonState2, buttonState3;
const int buttonPin1 = 8; // the number of the control switch position A pin
const int buttonPin2 = 9; // the number of the control switch position B pin
const int buttonPin3 = 10; // the number of the feedback microswitch pin
const int ledPin1 = A0; // the number of the LED pin
const int ledPin2 = A1; // the number of the LED pin
const int ledPin3 = A2; // the number of the feedback LED pin
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState2 = digitalRead(buttonPin3);
// check if control switch is in Position A. If it is, the buttonState is HIGH:
if (buttonState1 == LOW) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}
// check if control switch is in Position B. If it is, the buttonState is HIGH:
if (buttonState2 == LOW) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
// check if feedback microswitch is pressed. If it is, the buttonState is HIGH:
if (buttonState3 == LOW) {
// turn LED on:
digitalWrite(ledPin3, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin3, LOW);
}
}
The master control switch and feedback LED would be connected to the master, while LED's 1 and 2 plus the mircoswitch would be connected to the slave number 1.
I want to see how the sketch above would be implemented in a simple RS485 setup, and see what the Master and Slave sketches look like so that I can and try to figure out how to convert my other sketches. If someone is able to help me with those examples I would be grateful
Shaun
