Problem using 'for' control structure

Hey everyone!
This is my first post on the forum, so I hope my etiquette is ok! (Please let me know if there is anyway I could make the question clearer/ easier to understand :))

SO I am trying to collect data about the moisture levels of soil (using two electrodes) and print this to a .txt file on an SD card. For testing purposes I am trying print the data to the file 10 times with a 1 second break in between.

Here is the code I'm working with (please excuse bad structure- I'm new and this is a draft)

#include <SPI.h>
#include <SD.h>

int Sensor_1 = 0;
int Sensor_2 = 1;
int Sensor_3 = 2;
int Sensor_4 = 3;

int Val_1;
int Val_2;
int Val_3;
int Val_4;

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
  myFile.println("1 \t 2 \t 3 \t 4");
  
for (int = 0; i<10, i++) {
  Val_1 = analogRead(Sensor_1);
  Val_2 = analogRead(Sensor_2);
  Val_3 = analogRead(Sensor_3);
  Val_4 = analogRead(Sensor_4);
  
  myFile.print(Val_1/10.23, 0);
  myFile.print("\t");
  myFile.print(Val_2/10.23, 0);
  myFile.print("\t");
  myFile.print(Val_3/10.23, 0);
  myFile.print("\t");
  myFile.println(Val_4/10.23, 0);
  }
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

I have got the print to file all figured out but it is just the 'for' (which I'm using to repeat the function 10 times) that is problematic.

The error i got was:

 error: expected unqualified-id before '=' token

 for (int = 0; i<10, i++) {

I think I understand that the 'int' wasn't 'introduced' to begin with- is that correct?
If so how can I make it work?

Thanks for all the help :slight_smile:

for (int  [b]i[/b] = 0; i<10, i++) {

Thanks heaps! sorry it ended up being a silly question :confused:

for (int i = 0; i<10**;** i++)

Budvar10:
for (int i = 0; i<10**;** i++)

On an 8-bit microcontroller, you can optimize by using an 8 bit integer:

for (byte i = 0; i<10; i++)

Yes, the comma operator - in an expression if simply throws away the value before the comma and returns the bit after the comma, easy to get burned by that. (If the expression is already in a comma-separated list
then comma is just the list separator (unless within parantheses).

int a[] = { 1, 2, 3, (4, 5) }; is the same as

int a[] = { 1, 2, 3, 5 };

However:
[tt]int a[] = { 1, 2, 3, (t = 6, 5) };

would have a side-effect on variable t

[/tt]

Here is how to use a for with two variables, neatly using the comma operator:

  for (int i = 0, j = 100 ; i < 10 ; i++,j++)
  {
  }

Note that its usually a very good idea to always use both ';' inside a for, even though they are
not mandatory - if you don't need them both, you probably should be using a while or do-while
loop instead.