Robin2:
I have attempted to rework your code into a series of short functions. It compiles, but I haven't tested it. Have a look and see if you feel it has any merit as a way to organize the code. Be sure to read my comments.
I don't know how you plan to connect the coloured LEDs for each signal so I haven't been able to deal with that.
Connect them as in how? the positive leg on the led goes to the mega and the negative goes to ground.
As far as all the pin assignments I have them all written down.
I don't know the relationship between routes and signal/switch settings so I haven't dealt with that. It may make sense to define these at the top of the program.
My objective has been to use meaningful names so that the code can be read in the way you read a story. There is no value in using abbreviated names.
Have fun ...
// http://forum.arduino.cc/index.php?topic=237805.30
// attempt 04 Jun 14 by Robin2 to reorganize code
//====global variables
const byte numSignals = 8; //Total number of signals? Instead of an array?
const byte numTrackSwitches = 4;
const byte numCodeButtons = 6;
const byte numBlocks = 6;
const byte numSections = 3;
const byte turnoutNormal = 0; // these are just wild guesses Turnout in normal position?
const byte turnoutReversed = 1;
const byte redred = 0;
const byte grnred = 1;
// all my pin numbers are guesses - replace with correct values
byte signal[numSignals];
byte signalPin[numSignals] = {40,41,42,43,44,45,46,47}; //all the pin numbers that the signals use?
byte trackSwitch[numTrackSwitches]; // the word switch has a special meaning
byte trackSwitchPin[numTrackSwitches] = {48,49,50,51};
byte block[numBlocks]; // guessing bk = block //total amount of blocks?
byte codeButtonPin[numCodeButtons] = {22, 23, 24, 25, 26, 27};
byte sectionPin[numSections] = {30, 31, 32};
byte route = 0;
byte newRoute = 0;
byte digitNum = 1; // to deal with multi-digit values
byte maxDigits = 2;
boolean cancelButton = true; // will make everything go red at the start ?
byte cancelButtonPin = A0; // an analog pin will work as a digital pin
//==================
void setup() {
Serial.begin(9600);
Serial.println("Starting R2SigTest.ino"); // just so we know it is alive
for(byte n = 2; n<54; n++){ // start with x = 2 to avoid the Tx and Rx pins
pinMode(n, OUTPUT);
}
for (byte n = 0; n < numCodeButtons; n++) {
pinMode(codeButtonPin[n], INPUT_PULLUP);
}
for (byte n = 0; n < numSections; n++) {
pinMode(sectionPin[n], INPUT_PULLUP);
}
pinMode(cancelButtonPin, INPUT_PULLUP);
cancelButton = true;
respondToCancel(); // initializes settings
}
//==================
void loop() {
readButtons();
processRoute();
setSignals();
setTrackSwitches();
}
//==================
void readButtons(){
cancelButton = digitalRead(cancelButtonPin);
updateRouteNumber(); // slightly complex so give it a separate function
}
//======================
void respondToCancel() {
if (cancelButton) {
for (byte n = 0; n < numSignals; n++) {
signal[n] = redred;
}
for (byte n = 0; n < numTrackSwitches; n++) {
trackSwitch[n] = turnoutNormal;
}
return;
}
}
//======================
void updateRouteNumber() {
// gather the digits for route
if (digitNum == 1) {
newRoute = 0;
}
for (byte n = 0; n < numCodeButtons; n ++) {
if (digitalRead(codeButtonPin[n]) == LOW) {
newRoute = newRoute * 10 + n;
}
}
digitNum ++;
if (digitNum > maxDigits) {
digitNum = 1;
route = newRoute;
}
}
//================
void processRoute() {
// get things ready for setSignals() etc
switch (route) {
case 14:
trackSwitch[1] = turnoutNormal;
break;
case 15:
trackSwitch[1] = turnoutNormal; //set switch 1 to normal
// trackSwitch[1] = grnred; // shouldn't this be a signal //yes grnred and redred apply to signals only
break;
case 36:
// ????
break;
}
}
//================
void setSignals() {
// this is wrong because I don't know how each signal is wired to give different colours
for (byte n = 0; n < numSignals; n ++) {
digitalWrite(signalPin[n], signal[n]);
} //All of the signals leds use 1 pin per color. // I will send you the pin layout so itll make thins easier.
}
//================
void setTrackSwitches() {
for (byte n = 0; n < numTrackSwitches; n ++) {
digitalWrite(trackSwitchPin[n], trackSwitch[n]);
} //Ive been seing alot of this code. Is this just like a one code fits all?
writing the pin the value? So if sw1 == tun then this code?
}
...R