Mi arduino board does not work

Hello everyone,

After waste a few hours without any progress, I decided to register in the forum and ask for help.

My problem is my board is not working properly. So far it is clear that the board is no capable of execute properly the if statements. Sometimes the if condition is ignored. I guess it has no sense to share the code, I have done a lot of tests, and it fails in a wide variety of cases. When I did some examples, it looks ok, but once you start to work in your own projects with if statements, the most stupid if for example if(a < b) {... is failing. Problems with int variables, and also with float variables. And that is quite frustrating, because I work for many years with Siemens PLC's without any issue.

Hardware data:
Board Mega 2560 - may be is a bad board? And I should buy a better one.
In this project I have connected 2 devices:
Accelerometer module MPU-6050
OLED display interface SSD1325

Software:
In my PC Arduino 1.8.19 (Windows Store 1.8.57.0)
Libraries:
for the accelerometer:
Wire.h
for the display I am using adafruit libraries:
SPI.h
Adafruit_GFX.h
Adafruit_SSD1325.h

Any one has experience problems with any of the items listed?

Thank you very much,
Rafael Martínez

yes. they give as hope but want not work properly. sad. And "if" is the worst from them. :face_vomiting:

1 Like

I have yet to see an Arduino that booted up that did not properly use the if statements. I will take a SWAG and say it is in your code which you decided was not worth us looking. From what I can see of your code it is not good. Not all languages implement the if statements exactly the same.

I felt the same way when I first tried to ride a bicycle. Everyone else rode it just fine, but it always failed me.

Hi,

Thanks for the feedback. I have done aditional test today. The "if" problem was linked to astrange bug with the for loop and the size of the data array.

This is a loop for gathering the data:

for (j = 1; j < 501; j++) {
    Wire.beginTransmission(MPU_addr);
    Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  
    //AcX[j] =Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) 
    AcX[j] = 200.0*cos(2.0*3.14159*160*j/1000.0)+200*cos(2.0*3.14159*90*j/1000.0+5.0);
   
    delay(1);
  }escribe o pega el código aquí

It is obvious that you are taking 500 samples, so the variable definition is of that size:

int j, AcX[500], AcXM, AcXmi;

(NOTE: used int because the accelerometer output is int)

When I tried to get the max and min value of the signal the result was always wrong, this is the loop:

//identification of the max (AcXM) and min value (AcXmi) of the signal
  AcXmi = 1000;
  AcXM = -1000;
 
  for (j = 1; j < 501; j++) {
      if (AcXM < AcX[j]) {
          AcXM = AcX[j];
      }

      if (AcXmi > AcX[j]) {
        AcXmi = AcX[j];
      }
  }

And the solution (at least for this bug), is increase the size of the array to 501, so if you define the array as:

int j,AcX[501],AcXM,AcXmi;

Now the code works properly. It has no sense to me, becuase the variable AcX[501] is not employed at all, but this works.

The code do many other things, so I stiill have to debug other parts, but at least the "fustrating point" seems solved...

If you define an array of 500 elements, then the indexes for those elements are AcX[0] to AcX[499].

Trying to access indexes outside the defined array, such as AcX[500] can result in unpredictable behaviour.

Yeah, that is exactly the problem.

I have just checked the array definition in arduino.

does you understand? It is non sense to guess. Somewhere is reference already. One thing work here not properly ans this is you. Read how to usi this forum 500 times to averaging the readings.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.