Does trying to get a series out for Intro count as a Project?

I have the code for the first 4 modules and 2 examples of combined modules coded and plan for more.

Can I go with if it's done in an Arduino Tutorial Example then it's okay to code if I want? See section 5 Examples.

The modules are tools for the user's code toolbox, I'm starting with a code-feedback tool, a loop() counter.

I have 3 more base sketches so far and those combined with the counter for comparisons. Last of those shows the un-delay technique to clean up sketches with delays.

No buttons yet.

So anyway is it okay to ask for help with comments and better names? Here is the loop counter:

// combine sketches loop counter 2018 by GoForSmoke @ Arduino.cc Forum
// Free for use, Mar 26/18 by GFS
// This sketch shows the number of times void loop() runs each second.

#include <avr/io.h>
#include "Arduino.h"

//int r,s,t; // WinXP compile jam fix
//byte a,b,c;

unsigned long loopCounter, startCounting;

void setup()
{
  Serial.begin( 115200 );
  Serial.println( F( "\n\n\n  Loop Counter, free by GoForSmoke\n" ));
  Serial.println( F( "This sketch shows the number of times void loop() runs each second." ));
  Serial.print( F( "Since loop" ));
  Serial.write( '(' );
  Serial.write( ' ' );
  Serial.write( ')' );
  Serial.print( F( " only runs this example, the number will be high." ));
  Serial.println( F( "My Uno mostly gets 131380 and 131381." ));
  Serial.println( F( "Printing decimal from 32-bit binary is *the* heavy load here.\n" ));
};

void LoopCounter() // from combosketch_01_loop_counter
{
  loopCounter++;
  if ( micros() - startCounting >= 1000000 ) // 1 second
  {
    startCounting += 1000000;
    Serial.println( loopCounter );
    loopCounter = 0;
  }
}

void loop()  // runs over and over, see how often
{            
  loopCounter++; // lowercase-l
  LoopCounter(); // capital-L
}

GoForSmoke:

#include <avr/io.h>

#include "Arduino.h"

Why the unnecessary #include directives?

GoForSmoke:

unsigned long loopCounter, startCounting;

I like to use the word timestamp in the variable names of timestamps. startCounting sounds more like the name of a boolean flag variable to me.

GoForSmoke:

  Serial.println( F( "This sketch shows the number of times void loop() runs each second." ));

Serial.print( F( "Since loop" ));
  Serial.write( '(' );
  Serial.write( ' ' );
  Serial.write( ')' );
  Serial.print( F( " only runs this example, the number will be high." ));

Why this strange way of printing "loop ( )"?

GoForSmoke:

  Serial.print( F( " only runs this example, the number will be high." ));

Serial.println( F( "My Uno mostly gets 131380 and 131381." ));

You're missing either a newline or space separating the two sentences.

GoForSmoke:

  if ( micros() - startCounting >= 1000000 ) // 1 second

