This is my second post of today. After my first problem was solved I got a new problem.
In this sketch I made a train decoder for my model railway. It almost work but only one pin is doing awkward.
It's declared and programmed like the other 3 but it goes sometimes random on and doesn't want to go off (only when I restart the Arduino or program). It just have to give a pulse to a relay. The other 15 pins which have the same configuration works perfectly
In all the places in your code where you have loops processing arrays like this:
int aantalAfremmodule = 8;
for (int i = 0; i <= aantalAfremmodule; i++)
{
if(digitalRead(Afremmodule[i]) == HIGH)
{
digitalWrite(Afremmodule[i],LOW);
Serial.println(digitalRead(Afremmodule[i]));
}
}
You need to make two changes.
Firstly, define an integer constant for each array which holds the number of elements in the array. Do this right next to the array declaration, and calculate the value from the array - do not type it in by hand:
int Afremmodule[8]= {23,25,27,29,31,33,35,37};
const int aantalAfremmodule = sizeof(Afremmodule) / sizeof(Afremmodule[0]);
Secondly, in all the places where you use a loop to process the array, use that global constant as the bounds of the loop, and use the less-than operator '<' not the less-than-or-equal operator '<='. As it is, you are overrunning the end of each array, resulting in you using invalid data. I'm reasonably confident you will find this is the cause of your elusive bug.
It gives you the number of elements in your array.
sizeof gives the number of bytes in its argument, so the total number of bytes in an array divided by the number of bytes in one element is the number of elements.
I tried the solution but it doesn't worked.
The system works like this:
Set relais on green (train depart), pulse 250 ms
Wait 10sec
Set relais back on red, pulse 250 ms
The pulse for red works perfectly but the pulse for green doesn't work it passed the whole programme (I see that on the Serial Monitor) but it doesn't want to set the pin for green off after 250 ms. The 7 other pins (red and green) works perfectly
Thanks for your reaction.
I think the problem situate in this structure (RijwegSpoor3):
void AfremmoduleRijwegSpoor3()
{
digitalWrite(Sein[4], LOW);
digitalWrite(Sein[5], HIGH);
Serial.println("Sein 3 op GROEN");
digitalWrite(Afremmodule[4], LOW);
digitalWrite(Afremmodule[5], HIGH);
Serial.println("Afremmodule 3 op GROEN");
RijwegSpoor3Status = 3;
Serial.println("Rijweg 3 op PAUSE voor 10 seconden");
SchakeldelayAfremmodules.check();
}
After this structure he will do this command: SchakeldelayAfremmodules.check();
In the other 3 (the same structures but with different pins) structures works it perfectly, and the second time I call it in this structure it works perfectly for Afremmodule[4]. But it doesn't work for Afremmodule [5].
This is the action that will complete by calling: SchakeldelayAfremmodules.check();
void StatusCheckAfremmodules()
{
const int aantalAfremmodule = sizeof(Afremmodule) / sizeof(Afremmodule[0]);
for (int i = 0; i < aantalAfremmodule; i++)
{
if(digitalRead(Afremmodule[i]) == HIGH)
{
digitalWrite(Afremmodule[i],LOW);
Serial.println(digitalRead(Afremmodule[i]));
}
}
}
Afremmodule[5] stays on but it has to be off after 250 ms.