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)