Counting loop operations.

Hi everyone.

I wonder if someone may be able to help with this.

I'd like to add how many times the loop has cycled, to a column on my CSV data. The far left column in the example below. This would make plotting the data afterwards much easier. Essentially increment a counter I suppose, which I could then print to the SD card.

Something like:

  1. Value_x Value_y Value_z
  2. Value_x Value_y Value_z

Many thanks.

MonkeyBoy11:
Essentially increment a counter I suppose, which I could then print to the SD card.

Yep.

http://arduino.cc/en/Tutorial/Variables
http://www.arduino.cc/en/Reference/VariableDeclaration
http://arduino.cc/en/Reference/Increment

You start by using a For Loop in the first place - and you already have the index.

Thanks for the links guys, but I'm still struggling on what to pick up on to trigger the counter? Would you be able to give anymore guidance. An example sketch woul be great. The most simple part of the sketch and I can't get my head around it! :0

MonkeyBoy11:
I'd like to add how many times the loop has cycled, to a column on my CSV data. The far left column in the example below. This would make plotting the data afterwards much easier. Essentially increment a counter I suppose, which I could then print to the SD card.

Something like:

  1. Value_x Value_y Value_z
  2. Value_x Value_y Value_z

Well, I can't get my head around it either, but it's not because it's a tough problem. In fact, from your description, I cannot even decide if it's tough or easy, because I can't begin to figure out what you are trying to do, and what you are trying to do it with.

How many times WHAT LOOP has cycled?

What CSV data? I don't see any there.

Where is the data coming from, and how is it getting to the Arduino?

Does the far left column contain 1. and 2. ? Or does it contain Value_x?

Please describe what you have for input, and what you are trying to do with it.

int k=0;

loop...

  k=k+1; 
Serial.print (k);
otherstuff;

You might even go (at anywhere in the loop)

  if (k>9 )
  { 
do something;
k=0;
}

It really can't be any harder than that

MonkeyBoy11:
Thanks for the links guys, but I'm still struggling on what to pick up on to trigger the counter?

...
An example sketch would be great.

Why not!

/* Loop counter
 ______________________________________   */

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

#define lcdAddr 0x20 // set the address of the I2C device the LCD is connected to
// create an lcd instance with correct constructor for how the lcd is wired
//  to the I2C chip.  In this case, the www.mjkdz.com module
LiquidCrystal_I2C lcd(lcdAddr, 4, 5, 6, 0, 1, 2, 3, 7, NEGATIVE); // addr, EN, RW, RS, D4, D5, D6, D7, Backlight, POLARITY

int startstring = 0;      // recognition of beginning of new string
int charcount = 0;        // keeps track of total chars on screen

void setup() {

  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  lcd.begin(16,2);        // initialize the lcd as 20x4 (16,2 for 16x2)
  lcd.home ();            // go home to character 0 on line 0 (1st line)
  lcd.print(" Simple counter ");  
  lcd.setCursor(0,1);     // character 0 on line 1 (2nd line)
  lcd.print ("   Version 01 ");

  pinMode(13, OUTPUT);

}

void loop() {

  for(long j=0; j<65535 ;j++) { // This is your loop!

    Serial.println(j);          // Loop content
    digitalWrite(13,HIGH);		
    lcd.setCursor(0,1);     // character 0 on line 1 (2nd line)
    lcd.print ("    ");
    lcd.print(j); 
    lcd.print ("          ");
    delay(250);
    digitalWrite(13,LOW);		
    delay(250);                 // End of loop content

  } 		
}