I know that you did not ask for this but I would suggest to make it more scalable. Every time that you start numbering variables you should think "arrays". Also, your turnouts have related information, the two pins and the old position ('oudeStand').
To start with the related information, you can consider to use a struct or a class; the below uses a struct to combine the related information in a new type. Both structs and classes are like an entry in a phonebook where a name is associated with a phone number, an address and possibly other information.
struct WISSEL
{
const uint8_t pinRecht;
const uint8_t pinKrom;
int oudeStand;
};
You can now create an array of WISSEL structs for your 4 turnouts.
WISSEL wissels[] = {
{2, 3, 1},
{4, 5, 1},
{6, 7, 1},
{8, 9, 1},
};
This is easily scalable; if you ever want to increase the number of turnouts you can simply add one or more lines instead of adding 3 variables for each turnout.
You can access the fields in the WISSEL struct using a dot as shown below for setup
// macro om het aantal elementen in enig type array te berekenen
#define NUMELEMENTS(x) (sizeof(x) / sizeof(x[0]))
// wissel pin is actief als het niveau laag is
#define ACTIVE LOW
//int wissel;
int wisselNummer;
int wisselStand; // 1 = recht, 0 = afbuigend
struct WISSEL
{
const uint8_t pinRecht;
const uint8_t pinKrom;
int oudeStand;
};
WISSEL wissels[] = {
{2, 3, 1},
{4, 5, 1},
{6, 7, 1},
{8, 9, 1},
};
int puls = 15;
void setup()
{
Serial.begin(115200); // aanpassen indien noodzakelijk
Serial.print(F("Er zijn "));
Serial.print(NUMELEMENTS(wissels));
Serial.println(F(" wissels"));
for (uint8_t teller = 0; teller < NUMELEMENTS(wissels); teller++)
{
Serial.print(F("Wissel "));
Serial.println(teller + 1);
Serial.print(F(" Pin krom = "));
Serial.println(wissels[teller].pinKrom);
Serial.print(F(" Pin recht = "));
Serial.println(wissels[teller].pinRecht);
Serial.print(F(" Wisselstand = "));
Serial.println(wissels[teller].oudeStand);
// zet wissel pinnen als uitgang
// we zetten eerst het niveau zodat recht en krom niet actief zijn
digitalWrite(wissels[teller].pinKrom, !ACTIVE);
pinMode(wissels[teller].pinKrom, OUTPUT);
digitalWrite(wissels[teller].pinRecht, !ACTIVE);
pinMode(wissels[teller].pinRecht, OUTPUT);
}
}
I've defined two macros at the top of the code.
You can see how you can iterate through the turnouts. Instead of if/else or switch/case, you can simply use an index (teller, later in the program the code uses wisselNummer).
There is a function (zetWissel) to set the position of a turnout.
void zetWissel()
{
if (wisselNummer < 1 || wisselNummer > NUMELEMENTS(wissels))
{
// niks te doen
return;
}
if (wisselStand != 0 && wisselStand != 1)
{
// incorrect, niks te doen
return;
}
// als de positie veranderd is
if (wisselStand != wissels[wisselNummer - 1].oudeStand)
{
if (wisselStand == 1)
{
digitalWrite(wissels[wisselNummer - 1].pinRecht, ACTIVE);
delay(puls);
digitalWrite(wissels[wisselNummer - 1].pinRecht, !ACTIVE);
Serial.println(F("recht"));
}
else
{
digitalWrite(wissels[wisselNummer - 1].pinKrom, ACTIVE);
delay(puls);
digitalWrite(wissels[wisselNummer - 1].pinKrom, !ACTIVE);
Serial.println(F("afbuigend"));
}
// onthoud laatste stand
wissels[wisselNummer - 1].oudeStand = wisselStand;
}
}
The function first checks if wisselNummer and wisselStand are valid; if yes, it checks if the position changed and sets the desired position. Note that the user enters a wisselNumber starting at 1 and hence one is subtrackter from the wisselNummer to get the index in the array.
And lastly your loop() function
void loop()
{
Serial.print("wisselnummer: ");
while (Serial.available() == 0) {}
wisselNummer = Serial.parseInt();
Serial.print(wisselNummer);
Serial.print(", wisselstand: ");
while (Serial.available() == 0) {}
wisselStand = Serial.parseInt();
Serial.println(wisselStand);
zetWissel();
wisselNummer = 0;
}
As said, this keeps it scalable. Need 20 turnouts, just add them in the array.
Some more comments.
- It might be easier to remember letters for the turnout position instead of numbers; e.g. 'k' instead of '0' and 'r' instead of '1'.
- It might be advisable to set all turnouts in a predefined position in setup().
- You might benefit from reading Serial Input Basics - updated.