Inexplicable action of the Arduino MEGA

Hello,

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

It's pin 31 (Afremmodule[5] in the sketch).

http://www.modeltreinen.comze.com/Arduino/ModuleDecoderV1_5_DCC_4.ino

regards,
minitreintje

int Afremmodule[8]= {23,25,27,29,31,33,35,37};

Pin 31 is index 4, not 5

Think of an array along the lines of a ruler. A foot long ruler is 12 inches long. With the first inch starting at offset 0 and the last inch at 11.

Same way with an array.

Oh I am sorry it's pin 33 than...
I can't make a patron for it, it completely random :open_mouth: I checked if I didn't use it twice somewhere but I can't find it.

regards,
minitreintje

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.

for (int i = 0; i < aantalAfremmodule; i++)

I will try it tomorrow, I will keep you up to date :slight_smile:

regards,
minitreintje

I tried to insert the solution but I don't get this line:

int Afremmodule[8]= {23,25,27,29,31,33,35,37};
const int aantalAfremmodule = sizeof(Afremmodule) / sizeof(Afremmodule[0]);

What do you mean with: sizeof(Afremmodule) / sizeof(Afremmodule[0]); ?
I don't get it ?

regards,

minitreintje

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.

Thanks, it's better to understand what you are doing otherwise you don't learn anything...

regards,
minitreintje

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 :open_mouth:

regards,
minitreintje

I don't see any code, so it's not possible to tell where you're going wrong.

You can find it in the first post. It's to large to place it with the [code ][ /code] tags.

regards,
minitreintje

I really can't be bothered to trawl through 700+ lines of code, but this
if ((Time - Straatverlichting) == 720000 && onoff==true) jumped out at me.

"720000" cannot be represented in 16 bits.

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.

regards,
minitreintje

Somebody? I know it's difficult but I can't understand why it works properly with the other 7...

regards,
minitreintje