{
    startCounting += 1000000;

I think the code might be easier to understand without the 100000 magic number:

const unsigned long microsecondsPerSecond = 1000000;

...

  if ( micros() - startCounting >= microsecondsPerSecond ) // 1 second
  {
    startCounting += microsecondsPerSecond;

GoForSmoke:

    startCounting += 1000000;

I don't think this is the best choice. That system is best when a specific interval is desired. Setting the timestamp variable to micros() is better when a specific duration is desired. I also think setting it to micros() makes the code easier for a beginner to understand.

GoForSmoke:

void loop()  // runs over and over, see how often

{           
  loopCounter++; // lowercase-l

You increment loopCounter in LoopCounter(), where it makes more sense to put it, so this causes the results to be double the actual loop count.

Hi,
Code gives me;

Loop Counter, free by GoForSmoke

This sketch shows the number of times void loop() runs each second.
Since loop( ) only runs this example, the number will be high.My Uno mostly gets 131380 and 131381.
Printing decimal from 32-bit binary is the heavy load here.

346936
353280
353280
353280
353282
353280
353282
353278
353280
353282
353278
353282
353280
353282

UNO is Sainsmart;

Tom... :slight_smile:

Hi,
XDRduino MEGA;
Loop Counter, free by GoForSmoke

This sketch shows the number of times void loop() runs each second.
Since loop( ) only runs this example, the number will be high.My Uno mostly gets 131380 and 131381.
Printing decimal from 32-bit binary is the heavy load here.

294534
299928
299928
299928
299930
299926
299930
299928
299928
299928
299928
299930
299926

Tom.... :slight_smile:

Variable names and function names that only differ in case (loopCounter and LoopCounter in this case) are a recipe for disaster.

You did comment a part out in the beginning of the code so I do no know if the following still applies.

Don't use single character variable names in the best of times (as they can't reflect what they are used for), and absolutely do NOT use a single character variable name if the variable has global scope (you will have a hard time finding where they are used).

Some say that it's acceptable for loops (for, while) but I still tend to spell them out (e.g. xPos and yPos).

My suggestion is to write the text that will accompany and explain your project now - before you go any further. I find when I start writing an explanation that shortcomings in my thought process and code show up very quickly.

I also find that reading a text out loud is a great way to identify bits that don't make sense.

As others have mentioned it also seems to me wise to use very meaningful variable names in tutorial code - longer names than you would probably use if you were writing code just for yourself.

...R

pert:
Why the unnecessary #include directives?

Holdovers from when it made a difference?

I like to use the word timestamp in the variable names of timestamps. startCounting sounds more like the name of a boolean flag variable to me.

maybe loopCountStartTime or loopCountStartMicros?

Why this strange way of printing "loop ( )"?

I used escape characters wrong, hit a blank and filled in that temp kludge.

You're missing either a newline or space separating the two sentences.

yup

I think the code might be easier to understand without the 100000 magic number:

const unsigned long microsecondsPerSecond = 1000000;

...

  if ( micros() - startCounting >= microsecondsPerSecond ) // 1 second

{
    startCounting += microsecondsPerSecond;



I don't think this is the best choice. That system is best when a specific interval is desired. Setting the timestamp variable to micros() is better when a specific duration is desired. I also think setting it to micros() makes the code easier for a beginner to understand.
You increment loopCounter in LoopCounter(), where it makes more sense to put it, so this causes the results to be double the actual loop count.

Ah hah! Now that one tells me I'm rushing along too fast. 67K makes much more sense on a 16MHz Uno.

I don't think that I want to set the counter to micros as opposed to adding the million. Micros granularity is part of that, I'd rather try to make a steady mark and collect to that.

I see other suggestions to take into account. I'll get changes done and come back to it.

I'm thinking of trying to show cooperative tasking through a modular tools explanation. Learn the tools, keep the tools, use the tools.

If I can hang in long enough, I might get a nice set, but some will have to be libraries the user does not need to know details, same as with Serial.

And for which way to go with buttons.... undecided. Library is an option, neat, clean and easy to use without trying to explain how code objects make it so much less details.

I have a bit of handicap when commenting. When I write code I think code. Comments are a different think. They slow me down and make me tired, they block my code think.

Maybe painters shouldn't write novels?

GoForSmoke:
I have a bit of handicap when commenting. When I write code I think code. Comments are a different think. They slow me down and make me tired, they block my code think.

Maybe painters shouldn't write novels?

I would keep comments to a minimum and invest the time in writing a good explanatory text. That way the writing thought process is separate from the coding.

...R

Well structured code and ‘good’ variable / function names can provide - as much as 50% of your inline documentation.

Next Rev, hope I remembered enough!

I changed the combosketch tag to toolbox, this is toolbox_01.

// toolbox_01 2018 by GoForSmoke @ Arduino.cc Forum
// Free for use, Mar 29/2018 by GFS
// This sketch counts times that loop has run each second and prints it.

/*
 * This is the first tool in the box, 
 * it lets you measure how responsive your sketch is on average.
 * you can see how changes affect the speed of a sketch
 */

//int r,s,t; // WinXP compile jam fix
//byte a,b,c;

void setup()
{
  Serial.begin( 115200 );
  Serial.println( F( "\n\n\n  Loop Counter, free by GoForSmoke\n" ));
  Serial.println( F( "This sketch counts times that loop has run each second and prints it." ));
  Serial.println( F( "On a 16MHz Uno I am usually getting 128204 to 128206.\n" ));
}

#define microsInOneSecond 1000000UL
void LoopCounter()
{
  static unsigned long loopCount, loopCountStartMicros; // static vars stay in memory

  loopCount++;
  if ( micros() - loopCountStartMicros >= microsInOneSecond ) // 1 second
  {
    loopCountStartMicros += microsInOneSecond; // for a regular second
    Serial.println( loopCount );
    loopCount = 0;
  }
}

void loop()  // runs over and over, see how often each second
{            
  LoopCounter(); 
}

pert:

  if ( micros() - startCounting >= microsecondsPerSecond ) // 1 second

{
    startCounting += microsecondsPerSecond;



I don't think this is the best choice. That system is best when a specific interval is desired. Setting the timestamp variable to micros() is better when a specific duration is desired. I also think setting it to micros() makes the code easier for a beginner to understand.

We don't agree.

I want a regular interval to count loops in. If I set to micros() after calculating a time difference then comparing it, my start time creeps forward. Arduino micros() is also 4us granular. I'll stick with regular seconds here.

How is the rest of the revised sketch?