Hi Folks
1st post here so apologies if I've not picked the correct location or done something otherwise incorrect.
I'm looking for a little help / guidance on what I have done so far with my project and what to do next - I feel like the multiple sources of examples and learning materials have got me to the point I am at but my needs are getting more specific and I'm drifting away from the useful examples on here and elsewhere.
As you can guess from the title the ultimate aim is to control model railway semaphore signals in a realistic way, ideally with a range of potential movements chosen at random each time the operation of a signal is triggered. I am using an Elegoo Uno R3 and have chosen to use PCA9685 boards with the thought that the Uno can go in the control panel and I can daisy chain the PCA9685's round the layout as required. For the moment I am testing with 1 PCA9685 board. The eventual setup will have levers to control the operation which could be on/off, on/on or momentary but at the moment I am using push buttons on a breadboard to simulate this.
I've included a diagram of my setup (hopefully ):
I couldn't find a way to represent the PCA9685 but its 6 pins are represented on the RHS.
If that didn't work the setup is as follows:
2 servos connected to pins 0 & 1 of the PCA9685
PCA9685 to Uno connections: SCL->A5, SDA->A4
On the Uno Push Buttons connected to Digital Pins 3 & 4 and GND
The PCA9685 has a separate 5v power supply into the terminals and is connected to Uno power on the Pin connections.
I am trying to code in such a way that I can repeat the setup out over multiple PCA9685s and easily add more switches when required by just duplicating code. The eventual aim is to also get a more realistic look to the signal movement but that is a level beyond me at the min.
Ultimately in the Loop, currently, 'if' statements test the status of a switch then perform a pmw.set to move the servo arm. I have predefined pwm settings for each servo at the beginning of the program which I hope will allow me to fine tune positions easily.
So as it stands my setup allows me to power up the system and all of the signals will be set to Danger, when I press and hold a button they move to clear, release the button and back to danger. Holding the button down isn't an issue as I intend the button to be replaced with an on/off switch of some kind.
My queries are really this:
- Have I done anything stupid?
- Are there alternative ways to code this I've missed?
- Is the INPUT_PULLUP appropriate to minimise wiring to switches or does this compromise something else?
- Am I likely to be able to replace my pwm.Set lines with more complex moves?
- Am I likely to run into any issues as the system grows to include more servos?
- Anyone fancy giving me a steer in relation to the realistic movements??? [So far I have studied a couple of youtube videos by folks that have achieved this but I dont know my way round the code well enough to understand what is relevant to my setup]
Here is my code:
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver signalBoard01 = Adafruit_PWMServoDriver(0x40);
#define SERVOMIN 100 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 710 // this is the 'maximum' pulse length count (out of 4096)
int lever01Pin = 3;
int signal01Clear = 200;
int signal01Danger = 120;
int lever02Pin = 4;
int signal02Clear = 190;
int signal02Danger = 104;
void setup() {
Serial.begin(9600);
pinMode(lever01Pin, INPUT_PULLUP);
pinMode(lever02Pin, INPUT_PULLUP);
Serial.println("GingerAngles Signal Control!");
signalBoard01.begin();
signalBoard01.setPWMFreq(50); // Analog servos run at ~60 Hz updates
}
void loop()
{
int lever01State = digitalRead(lever01Pin);
if(lever01State == LOW)
{
signalBoard01.setPWM(0, 0, signal01Clear);
delay(10);
}
else
signalBoard01.setPWM(0, 0, signal01Danger);
delay(10);
int lever02State = digitalRead(lever02Pin);
if(lever02State == LOW)
{
signalBoard01.setPWM(1, 0, signal02Clear);
delay(10);
}
else
signalBoard01.setPWM(1, 0, signal02Danger);
delay(10);
}
Many TIA!