Strange behavior for Arduino Mega + Internet Shield

Hi All,

will try to keep this short and simple: I'm using an Arduino Mega board and Ethernet Shield connected to it. Everything works perfect, several successful setups in the last few days.

Now, today I'm using two switches interfaced on pins 2 and 3. By switching any of the switches, other 3 pins are circling the HIGH signal (e.g. one click and HIGH changes from pin 6 to pin 7 etc.)

To do that, I'm declaring pins 2 and 3 as inputs:

int iPinIn[2] = {2, 3};

then, in the Setup() I'm iterating through those and declaring them as inputs with

pinMode(iPinIn*, INPUT);* however, this does NOT work. It works perfectly though, if in case instead of: int iPinIn[2] = {2, 3}; I put something like int iPinIn[3] = {2, 3, 53}; so, if I put an additional pin as INPUT in the code (although I don't use it and don't even ground it on the board), the sketch will work Any idea why the sketch only works when adding another input? If this one is not added, the sketch doesn't work correctly, I sometimes need to push several times the switches to see a result. Thanks!

Which pins are you using ? Pins 4 and 10 are in use by the Ethernet Shield. Pin 53 is part of the SPI bus.

Your “inPinIn” is the name of the array. It is not an element of the array. The pinMode() needs a pin number, and the pin numbers are in the elements of the array.

int iPinIn[10] = { 2, 3, 22, 23, 24, 25, 40, 41, 42, 43 };
...
for( int i=0; i<10; i++)
{
  pinMode( iPinIn[i], INPUT);
}

Yes I know that, I just didn’t paste everything, so I don’t make the post too long. This is what I have:

int iPinIn[2] = {2, 3}; //define the input pins

void setup()
{

for (int i=0; i<=sizeof(iPinIn); i++) {
pinMode(iPinIn*, INPUT); //iterate through input pins to declare them as INPUTs*

  • }*

    }
    So, this doesn’t quite work. The result is intermittent. I need to sometimes push several times the input switches to get a result. However, if I add one random input pin (e.g. 45) to the array, everything will work perfect. Again, I’m not interfacing the 45-pin with anything on the board, just adding it in the code.
    so, this will work:
    int iPinIn[3] = {2, 3, 45}; //define the input pins by adding a random one in the array
    Condensed version: I need to put one more item in the array besides the pins used for my sketch to work.
    I really appreciate your help - thanks!!

Please use code tags, it is explaned at number 7 on this page : forum.arduino.cc/index.php/topic,148850.0.html

I can’t read you sketch without code tags because the [ i ] is text layout command for italic

What I learned over the years is not to try to solve a problem by creating bad code as a fix.
That is why I want to ignore, that you can fix it with a random extra code. It could have to do with the problem, or with another problem or caused by something else that was caused by the problem.

Let’s find the real cause… there it is, the sizeof() is the number of bytes of an integer array, not the number of elements.

An array can be integers, or floats, or even an array of struct.
This is the normal way to do that:

int xPin[] = { 2, 3 };
...
for (int i=0; i<sizeof(xPin)/sizeof(int); i++)
{
  ..
}

That is how it is always done, the sizeof() of the total array divided by the sizeof() of a single element.

OK, thanks for the explanations.
I fixed my code by dividing everywhere sizeof(), in this case, int
And still, I have the same behavior. If I use the additional (random) pin, it works exactly as expected.
Once I remove it, I either have to push the switches for a bit longer, or double/triple click them, in order to get the right result.
Right now my code looks as follows:

int iPinIn[3] = {2, 3, 45}
.......
void setup() {
.......
    for (int i=0; i<sizeof(iPinIn)/sizeof(int); i++) {
      pinMode(iPinIn[i], INPUT);
    }
.......
}

The above is the working scenario.
I attached all the code in a zip file. I removed all the code related to Ethernet connectivity, to make the code shorter.
While I appreciate your time looking into this, please do not spend too much time on it. Anyway, I have a working example, although it’s using bad code that is inexplicable to me.
Thank you.

my_code.zip (2.07 KB)

O no, you settle for something bad. That will boomerang back to you.

You disable ChipSelect pin 10. I think that is not enough.

I can reduce a few problems by making the SPI bus idle before doing something.
Like this : http://forum.arduino.cc/index.php?topic=281741.msg1980190#msg1980190
It sets every possible chip select high (either on a Uno or a Mega), and waits 1 ms.

Bug. This code

for (int i=0; i<=iNumOfCircles_B12; i++) {

is for an array with three elements, ‘i’ will be 0,1,2
But iPinIn_B12 has only two.
If you add a third ‘dummy’ number, the problem is gone.
I think the “<=” should be “<”.
Or use the: sizeof(iPinIn) / sizeof (int)

I have not checked the deboucing carefully, and I did not check the use of arrays and indexes in md_switchOption_Bx().

When it would be my sketch, I would use Bounce2 library for the buttons; use zero as starting index for everything; and combine both the ms_switchOption_Bx() functions into a single function :stuck_out_tongue:

OK, sir - I had a chance to test everything again only now.
The issue indeed is hidden in the line of code you pasted in your last post.
The zero-indexed array expects 2 elements, so i< should be used and not i<=
This was an oversight.
Also, I’m taking into consideration all your comments and will be definitely working tonight to 1) merge both functions 2) make everything zero-indexed and 3) use the Bounce2 library.
Thanks a lot for looking into this! Highly appreciated!!

Regards

Only the ‘<’ instead of ‘<=’ and making the SPI bus idle are the important things.

The rest is just extra, in case you like to write software and want to make it look better :wink: