"for" statement looping 8, not 12 times

I have written a "for" statement but it only loops 8 times, not 12 times. Changing 12 to 15, to see what happens, and it still only loops 8 times.

for (i = 0; i < 12; ++i) {
output_bit(1);
Serial.print(i);

Serial Monitor output = 01234567.

How do I get the "for" statement to loop 12 times?

Something else is changing i. Post the full code.

post your entire code

const uint8_t In3 = 7;
const uint8_t In4 = 6;
const uint8_t bit_timings[2] = {100, 58};

struct Packet{
  uint8_t address[9];
  uint8_t command[9];
  uint8_t XOR[9];
} train_1 = {{0, 0, 0, 0, 0, 0, 1, 1, 0}, // 8-bit address + "0" at LSB
             {0, 1, 1, 0, 1, 1, 1, 1, 0}, // 8-bit command + "0" at LSB
             {0, 1, 1, 0, 1, 1, 0, 0, 1}  // 8-bit error check + "1" at LSB. XOR precalculated. This will be changed when I get the program to work.
            };
            
void output_bit(uint8_t bit_val)
{
  digitalWrite(In3, LOW);
  digitalWrite(In4, HIGH);

  delayMicroseconds(bit_timings[bit_val]);

  digitalWrite(In3, HIGH);
  digitalWrite(In4, LOW);

  delayMicroseconds(bit_timings[bit_val]);
}

void goStraight(struct Packet train)
{
  uint8_t i;

  //Send Packet preamble. send 12 Ones and a Zero
  for (i = 0; i < 12; ++i) {
    output_bit(1);
  output_bit(0);
  }

  //send Address byte
  for (i = 0; i < 9; ++i) {
    output_bit(train.address[i]);
  }

  //send Instruction byte
  for (i = 0; i < 9; ++i) {
    output_bit(train.command[i]);
  }

  //send Error Detection byte
  for (i = 0; i < 9; ++i) {
    output_bit(train.XOR[i]);
  }

  //Send Packet End bit
  //output_bit(1);
}

void setup()
{

  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT);

  // Initialise outputs(?)
  digitalWrite(In3, HIGH);
  digitalWrite(In4, LOW);

}

void loop()
{

  goStraight(train_1);

}

Is it this for loop

  for (i = 0; i < 12; ++i)

or this for loop

  for (i = 0; i < 9; ++i)

that only loops 8 times ?

Try printing the value of i inside the for loops so that you can see what what is going on with the loop variable

when I add a Serial.println (i) like you did I get the following

0
1
2
3
4
5
6
7
8
9
10
11

When I type in:

Serial.begin(9600);

//Send Packet preamble. send 12 Ones and a Zero
for (i = 0; i < 12; ++i) {
Serial.print(i);
output_bit(1);
output_bit(0);
}

I get: 01234567891. Should I get 01234567891011?

When I type in:

Serial.begin(9600);

//Send Packet preamble. send 12 Ones and a Zero
for (i = 0; i < 12; ++i) {
Serial.println(i);
output_bit(1);
output_bit(0);
}

I get

0
1
2
3

gcjr: how did you write your code?
Thanks

This:

const uint8_t In3 = 7;
const uint8_t In4 = 6;
const uint8_t bit_timings[2] = {100, 58};

struct Packet
{
  uint8_t address[9];
  uint8_t command[9];
  uint8_t XOR[9];
} train_1 = {{0, 0, 0, 0, 0, 0, 1, 1, 0}, // 8-bit address + "0" at LSB
  {0, 1, 1, 0, 1, 1, 1, 1, 0}, // 8-bit command + "0" at LSB
  {0, 1, 1, 0, 1, 1, 0, 0, 1}  // 8-bit error check + "1" at LSB. XOR precalculated. This will be changed when I get the program to work.
};

void output_bit(uint8_t bit_val)
{
  digitalWrite(In3, LOW);
  digitalWrite(In4, HIGH);
  delayMicroseconds(bit_timings[bit_val]);
  digitalWrite(In3, HIGH);
  digitalWrite(In4, LOW);
  delayMicroseconds(bit_timings[bit_val]);
}

void goStraight(struct Packet train)
{
  uint8_t i;
  //Send Packet preamble. send 12 Ones and a Zero
  for (i = 0; i < 12; ++i)
  {
    Serial.print(i);
    output_bit(1);
    output_bit(0);
  }
  Serial.println();
  //send Address byte
  for (i = 0; i < 9; ++i)
  {
    output_bit(train.address[i]);
  }
  //send Instruction byte
  for (i = 0; i < 9; ++i)
  {
    output_bit(train.command[i]);
  }
  //send Error Detection byte
  for (i = 0; i < 9; ++i)
  {
    output_bit(train.XOR[i]);
  }
  //Send Packet End bit
  //output_bit(1);
}

void setup()
{
  Serial.begin(9600);
  pinMode(In3, OUTPUT);
  pinMode(In4, OUTPUT);
  // Initialise outputs(?)
  digitalWrite(In3, HIGH);
  digitalWrite(In4, LOW);
}

void loop()
{
  goStraight(train_1);
  delay(1000);
}

Gives me

01234567891011
01234567891011
01234567891011
01234567891011
...

Thanks wildbill.

I figured out why my code did not work. I did not use auto-format and was left with some weird code that ran but did not give the desired results.

I did not use auto-format and was left with some weird code that ran but did not give the desired results.

Auto format changes the layout of the code not its logic, order of statements etc so it cannot fix problems

Do you mean that Auto format revealed problems with your code that you subsequently fixed ? I find that having each { and } on its own line in the program is helpful is showing up code blocks and have Auto format set up to do that for me.

When examining code posted here it is the first thing that I do once the code has been posted into the IDE. This often reveals logical flaws in the program or orphan code not inside the code block of for loops and while loops.

UKHeliBob:
I find that having each { and } on its own line in the program is helpful is showing up code blocks and have Auto format set up to do that for me.

How do you alter the default behavior of Auto Format?

Open the folder where preferences.txt is situated. You can get there from File/Preferences in the IDE and clicking on filename near the bottom of the screen

In that folder you will find a file named formatter.conf which can be edited. Here is mine that I have changed to suit me

# This configuration file contains a selection of the available options provided by the formatting tool "Artistic Style"
# http://astyle.sourceforge.net/astyle.html
#
# If you wish to change them, don't edit this file.
# Instead, copy it in the same folder of file "preferences.txt" and modify the copy. This way, you won't lose your custom formatter settings when upgrading the IDE
# If you don't know where file preferences.txt is stored, open the IDE, File -> Preferences and you'll find a link

# 2 spaces indentation
indent=spaces=2

# also indent macros
indent-preprocessor

# indent classes, switches (and cases), comments starting at column 1
indent-classes
indent-switches
indent-cases
indent-col1-comments

# put a space around operators
 pad-oper

# put a space after if/for/while
 pad-header

# Move opening brackets onto new line
 --style=allman --style=bsd --style=break -A1

# delete empty lines in functions
 --delete-empty-lines 

# Insert space padding around operators. 
 --pad-oper

From the comment in the file it may originally have been situated somewhere else

Visit the url near the top of the file for more options

UKHeliBob:
In that folder you will find a file named formatter.conf which can be edited. Here is mine that I have changed to suit me
//...
From the comment in the file it may originally have been situated somewhere else

Visit the url near the top of the file for more options

Thanks. I did not have that file, but did locate it in the install directory ..\Arduino\lib\formatter.conf