Critique My Code

Hello Forum,
I'm an Arduino beginner, and this is the first time I have ever posted to this forum, my code is below.

#include <Metro.h>

int button = 2;
int led = 13;
int points = 0;
int pointState = 3;

boolean ledState = LOW;

Metro ledButton = Metro(0);

void setup()
{
  Serial.begin(9600);
  pinMode (led, OUTPUT);
  pinMode (button, INPUT);
}

void loop()
{
  boolean buttonState = digitalRead(button);
  
  if (ledButton.check() == 1)
  {
      if (ledState == LOW)
      {
        ledState = HIGH;
      
        if (pointState == HIGH){
          points++;
          pointState = 3;
      }
      ledButton.interval(500);
    }
  
    else
    {
      ledState = LOW;
      
      if (pointState == LOW){
        points--;
        pointState = 3;
      }
      Serial.println(points);
    }
  }
 
  if (buttonState == HIGH && ledState == HIGH)
    pointState = HIGH;
    
  if (buttonState == HIGH && ledState == LOW)
    pointState = LOW;
    
  digitalWrite (led,ledState);
}

sorry for not commenting it, I don't usually share my code.

Note the code is working.

The goal is to blink an led, if the user presses a button while the led is on, they get a point and it's printed to the console, if they press the button while the led is off, they lose a point which is printed to the console. (I don't have an LCD panel yet)

My question is, is this good code? or would you have done it better/more efficiently?

Would it be better to make a second metro object at a different interval that registers if the button is pressed or not while the light is on or off?

Others ways you would have handled the way points are given? (currently the point is printed to the console until after you've pressed it)

Maybe I could have used the millis() function to handle some of this problem?

I'm open to any suggestions that would make the code better?

(I Tried to post this under the Programming Questions board than delete it from here, but it won't let me delete it, So sorry for it being under two locations)

const byte button = 2; // makes sure you tell everyone you're not 
const byte led = 13; //going to change it
int points;  // it already is zero.

AWOL:

const byte button = 2; // makes sure you tell everyone you're not 

const byte led = 13; //going to change it
int points;  // it already is zero.

I tried to move this post to programming and then delete it from here, but it wouldn't let me

sorry for not commenting it, I don't usually share my code.

comments are not for others........when you want to modify the code, months or even years later, they save you hours :slight_smile:

comments are not for others........when you want to modify the code, months or even years later, they save you hours

That is so true. Sometimes I think I go over the top.If I found something hard to do I don't just comment the code I wrote but I include references or even explanations of the principles involved so that the code plus comments becomes a self contained entity. If I come back later I don't need to root around figuring why I did what I did. I am overdoing it or do others do the same?

alnath:

sorry for not commenting it, I don't usually share my code.

comments are not for others........when you want to modify the code, months or even years later, they save you hours :slight_smile:

Thanks for the notice

boolean ledState = LOW;

The only values that should be assigned to a boolean variable are true or false. If you want to use other value, boolean is not the right type.

Metro ledButton = Metro(0);

Some explanation of what the argument to the Metro constructor would be useful.

int button = 2;
int led = 13;
int points = 0;
int pointState = 3;

In the same way that state variables should have state in the name, so should pin number variables. That helps tremendously when you go to read a pin and save it's state. Which variable goes where is self-documenting.

        if (pointState == HIGH){
          points++;
          pointState = 3;

WTF? Mixing LOW and HIGH and 3 in assigning a value to a variable with state (it should be either HIGH or LOW) makes no sense.

      Serial.println(points);

If you worked for me, you'd get the rest of the day off. Without pay. NEVER print a value without some explanatory text. That makes debugging way harder than it needs to be.

And, remember, you asked.

I use LOW, HIGH, and 3 because I need lets say a neutral state, so should I just use 0, 1 , and 2 instead?

Also I don't have an lcd screen so that's why I print the points to the console.

I used boolean, because low is the same as false, and high is the same is true. Or should I not do that to make it easier to read?

Metro(0) The 0, is the initial interval set

I use LOW, HIGH, and 3 because I need lets say a neutral state, so should I just use 0, 1 , and 2 instead?

Yes, along with a better name. Pins have state - on or off. There is no "neutral state", nor does that make sense.

Also I don't have an lcd screen so that's why I print the points to the console.

So, some number appears in the Serial Monitor, and everyone is supposed to know what it means.

Serial.print("points = ");
Serial.println(points);

removes all ambiguity.

I used boolean, because low is the same as false, and high is the same is true. Or should I not do that to make it easier to read?

No, they aren't. They have the same value, but that is not the same thing. Assign true or false to boolean variables. Assign HIGH and LOW or 0 and 1 and 2 and 3 to byte variables.

The 0, is the initial interval set

const int initialInterval = 0;
Metro ledButton =  Metro(intialInterval);

and nobody needs to ask what the value means.

Okay thanks for the clarification, and by neutral state what I mean is i need 3 option, one option makes the point value go down, the other up, and the "neutral" option to do keep from being either until input is taken.

duinoDude:
I use LOW, HIGH, and 3 because I need lets say a neutral state, so should I just use 0, 1 , and 2 instead?

LOW = 0. Any value other than 0 is HIGH. A digital pin can only be LOW or HIGH.

A digital pin can only be LOW or HIGH.

But, pointState is never used to alter a pin state. Which is why having state in the name is so confusing.

Okay just forget I said LOW or HIGH for the pointState. I changed the code. All I meant was
(I've changed the name of pointState to pointOption)

if (pointOption == 0){ 
points--;
pointOption = 2;
}

if (pointOption == 1){
points ++;
pointOption = 2;
}

pointOption = 2 is there to keep the points from being added or subtracted until input is given. The code has always done that my names were just confusing.

I never actually meant that pins on the Arduino had a neutral state, I just meant that pointState had three options add, subtract, or nothing "neutral." Sorry if my naming was absolutely horrible. I've been working on this to long and I'm a little "head-ached" right now.

Learn to use enums instead of integer literals for state values. Enums are much more expressive and also type safe

duinoDude:
Thanks for the notice

Also remember when you do comment - comment on the why, not on the how (the how is your code).

In fact, one way that some people write code is to write the comments first, then go back through and fill out the code for each comment.

Also, remember to change your comments if you change the code; nothing irks us programmers more than to see a comment that says one thing, while the code is doing something completely different (and you'll hate it, too - when you come back later, fixing something, and wondering to yourself which is the correct version - the code, or the comment?)...

Sorry if my naming was absolutely horrible.

"Absolutely horrible" is not the same as "there is room for improvement". The thing to keep in mind is that there are a number of professional programmers in here, and we've learned (the hard way) the value of good names. Starting off using good names is far better than changing naming practices later.