[SOLVED] Issue with SD card while using a 'for' loop

Hi all,

Thank you for reading this again. The code posted here is an MRE of a much larger code.

Goals:

  • Create an arbitrary array with 10 elements, each of which is the following 10-bit integer: 0000000000.
  • Change the first 6 members of this array to 0000000100.
  • Write the "non-0000000000" members of this array into an SD card.

Code:

#include <SD.h>

File myFile;
int intermDataLog [10];

void setup () {
  pinMode (53, OUTPUT);
  SD.begin (53);

  for (int i = 0; i <= 9; i++) {
    intermDataLog [i] = (0 << 9);
  }
  for (int i = 0; i <= 5; i++) {
    bitWrite (intermDataLog [i], 2, 1);
  }
  myFile = SD.open ("testing.txt", FILE_WRITE);
  for (int i = 0; i <= 9; i++) {
    if (intermDataLog [i] != (0 << 9)) {
      for (int ii = 9; ii >= 0; i--) {
        myFile.print (bitRead (intermDataLog [i], ii));
      }
      myFile.print ("\n");
      intermDataLog [i] = (0 << 9);
    }
  }
  myFile.close ();

}

void loop () {

}

Reality:
The SD card reader's "activity" LED keep going on and off in a seemingly infinite loop. When I take out the SD card, a file with the desired file title is created, but it's empty.

Please let me know if I need to provide anything else. Thank you again.

    intermDataLog [i] = (0 << 9);

did you mean

   intermDataLog [i] = (1 << 9);

?

also

  for (int i = 0; i <= 9; i++) {
    if (intermDataLog [i] != (0 << 9)) {
      for (int ii = 9; ii >= 0; i--) {

Did you mean to decrement 'i' or 'ii' in the last line there?

aarg:

    intermDataLog [i] = (0 << 9);

did you mean

    intermDataLog [i] = (1 << 9);

?

Thank you for your response. Wouldn't that make intermDataLog [eye] = 1000000000?

My goal is for it to be 0000000000.

ArianKS:
Thank you for your response. Wouldn't that make intermDataLog [eye] = 1000000000?

My goal is for it to be 0000000000.

The compiler initializes global variables to 0. To make it 00000000, you just:

intermDataLog [i] = 0;
   intermDataLog [i] = (0 << 9);

is the same as

  intermDataLog [i] = 0;

Change the first 6 members of this array to 0000000100.

Example for the first element:

   intermDataLog [0] = (1 << 3);

Hint: "non-zero" array element can be detected as simply as

 if (intermDatalog[i])  do_something();

Thank you all. I've added your suggestion—and for the sake of simplicity, I'm using Serial instead of SD here. The issue still remains unsolved:

Code:

int intermDataLog [10];

void setup () {
  Serial.begin (9600);
  while (!Serial) {
    
  }


  for (int i = 0; i <= 9; i++) {
    intermDataLog [i] = 1000000000;
  }
  for (int i = 0; i <= 5; i++) {
    bitWrite (intermDataLog [i], 2, 1);
  }

  for (int i = 0; i <= 9; i++) {
    if (intermDataLog [i] != 1000000000) {
      for (int ii = 9; ii >= 0; i--) {
        Serial.print (bitRead (intermDataLog [i], ii));
      }
      Serial.print ("\n");
      intermDataLog [i] = 1000000000;
    }
  }

}

void loop () {

}

Serial monitor output:
⸮⸮⸮⸮⸮⸮⸮⸮

The number of '⸮' keeps on increasing horizontally.

an int is only 16 bits long on Arduinos
an int can only be between −32,768 and 32,767
intermDataLog = 1000000000; will not work.....
I think what you're after is
intermDataLog = 0b1000000000;
the 0b is telling the compiler that the number after it should be interpenetrated as binary number
instead of 1 billion.

Cheetor:
an int is only 16 bits long on Arduinos
an int can only be between −32,768 and 32,767
intermDataLog = 1000000000; will not work.....
I think what you're after is
intermDataLog = 0b1000000000;
the 0b is telling the compiler that the number after it should be interpenetrated as binary number
instead of 1 billion.
[/quote]
Thank you for your response. I added your recommendation, but there's a different issue:
Code:
```
*int intermDataLog [10];

void setup () {
  Serial.begin (9600);
  while (!Serial) {
   
  }

for (int i = 0; i <= 9; i++) {
    intermDataLog [i] = 0b1000000000;
  }
  for (int i = 0; i <= 5; i++) {
    bitWrite (intermDataLog [i], 2, 1);
  }

for (int i = 0; i <= 9; i++) {
    if (intermDataLog [i] != 0b1000000000) {
      for (int ii = 9; ii >= 0; i--) {
        Serial.print (bitRead (intermDataLog [i], ii));
      }
      Serial.print ("\n");
      intermDataLog [i] = 0b1000000000;
    }
  }

}

void loop () {

}*

```
Serial output:
11111111111111111111111111111111111111111111111111111111111111111111111111111111111
The 1s just keep on increasing horizontally.

Hi all—it's fixed. Thank you so much, I learned a lot!

      for (int ii = 9; ii >= 0; i--) {

Oops.

jremington:

      for (int ii = 9; ii >= 0; i--) {

Oops.

Yep, one of the beauties of my life.

5 hours basically wasted.

ArianKS:
Yep, one of the beauties of my life.

5 hours basically wasted.

Welcome to the life of a software developer
It happens to all of us at times no matter how many years we've been doing it.