Need Help After Stroke - Code Modification

Hello,

I need some code changed. I would like to do it myself, but I had a stroke that has limited some of my logic abilities. So I need help.

Now I am basically limited to uploading code and, with the help of my wife, doing the necessary wiring.

I am using an arduino mega and pca9685.

So I ask if someone could do this for me.

I will pay if necessary.

There are to be 12 turnouts.

There will be 6 lights per turnout. 2 on each leg and 2 on the control panel.

Code is to be modified so that LEDS are shifted out to a shift register.

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

int buttonPin0 = 2;   // Can add as many buttons as needed
int ledPin0C = 3;     // C designates Closed position of turnout
int ledPin0T = 4;     // T designates Thrown position of turnout
int buttonState0 = 0;

int buttonPin1 = 5;
int ledPin1C = 6;
int ledPin1T = 7;
int buttonState1 = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("pca9685_TurnoutFinal!");
  pwm.begin();
  pwm.setPWMFreq(60);
  delay(30);

  pinMode(ledPin0C, OUTPUT);
  pinMode(ledPin0T, OUTPUT);
  pinMode(buttonPin0, INPUT);
  pinMode(ledPin1C, OUTPUT);
  pinMode(ledPin1T, OUTPUT);
  pinMode(buttonPin1, INPUT);
}

void loop() {
  ///////////
  buttonState0 = digitalRead(buttonPin0);
  if (buttonState0 == HIGH) {
    pwm.setPWM(0, 0, 370);
    digitalWrite(ledPin0T, LOW);
    digitalWrite(ledPin0C, HIGH);
  }
  else  {
    pwm.setPWM(0, 0, 285);
    digitalWrite(ledPin0C, LOW);
    digitalWrite(ledPin0T, HIGH);
  }
  //////////
  buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {
    pwm.setPWM(1, 0, 370);
    digitalWrite(ledPin1T, LOW);
    digitalWrite(ledPin1C, HIGH);
  }
  else  {
    pwm.setPWM(1, 0, 285);
    digitalWrite(ledPin1C, LOW);
    digitalWrite(ledPin1T, HIGH);
  }
  //////////
}

It looks like what you have does two turnouts which each have two LEDs. So the ask is to increase to twelve and more LEDs?

Do you have a particular shift register in mind?

Here’s a modified version that uses an array of structs. It does your existing two turnouts and adds a third as an illustration with the pin numbers following the existing pattern.

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>

Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

struct Turnout
{
byte buttonPin;
byte ClosedLedPin;
byte ThrownLedPin;
byte ServoNumber;
byte buttonState;
};

Turnout Turnouts[] = {{2,3,4,0},{5,6,7,1},{8,9,10,2}};
const byte NumTurnouts=sizeof Turnouts/ sizeof Turnouts[0];

void setup()
{
Serial.begin(9600);
Serial.println("pca9685_TurnoutFinal!");
pwm.begin();
pwm.setPWMFreq(60);
delay(30);
for(byte lp=0;lp<NumTurnouts;lp++)
  {
  pinMode(Turnouts[lp].ThrownLedPin, OUTPUT);  
  pinMode(Turnouts[lp].ClosedLedPin, OUTPUT);
  pinMode(Turnouts[lp].buttonPin, INPUT);
  }
}

void loop()
{
for(byte lp=0;lp<NumTurnouts;lp++)
  {
  Turnouts[lp].buttonState = digitalRead(Turnouts[lp].buttonPin);
  if (Turnouts[lp].buttonState == HIGH)
    {
    pwm.setPWM(Turnouts[lp].ServoNumber, 0, 370);
    digitalWrite(Turnouts[lp].ThrownLedPin, LOW);
    digitalWrite(Turnouts[lp].ClosedLedPin, HIGH);
    }
  else
    {
    pwm.setPWM(Turnouts[lp].ServoNumber, 0, 285);
    digitalWrite(Turnouts[lp].ClosedLedPin, LOW);
    digitalWrite(Turnouts[lp].ThrownLedPin, HIGH);
    }
  }
}

Compiled, not tested.

It does not address the additional LEDs or the shift register(s).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.