3 LED blinking randomly using millis got reversed for some reason

hmmmm what about the "No punch" case, you have any ideas about it?

When elapsedMilliseconds >= (LEDonTimer.Interval + LEDoffTimer.Interval) - some value you want, there was no punch.

  • LEDonTimer.Interval is 1 second
  • LEDoffTimer.Interval is 1 second
  • When elapsedMilliseconds = 2 seconds, there was no punch.

Example, not tested:

int oneTenthSecond = 100;  //you pick what you want to use

. . .

if(elapsedMilliseconds >= (LEDonTimer.Interval + LEDoffTimer.Interval) - oneTenthSecond)
{
   Serial.println( “There was no punch”);
}
2 Likes

i have an issue right here, after a pressed the button, no matter it is fast or slow, after printed out "you are fast only xyz ms", it printed "there was no punch". Same thing happened with slow.

Show us your attempt.

2 Likes

Oops, yesterday i forgot to paste it

//================================================
#define LEDon                      HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff                     LOW

#define PUSHED                     LOW    //+5V---[Internal 50k]---PIN---[switch]---GND
#define RELEASED                   HIGH

#define CLOSED                     LOW    //+5V---[Internal 50k]---PIN---[switch]---GND 
#define OPEN                       HIGH


//============================================^================================================
//                     millis() / micros()   B a s e d   T I M E R S
//============================================^================================================
//To keep the sketch tidy, you can put this structure in a different tab in the IDE
//
//These TIMER objects are non-blocking
struct makeTIMER
{
#define MILLIS             1
#define MICROS             1000  //we can use this value to divide into a variable to get milliseconds

#define ENABLED            true
#define DISABLED           false

#define YES                true
#define NO                 false

#define STILLtiming        0
#define EXPIRED            1
#define TIMERdisabled      2

  //these are the bare minimum "members" needed when defining a TIMER
  int           TimerType;      //what kind of TIMER is this? MILLIS/MICROS
  unsigned long Time;           //when the TIMER started
  unsigned long Interval;       //delay time which we are looking for
  bool          TimerFlag;      //is the TIMER enabled ? ENABLED/DISABLED
  bool          Restart;        //do we restart this TIMER   ? YES/NO

  //================================================
  //condition returned: STILLtiming (0), EXPIRED (1) or TIMERdisabled (2)
  //function to check the state of our TIMER  ex: if(myTimer.checkTIMER() == EXPIRED);
  byte checkTIMER()
  {
    //=====================
    //is this TIMER enabled ?
    if (TimerFlag == ENABLED)
    {
      //=====================
      //has this TIMER expired ?
      if (getTime() - Time >= Interval)
      {
        //=====================
        //should this TIMER restart again?
        if (Restart == YES)
        {
          //restart this TIMER
          Time = getTime();
        }

        //this TIMER has expired
        return EXPIRED;
      }

      //=====================
      else
      {
        //this TIMER has not expired
        return STILLtiming;
      }

    } //END of   if (TimerFlag == ENABLED)

    //=====================
    else
    {
      //this TIMER is disabled
      return TIMERdisabled;
    }

  } //END of   checkTime()

  //================================================
  //function to enable and restart this TIMER  ex: myTimer.enableRestartTIMER();
  void enableRestartTIMER()
  {
    TimerFlag = ENABLED;

    //restart this TIMER
    Time = getTime();

  } //END of   enableRestartTIMER()

  //================================================
  //function to disable this TIMER  ex: myTimer.disableTIMER();
  void disableTIMER()
  {
    TimerFlag = DISABLED;

  } //END of    disableTIMER()

  //================================================
  //function to restart this TIMER  ex: myTimer.restartTIMER();
  void restartTIMER()
  {
    Time = getTime();

  } //END of    restartTIMER()

  //================================================
  //function to force this TIMER to expire ex: myTimer.expireTimer();
  void expireTimer()
  {
    //force this TIMER to expire
    Time = getTime() - Interval;

  } //END of   expireTimer()

  //================================================
  //function to set the Interval for this TIMER ex: myTimer.setInterval(100);
  void setInterval(unsigned long value)
  {
    //set the Interval
    Interval = value;

  } //END of   setInterval()

  //================================================
  //function to return the current time
  unsigned long getTime()
  {
    //return the time             i.e. millis() or micros()
    //=====================
    if (TimerType == MILLIS)
    {
      return millis();
    }

    //=====================
    else
    {
      return micros();
    }

  } //END of   getTime()

}; //END of   struct makeTIMER


//                        D e f i n e   a l l   a r e   T I M E R S
//============================================^================================================
/*example
  //=====================
  makeTIMER toggleLED =
  {
     MILLIS/MICROS, 0, 500ul, ENABLED/DISABLED, YES/NO  //.TimerType, .Time, .Interval, .TimerFlag, .Restart
  };

  Function examples:
  toggleLED.checkTIMER();
  toggleLED.enableRestartTIMER();
  toggleLED.disableTIMER();
  toggleLED.expireTimer();
  toggleLED.setInterval(100ul);
*/

//=====================
makeTIMER heartbeatTIMER =
{
  MILLIS, 0, 500ul, ENABLED, YES      //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER checkSwitchesTimer =
{
  MILLIS, 0, 50ul, ENABLED, YES       //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER LEDonTimer =
{
  MILLIS, 0, 1000ul, DISABLED, NO     //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER LEDoffTimer =
{
  MILLIS, 0, 1000ul, DISABLED, NO     //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER stopWatchTimer =
{
  MICROS, 0, 1000ul, ENABLED, YES    //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER commonTimer =
{
  MILLIS, 0, 150ul, ENABLED, YES     //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER machineTIMER =
{
  MICROS, 0, 1000ul, ENABLED, YES   //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};



//                           B E E P   S t a t e   M a c h i n e
//============================================^================================================
//the states in our machine
enum STATES : byte
{
  STARTUP, BEEP1, BEEP2, BEEP3, FINISHED
};
STATES beepState = STARTUP;


//                                        G P I O s
//============================================^================================================
const byte LEDpin1               = 3;
const byte LEDpin2               = 4;
const byte LEDpin3               = 5;
const byte speakerPin            = 6;
const byte button                = 8;
const byte heartbeatLED          = 13;

//variables
int beepDuration                 = 50ul;
int frequency0                   = 0;
int frequency1                   = 4500ul;  //fast hit frequency
int frequency2                   = 4000ul;  //slow hit frequency

int elapsedMilliseconds;
int oneTenthSecond = 100;  //you pick what you want to use
byte randNumber;
byte LEDselected;
byte lastButtonSwitch            = RELEASED;


//                                       s e t u p ( )
//============================================^================================================
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(LEDpin1, OUTPUT);
  pinMode(LEDpin2, OUTPUT);
  pinMode(LEDpin3, OUTPUT);

  pinMode(button, INPUT_PULLUP);

  randomSeed(analogRead(A0));

} //END of   setup()


//                                        l o o p ( )
//============================================^================================================
void loop()
{
  checkNoPunch();

  //================================================              T I M E R  heartbeatLED
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //is it time to toggle the heartbeat LED ?
  if (heartbeatTIMER.checkTIMER() == EXPIRED)
  {
    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //================================================              T I M E R  checkSwitches
  //is it time to check the switches ?
  if (checkSwitchesTimer.checkTIMER() == EXPIRED)
  {
    checkSwitches();
  }

  //================================================              T I M E R  machine
  //is it time to service our State Machine ?
  if (machineTIMER.checkTIMER() == EXPIRED)
  {
    checkMachine();
  }

  //================================================              T I M E R  stopWatch
  //is the TIMER enabled and is it time to add 1ms to the counter ?
  if (stopWatchTimer.checkTIMER() == EXPIRED)
  {
    //one more millisecond has gone by
    elapsedMilliseconds++;
  }

  //================================================              T I M E R  LEDon
  //is the TIMER enabled and is it time to turn OFF this LED ?
  if (LEDonTimer.checkTIMER() == EXPIRED)
  {
    //LED selected goes OFF
    digitalWrite(LEDselected, LEDoff);

    //disabled this TIMER
    LEDonTimer.TimerFlag = DISABLED;

    LEDoffTimer.enableRestartTIMER();
  }

  //================================================              T I M E R  LEDoff
  //is the TIMER enabled and is it time to turn ON the next LED ?
  if (LEDoffTimer.checkTIMER() == EXPIRED)
  {
    //disabled this TIMER
    LEDoffTimer.TimerFlag = DISABLED;
  }

  //================================================              Next random LED
  //has LED timing stopped ?
  if (LEDonTimer.TimerFlag == DISABLED && LEDoffTimer.TimerFlag == DISABLED)
  {
    randomly();
  }

  //================================================
  //other non blocking code goes here
  //================================================

} //END of   loop()


//                                    r a n d o m l y ( )
//============================================^================================================
void randomly()
{
  //select a LED connected to pin 3,4 or 5
  LEDselected = random(3, 6);

  //LED goes ON
  digitalWrite(LEDselected, LEDon);

  LEDonTimer.enableRestartTIMER();

  stopWatchTimer.enableRestartTIMER();

  //start our counter
  elapsedMilliseconds = 0;

} //END of   randomly()


//                                c h e c k M a c h i n e ( )
//============================================^================================================
void checkMachine()
{
  //================================================
  //service the current "state"
  switch (beepState)
  {
    //=====================
    case STARTUP:
      {
        //do startup stuff
      }
      break;

    //=====================
    case BEEP1:
      {
        //is the common TIMER expired ?
        if (commonTimer.checkTIMER() == EXPIRED)
        {
          tone(speakerPin, frequency0, beepDuration);
          commonTimer.enableRestartTIMER();
          beepState = BEEP2;
        }
      }
      break;

    //=====================
    case BEEP2:
      {
        //is the common TIMER expired ?
        if (commonTimer.checkTIMER() == EXPIRED)
        {
          //make next beep
          tone(speakerPin, frequency0, beepDuration * 2);
          commonTimer.enableRestartTIMER();
          beepState = BEEP3;
        }
      }
      break;

    //=====================
    case BEEP3:
      {
        beepState = STARTUP;
      }
      break;

    //=====================
    case FINISHED:
      {
      }
      break;

  } //END of  switch/case

} //END of   checkMachine()


//                              c h e c k S w i t c h e s ( )
//============================================^================================================
void checkSwitches()
{
  byte state;

  //================================================              button
  state = digitalRead(button);

  //was there a change in the switches state ?
  if (lastButtonSwitch != state)
  {
    //update to this new state
    lastButtonSwitch = state;

    //========================
    if (state == PUSHED)       //+5V---[Internal 50k]---PIN---[switch]---GND
    {
      if (LEDonTimer.TimerFlag == ENABLED && LEDoffTimer.TimerFlag == DISABLED)
      {
        //Start beep sequence
        frequency0 = frequency1;
        tone(speakerPin, frequency0, beepDuration);
        commonTimer.enableRestartTIMER();
        beepState = BEEP1;

        Serial.print("\nYou are Fast only ");
        Serial.print(elapsedMilliseconds);
        Serial.println("ms");
      }

      else if (LEDonTimer.TimerFlag == DISABLED && LEDoffTimer.TimerFlag == ENABLED)
      {
        //Start beep sequence
        frequency0 = frequency2;
        tone(speakerPin, frequency0, beepDuration);
        commonTimer.enableRestartTIMER();
        beepState = BEEP1;

        Serial.print("\nYou are Sloooow ");
        Serial.print(elapsedMilliseconds);
        Serial.println("ms");
      }
    }
  }

} //END of   checkSwitches()


void checkNoPunch(){
  if(elapsedMilliseconds >= (LEDonTimer.Interval + LEDoffTimer.Interval) - oneTenthSecond)
  {
    Serial.println("There was no punch");
  }
}

Review this version.

  • Remember it is not enough to just copy what is being offered.
    If your aim is to learn, you must ask questions.
//============================================^================================================

#define LEDon                      HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff                     LOW

#define PUSHED                     LOW    //+5V---[Internal 50k]---PIN---[switch]---GND
#define RELEASED                   HIGH

#define CLOSED                     LOW    //+5V---[Internal 50k]---PIN---[switch]---GND 
#define OPEN                       HIGH


//============================================^================================================
//                     millis() / micros()   B a s e d   T I M E R S
//============================================^================================================
//To keep the sketch tidy, you can put this structure in a different tab in the IDE
//
//These TIMER objects are non-blocking
struct makeTIMER
{
#define MILLIS             1
#define MICROS             1000  //we can use this value to divide into a variable to get milliseconds

#define ENABLED            true
#define DISABLED           false

#define YES                true
#define NO                 false

#define STILLtiming        0
#define EXPIRED            1
#define TIMERdisabled      2

  //these are the bare minimum "members" needed when defining a TIMER
  int           TimerType;      //what kind of TIMER is this? MILLIS/MICROS
  unsigned long Time;           //when the TIMER started
  unsigned long Interval;       //delay time which we are looking for
  bool          TimerFlag;      //is the TIMER enabled ? ENABLED/DISABLED
  bool          Restart;        //do we restart this TIMER   ? YES/NO

  //================================================
  //condition returned: STILLtiming (0), EXPIRED (1) or TIMERdisabled (2)
  //function to check the state of our TIMER  ex: if(myTimer.checkTIMER() == EXPIRED);
  byte checkTIMER()
  {
    //=====================
    //is this TIMER enabled ?
    if (TimerFlag == ENABLED)
    {
      //=====================
      //has this TIMER expired ?
      if (getTime() - Time >= Interval)
      {
        //=====================
        //should this TIMER restart again?
        if (Restart == YES)
        {
          //restart this TIMER
          Time = getTime();
        }

        //this TIMER has expired
        return EXPIRED;
      }

      //=====================
      else
      {
        //this TIMER has not expired
        return STILLtiming;
      }

    } //END of   if (TimerFlag == ENABLED)

    //=====================
    else
    {
      //this TIMER is disabled
      return TIMERdisabled;
    }

  } //END of   checkTime()

  //================================================
  //function to enable and restart this TIMER  ex: myTimer.enableRestartTIMER();
  void enableRestartTIMER()
  {
    TimerFlag = ENABLED;

    //restart this TIMER
    Time = getTime();

  } //END of   enableRestartTIMER()

  //================================================
  //function to disable this TIMER  ex: myTimer.disableTIMER();
  void disableTIMER()
  {
    TimerFlag = DISABLED;

  } //END of    disableTIMER()

  //================================================
  //function to restart this TIMER  ex: myTimer.restartTIMER();
  void restartTIMER()
  {
    Time = getTime();

  } //END of    restartTIMER()

  //================================================
  //function to force this TIMER to expire ex: myTimer.expireTimer();
  void expireTimer()
  {
    //force this TIMER to expire
    Time = getTime() - Interval;

  } //END of   expireTimer()

  //================================================
  //function to set the Interval for this TIMER ex: myTimer.setInterval(100);
  void setInterval(unsigned long value)
  {
    //set the Interval
    Interval = value;

  } //END of   setInterval()

  //================================================
  //function to return the current time
  unsigned long getTime()
  {
    //return the time             i.e. millis() or micros()
    //=====================
    if (TimerType == MILLIS)
    {
      return millis();
    }

    //=====================
    else
    {
      return micros();
    }

  } //END of   getTime()

}; //END of   struct makeTIMER


//                        D e f i n e   a l l   a r e   T I M E R S
//============================================^================================================
/*example
  //=====================
  makeTIMER toggleLED =
  {
     MILLIS/MICROS, 0, 500ul, ENABLED/DISABLED, YES/NO  //.TimerType, .Time, .Interval, .TimerFlag, .Restart
  };

  Function examples:
  toggleLED.checkTIMER();
  toggleLED.enableRestartTIMER();
  toggleLED.disableTIMER();
  toggleLED.expireTimer();
  toggleLED.setInterval(100ul);
*/

//=====================
makeTIMER heartbeatTIMER =
{
  MILLIS, 0, 500ul, ENABLED, YES      //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER checkSwitchesTimer =
{
  MILLIS, 0, 50ul, ENABLED, YES       //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER LEDonTimer =
{
  MILLIS, 0, 1000ul, DISABLED, NO     //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER LEDoffTimer =
{
  MILLIS, 0, 1000ul, DISABLED, NO     //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER stopWatchTimer =
{
  MICROS, 0, 1000ul, ENABLED, YES    //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER commonTimer =
{
  MILLIS, 0, 150ul, ENABLED, YES     //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

//=====================
makeTIMER machineTIMER =
{
  MICROS, 0, 1000ul, ENABLED, YES   //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};


//                           B E E P   S t a t e   M a c h i n e
//============================================^================================================
//the states in our machine
enum STATES : byte
{
  STARTUP, BEEP1, BEEP2, BEEP3, FINISHED
};
STATES beepState = STARTUP;


//                                        G P I O s
//============================================^================================================
const byte LEDpin1               = 3;
const byte LEDpin2               = 4;
const byte LEDpin3               = 5;
const byte speakerPin            = 6;
const byte button                = 8;
const byte heartbeatLED          = 13;

//variables
int beepDuration                 = 50ul;
int frequency0                   = 0;
int frequency1                   = 4500ul;  //fast hit frequency
int frequency2                   = 4000ul;  //slow hit frequency

int elapsedMilliseconds;
byte randNumber;
byte LEDselected;
byte lastButtonSwitch            = RELEASED;


//                                       s e t u p ( )
//============================================^================================================
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(LEDpin1, OUTPUT);
  pinMode(LEDpin2, OUTPUT);
  pinMode(LEDpin3, OUTPUT);

  pinMode(button, INPUT_PULLUP);

  randomSeed(analogRead(A0));

} //END of   setup()


//                                        l o o p ( )
//============================================^================================================
void loop()
{
  //================================================              T I M E R  heartbeatLED
  //condition returned: STILLtiming, EXPIRED or TIMERdisabled
  //every 500ms, is it time to toggle the heartbeat LED ?
  if (heartbeatTIMER.checkTIMER() == EXPIRED)
  {
    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //================================================              T I M E R  checkSwitches
  //every 50ms, is it time to check the switches ?
  if (checkSwitchesTimer.checkTIMER() == EXPIRED)
  {
    checkSwitches();
  }

  //================================================              T I M E R  machine
  //every 1ms, is it time to service our State Machine ?
  if (machineTIMER.checkTIMER() == EXPIRED)
  {
    checkMachine();
  }

  //================================================              T I M E R  stopWatch
  //every 1ms, is the TIMER enabled and is it time to add 1ms to the counter ?
  if (stopWatchTimer.checkTIMER() == EXPIRED)
  {
    //one more millisecond has gone by
    elapsedMilliseconds++;
  }

  //================================================              T I M E R  LEDon
  //1 second, is the TIMER enabled and is it time to turn OFF this LED ?
  if (LEDonTimer.checkTIMER() == EXPIRED)
  {
    //LED selected goes OFF
    digitalWrite(LEDselected, LEDoff);

    //disabled this TIMER
    LEDonTimer.disableTIMER();

    LEDoffTimer.enableRestartTIMER();
  }

  //================================================              T I M E R  LEDoff
  //1 second,is the TIMER enabled and is it time to turn ON the next LED ?
  if (LEDoffTimer.checkTIMER() == EXPIRED)
  {
    //no punch ?
    if (elapsedMilliseconds >= LEDonTimer.Interval + LEDoffTimer.Interval - 100)
    {
      Serial.println("There was no punch");
    }

    stopWatchTimer.disableTIMER();

    //disabled this TIMER
    LEDoffTimer.disableTIMER();
  }

  //================================================              Next random LED
  //has LED timing stopped ?
  if (LEDonTimer.TimerFlag == DISABLED && LEDoffTimer.TimerFlag == DISABLED)
  {
    randomly();
  }

  //================================================
  //other non blocking code goes here
  //================================================

} //END of   loop()


//                                    r a n d o m l y ( )
//============================================^================================================
void randomly()
{
  //select a LED connected to pin 3,4 or 5
  LEDselected = random(3, 6);

  //LED goes ON
  digitalWrite(LEDselected, LEDon);

  LEDonTimer.enableRestartTIMER();

  //reset our counter and restart the sopWatch TIMER
  elapsedMilliseconds = 0;
  stopWatchTimer.enableRestartTIMER();

} //END of   randomly()


//                                c h e c k M a c h i n e ( )
//============================================^================================================
void checkMachine()
{
  //================================================
  //service the current "state"
  switch (beepState)
  {
    //=====================
    case STARTUP:
      {
        //do startup stuff
      }
      break;

    //=====================
    case BEEP1:
      {
        //is the common TIMER expired ?
        if (commonTimer.checkTIMER() == EXPIRED)
        {
          tone(speakerPin, frequency0, beepDuration);
          commonTimer.enableRestartTIMER();
          beepState = BEEP2;
        }
      }
      break;

    //=====================
    case BEEP2:
      {
        //is the common TIMER expired ?
        if (commonTimer.checkTIMER() == EXPIRED)
        {
          //make next beep
          tone(speakerPin, frequency0, beepDuration * 2);
          commonTimer.enableRestartTIMER();
          beepState = BEEP3;
        }
      }
      break;

    //=====================
    case BEEP3:
      {
        beepState = STARTUP;
      }
      break;

    //=====================
    case FINISHED:
      {
      }
      break;

  } //END of  switch/case

} //END of   checkMachine()


//                              c h e c k S w i t c h e s ( )
//============================================^================================================
void checkSwitches()
{
  byte state;

  //================================================              button
  state = digitalRead(button);

  //was there a change in the switches state ?
  if (lastButtonSwitch != state)
  {
    //update to this new state
    lastButtonSwitch = state;

    //========================
    if (state == PUSHED)       //+5V---[Internal 50k]---PIN---[switch]---GND
    {
      if (LEDonTimer.TimerFlag == ENABLED && stopWatchTimer.TimerFlag == ENABLED)
      {
        stopWatchTimer.disableTIMER();
        
        //Start beep sequence
        frequency0 = frequency1;
        tone(speakerPin, frequency0, beepDuration);
        commonTimer.enableRestartTIMER();
        beepState = BEEP1;

        Serial.print("\nYou are Fast, only ");
        Serial.print(elapsedMilliseconds);
        Serial.println("ms");
      }

      else if (LEDoffTimer.TimerFlag == ENABLED && stopWatchTimer.TimerFlag == ENABLED)
      {
        stopWatchTimer.disableTIMER();
        
        //Start beep sequence
        frequency0 = frequency2;
        tone(speakerPin, frequency0, beepDuration);
        commonTimer.enableRestartTIMER();
        beepState = BEEP1;

        Serial.print("\nYou are Sloooow ");
        Serial.print(elapsedMilliseconds);
        Serial.println("ms");
      }
    }
  }

} //END of   checkSwitches()

well you're right, i should make my own version

so i decided to figure out by my own, it is more simplified i think, heres how it works, i noticed that i can check the status of a button, every time the LEDs change to other, i check did a button was pressed or not, if yes, i pass, if no, i just simply say that there was no punch.

//************************************************
#define EXPIRED                    true
#define stillTIMING                false

#define LEDon                      HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff                     LOW

#define PUSHED                     LOW    //+5V---[Internal 50k]---PIN---[switch]---GND
#define RELEASED                   HIGH

#define CLOSED                     LOW    //+5V---[Internal 50k]---PIN---[switch]---GND 
#define OPEN                       HIGH

#define ENABLED                    true
#define DISABLED                   false

#define YES                        true
#define NO                         false

unsigned long stopWatch;
unsigned long timerStart;

//************************************************
const byte LEDpin1               = 3;
const byte LEDpin2               = 4;
const byte LEDpin3               = 5;
const byte heartbeatLED          = 13;
const byte button                = 8;

int statusOn = 0;
int statusOff = 0;

byte lastIncSwitch               = RELEASED;

byte lastOn                        = DISABLED;

int statusButton = 0;


byte randNumber;
byte LEDselected;

//                          m i l l i s ( )   B a s e d   T I M E R S
//********************************************^************************************************
//TIMER definitions
//Two ways to define a new TIMER
//The first, we will call it a manual method:        example: heartbeat TIMER
//The second, we will call it the structure method:  example: scanSwitches TIMER

//****************************************
//manual method:
//heartbeat TIMER
unsigned long heartbeatTime      = 0;
unsigned long heartbeatInterval  = 500ul;
bool heartbeatTimerFlag          = ENABLED;
bool heartbeatRestart            = YES;

//****************************************
//structure method:
struct makeTIMER
{
//#define ENABLED       true
//#define DISABLED      false

//#define YES           true
//#define NO            false

//#define EXPIRED       true
//#define stillTIMING   false

  unsigned long Time;           //when the TIMER started
  unsigned long Interval;       //delay time in ms which we are looking for
  bool          timerFlag;      //is the TIMER enabled ? ENABLED/DISABLED
  bool          Restart;        //restart this TIMER ? YES/NO

  //****************************************
  //fuction to check if the TIMER is enabled and check if the TIMER has expired
  bool checkTIMER()
  {
    //*********************
    //is this TIMER enabled and has this TIMER expired ?
    if (timerFlag == ENABLED && millis() - Time >= Interval)
    {
      //*********************
      //should this TIMER restart again?
      if (Restart == YES)
      {
        //restart this TIMER
        Time = millis();
      }

      //this TIMER is enabled and has expired
      return EXPIRED;
    }

    //this TIMER is disabled and/or has not expired
    return stillTIMING;

  } //END of   checkTime()

}; //END of   struct makeTIMER

//********************************************^************************************************
/*example
  // *******************
  makeTIMER toggleLED =
  {
  0, 500ul, ENABLED/DISABLED, YES/NO  //Time, Interval, timerFlag, Restart
  };
*/


makeTIMER checkSwitchesTimer =
{
  0, 50ul, ENABLED, YES      //Time, Interval, timerFlag, Restart
};

//*******************
makeTIMER LEDonTimer =
{
  0, 1000ul, DISABLED, NO      //Time, Interval, timerFlag, Restart
};
//*******************
makeTIMER LEDoffTimer =
{
  0, 1000ul, DISABLED, NO      //Time, Interval, timerFlag, Restart
};
//*******************


//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(LEDpin1, OUTPUT);
  pinMode(LEDpin2, OUTPUT);
  pinMode(LEDpin3, OUTPUT);

  pinMode(button, INPUT);

  randomSeed(analogRead(A0));

} //END of   setup()


//                                        l o o p ( )
//********************************************^************************************************
void loop()
{

  if (checkSwitchesTimer.checkTIMER() == EXPIRED)
  {
    checkSwitches();
  }

  checkSlowTime();

  //************************************************              T I M E R  heartbeatLED
  //when enabled, is it time to toggle the heartbeat LED ?
  if (heartbeatTimerFlag == ENABLED && millis() - heartbeatTime >= heartbeatInterval)
  {
    //restart this TIMER ?
    if (heartbeatRestart == YES)
    {
      heartbeatTime = millis();
    }

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //************************************************              T I M E R  LEDon
  //is the TIMER enabled and is it time to turn OFF this LED ?
  if (LEDonTimer.checkTIMER() == EXPIRED)
  {
    //LED selected goes OFF
    digitalWrite(LEDselected, LEDoff);

    //disabled this TIMER
    LEDonTimer.timerFlag = DISABLED;

    //enable the OFF TIMER
    LEDoffTimer.timerFlag = ENABLED;

    //restart the OFF TIMER
    LEDoffTimer.Time = millis();    
  }

  //************************************************              T I M E R  LEDoff
  //is the TIMER enabled and is it time to turn ON the next LED ?
  if (LEDoffTimer.checkTIMER() == EXPIRED)
  {
    //disabled this TIMER
    LEDoffTimer.timerFlag = DISABLED;
  }

  //************************************************              Next random LED
  //has LED timing stopped ?
  if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == DISABLED)
  {
    randomly();
  }

  if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == ENABLED){
    statusOn = 0;
  }

  if (LEDonTimer.timerFlag == ENABLED && LEDoffTimer.timerFlag == DISABLED){
    statusOff = 0;
  }

  //************************************************
  //other non blocking code goes here
  //************************************************

} //END of   loop()


//                                    r a n d o m l y ( )
//********************************************^************************************************
void randomly()
{
  //select a LED connected to pin 2,3 or 4
  LEDselected = random(3,6);

  //LED goes ON
  digitalWrite(LEDselected, LEDon);

  //enable the LEDon TIMER
  LEDonTimer.timerFlag = ENABLED;

  //restart this TIMER
  LEDonTimer.Time = millis();

  if(statusButton == 0){
    Serial.println("No punch");
  }
  else{
    statusButton = 0;
  }
} //END of   randomly()

void checkSwitches()
{
  int p = 0;
  byte state;
  state = digitalRead(button);

  if (lastIncSwitch != state)
  {
    lastIncSwitch = state;

    //*************************************
    if (state == PUSHED)
    {
      statusButton += 1;
      if (LEDonTimer.timerFlag == ENABLED && LEDoffTimer.timerFlag == DISABLED){
        
        statusOn++;
        if(statusOn == 1){
          Serial.println("Fast");
        }
        if(statusOn > 1){
          p++;
        }
      }

      if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == ENABLED){
        stopWatch = millis() - timerStart;
        statusOff++;
        int p = 0;
        if(statusOff == 1){
          Serial.print("Slow: ");
          Serial.println(stopWatch);
        }
        if(statusOff > 1){
          p++;
        }
      }
    }
  } //KẾT THÚC NÚT NÀY
} //KẾT THÚC   checkSwitches()


void checkSlowTime(){
  byte status;
  status = LEDoffTimer.timerFlag;

  if(lastOn != status){
    lastOn = status;
    if(status == ENABLED){
      timerStart = millis();
    }
  }
}
  //*************************************

can i ask you this, well i dont really understand your buzzer code so i wanted to make it simple, then i can understand your code and use it fluently, just like the time that you explained how a timer works. Lets try to use one LED, every time it turns on, buzzer will beep, then when it turns off, buzzer will stop beeping. Thanks!

That’s commendable. :+1:


  • Does the sketch in post #230 work ?

Note:

  • It is okay use others code, but you must master the techniques so you can use those techniques in future projects.
  • These techniques make your sketches easy to follow, makes the code easy to modify, run without code blocking, and keeps things organized into small manageable code blocks.
  • If you get an opportunity to ask questions to the author of code you don’t understand, take that opportunity to get your questions answered.

Some insight on the code in offered in post #230

Always comment your code blocks so you and others can understand what is being done; especially 6 months into the future.


Let’s look at making the speaker make the beep sequence you requested, make two beeps then a third beep of longer duration.

A Beep State Machine is used to make the beep sequence.

  • Criteria, we never want to have our code pause in its execution, we therefore use a non blocking TIMER.

  • There are basically 4 states to make your beep sequence,

  1. Do Nothing,
  2. making the first beep,
  3. making the second beep,
  4. make the third beep (twice as long). Then back to Doing nothing.
  • A TIMER is used to go from one state to the next, this TIMER is called commonTimer
    When the TIMER expires, we can do what’s needed, restart this same TIMER then proceed to the next state in the Beep State Machine.

In the sketch offered, two frequencies are used to alert the user what just happened.

  • A HIGH frequency sequence for a fast punch.
  • A LOWER frequency sequence for a slow punch.
    The same Beep State Machine is used in both cases.

Back in loop( ) we have a TIMER that checks our Beep State Machine every 1ms.

  //================================================              T I M E R  machine
  //every 1ms, is it time to service our State Machine ?
  if (machineTIMER.checkTIMER() == EXPIRED)
  {
    checkMachine();
  } 

When this TIMER expires, we service the State Machine to see if the commonTimer there has expired.
As mention, when the commonTimer expires, we do something then advance to the next machine state.



  • Don’t strive for more simplified when you write code.
    Doing so often makes things difficult to modify, doesn’t take other code blocks into consideration, and can leave hidden bugs free to raise their little heads later in the sketch development. :wink:
2 Likes

well i would love to learn these new techniques because i can level up my coding skills

but i don't really have much time because there's still lots of other things that i have to do with my project, second is i don't really have much time, on December 15th is probably the day that i will participate the province competition. One cool thing is my school has only one project to participate this competition and that's our project, well there's 2 people doing this which are me and my friend

the code works perfectly!

well do you know how to code a count down? like beep, beep, beeep, it works like the code you gave me right? or it's completely different, while i wait you to reply, i think i should take a look and try my best to figure out!

well never mind, now i just need a count down from 3 to 1, the way it beeps is completely the same with the code in here, last time i tried to make a countdown but it's kinda bad so do you have any ideas?

can you explain to me why we have to do nothing at the beginning and at the end?

One does nothing at the beginning/end of state machines so they get out of the way of running other code. It is an off-switch.

2 Likes

well i have a question right here, im trying to make a start button using one of 3 buttons that are use for testing are you fast or not, so heres my idea.
First, if you want to start testing, you need to punch to the middle, in my code, this button is button2.
Second, after you punched it, it will delay for 2 seconds, then start the testing program.
Finally, if you stop punching, (which is after 3 times "No punch" in the code), it will stop and go back to the first state which is waiting for you to punch the start button.
Here's my attempt, it didnt work well so can you take a look at it? Thanks
Oh and the method to do this is based on your 3 beeping buzzer

//************************************************
#define EXPIRED                    true
#define stillTIMING                false

#define LEDon                      HIGH   //PIN---[220R]---A[LED]K---GND
#define LEDoff                     LOW

#define PUSHED                     LOW    //+5V---[Internal 50k]---PIN---[switch]---GND
#define RELEASED                   HIGH

#define CLOSED                     LOW    //+5V---[Internal 50k]---PIN---[switch]---GND 
#define OPEN                       HIGH

#define ENABLED                    true
#define DISABLED                   false

#define YES                        true
#define NO                         false

#define MILLIS             1
#define MICROS             1000  //we can use this value to divide into a variable to get milliseconds

unsigned long stopWatch;
unsigned long timerStart;

//************************************************
const byte LEDpin1               = 4;
const byte LEDpin2               = 5;
const byte LEDpin3               = 6;
const byte LEDpin4               = 7;
const byte LEDpin5               = 8;
const byte LEDpin6               = 9;
const byte LEDpin7               = 10;
const byte LEDpin8               = 11;
const byte LEDpin9               = 12;
const byte LEDpin10              = 13;
const byte heartbeatLED          = 14;
const byte button1                = 41;
const byte button2                = 43;
const byte button3                = 45;
const byte button4                = 47;
const byte button5                = 49;


int statusOn = 0;
int statusOff = 0;

int noPunchCounter = 0;

byte lastIncSwitch1               = RELEASED;
byte lastIncSwitch2               = RELEASED;
byte lastIncSwitch3               = RELEASED;
byte lastIncSwitch4               = RELEASED;
byte lastIncSwitch5               = RELEASED;
byte lastStartSwitch              = RELEASED;

byte startButtonStage             = RELEASED;


byte lastOn                        = DISABLED;

int statusButton = 0;

int statusLED = 0;


byte randNumber;
byte LEDselected;

//                          m i l l i s ( )   B a s e d   T I M E R S
//********************************************^************************************************
//TIMER definitions
//Two ways to define a new TIMER
//The first, we will call it a manual method:        example: heartbeat TIMER
//The second, we will call it the structure method:  example: scanSwitches TIMER

//****************************************
//manual method:
//heartbeat TIMER

unsigned long heartbeatTime      = 0;
unsigned long heartbeatInterval  = 500ul;
bool heartbeatTimerFlag          = ENABLED;
bool heartbeatRestart            = YES;

//****************************************
//structure method:
struct makeTIMER
{
//#define ENABLED       true
//#define DISABLED      false

//#define YES           true
//#define NO            false

//#define EXPIRED       true
//#define stillTIMING   false
  int           TimerType;      //what kind of TIMER is this? MILLIS/MICROS
  unsigned long Time;           //when the TIMER started
  unsigned long Interval;       //delay time in ms which we are looking for
  bool          timerFlag;      //is the TIMER enabled ? ENABLED/DISABLED
  bool          Restart;        //restart this TIMER ? YES/NO

  //****************************************
  //fuction to check if the TIMER is enabled and check if the TIMER has expired
  bool checkTIMER()
  {
    //*********************
    //is this TIMER enabled and has this TIMER expired ?
    if (timerFlag == ENABLED && millis() - Time >= Interval)
    {
      //*********************
      //should this TIMER restart again?
      if (Restart == YES)
      {
        //restart this TIMER
        Time = millis();
      }

      //this TIMER is enabled and has expired
      return EXPIRED;
    }

    //this TIMER is disabled and/or has not expired
    return stillTIMING;

  } //END of   checkTime()

}; //END of   struct makeTIMER

//********************************************^************************************************
/*example
  // *******************
  makeTIMER toggleLED =
  {
  0, 500ul, ENABLED/DISABLED, YES/NO  //Time, Interval, timerFlag, Restart
  };
*/

makeTIMER checkStartSwitchesTimer =
{
  MILLIS, 0, 50ul, ENABLED, YES      //Time, Interval, timerFlag, Restart
};


makeTIMER checkSwitchesTimer =
{
  MILLIS, 0, 50ul, ENABLED, YES      //Time, Interval, timerFlag, Restart
};

//*******************
makeTIMER LEDonTimer =
{
  MILLIS, 0, 1000ul, DISABLED, NO      //Time, Interval, timerFlag, Restart
};
//*******************
makeTIMER LEDoffTimer =
{
  MILLIS, 0, 1000ul, DISABLED, NO      //Time, Interval, timerFlag, Restart
};

makeTIMER commonTimer =
{
  MILLIS, 0, 2000ul, ENABLED, NO     //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};

makeTIMER machineTIMER =
{
  MICROS, 0, 1000ul, ENABLED, YES   //.TimerType, .Time, .Interval, .TimerFlag, .Restart
};
//*******************

//                           S T A R T   U P   S t a t e   M a c h i n e
//============================================^================================================
//the states in our machine
enum STATES : byte
{
  STARTUP, STAGE1, STAGE2, STAGE3, FINISHED
};
STATES StageState = STARTUP;


//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(LEDpin1, OUTPUT);
  pinMode(LEDpin2, OUTPUT);
  pinMode(LEDpin3, OUTPUT);
  pinMode(LEDpin4, OUTPUT);
  pinMode(LEDpin5, OUTPUT);
  pinMode(LEDpin6, OUTPUT);
  pinMode(LEDpin7, OUTPUT);
  pinMode(LEDpin8, OUTPUT);
  pinMode(LEDpin9, OUTPUT);
  pinMode(LEDpin10, OUTPUT);

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(button5, INPUT);

  randomSeed(analogRead(A0));

} //END of   setup()


//                                        l o o p ( )
//********************************************^************************************************
void loop()
{
  if (machineTIMER.checkTIMER() == EXPIRED)
  {
    checkMachine();
  }

  if(startButtonStage == RELEASED){
    if (checkStartSwitchesTimer.checkTIMER() == EXPIRED)
    {
      checkStartSwitches();
    }
  }
  if(startButtonStage == PUSHED){
    //do nothing
  }

  checkNoPunch();


  //************************************************              T I M E R  heartbeatLED
  //when enabled, is it time to toggle the heartbeat LED ?
  if (heartbeatTimerFlag == ENABLED && millis() - heartbeatTime >= heartbeatInterval)
  {
    //restart this TIMER ?
    if (heartbeatRestart == YES)
    {
      heartbeatTime = millis();
    }

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }



} //END of   loop()


void checkSense()
{
  if (checkSwitchesTimer.checkTIMER() == EXPIRED)
  {
    checkSwitches();
  }

  checkSlowTime();

  //************************************************              T I M E R  LEDon
  //is the TIMER enabled and is it time to turn OFF this LED ?
  if (LEDonTimer.checkTIMER() == EXPIRED)
  {
    //LED selected goes OFF
    digitalWrite(LEDselected, LEDoff);

    //disabled this TIMER
    LEDonTimer.timerFlag = DISABLED;

    //enable the OFF TIMER
    LEDoffTimer.timerFlag = ENABLED;

    //restart the OFF TIMER
    LEDoffTimer.Time = millis();    
  }

  //************************************************              T I M E R  LEDoff
  //is the TIMER enabled and is it time to turn ON the next LED ?
  if (LEDoffTimer.checkTIMER() == EXPIRED)
  {
    //disabled this TIMER
    LEDoffTimer.timerFlag = DISABLED;
  }

  //************************************************              Next random LED
  //has LED timing stopped ?
  if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == DISABLED)
  {
    randomly();
  }

  if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == ENABLED){
    statusOn = 0;
  }

  if (LEDonTimer.timerFlag == ENABLED && LEDoffTimer.timerFlag == DISABLED){
    statusOff = 0;
  }

  //************************************************
  //other non blocking code goes here
  //************************************************

}

//                                c h e c k M a c h i n e ( )
//============================================^================================================
void checkMachine()
{
  //================================================
  //service the current "state"
  switch (StageState)
  {
    //=====================
    case STARTUP:
      {
        //do startup stuff
      }
      break;

    //=====================
    case STAGE1:
      {
        //is the common TIMER expired ?
        if (commonTimer.checkTIMER() == EXPIRED)
        {
          commonTimer.Time = millis();
          StageState = STAGE2;
        }
      }
      break;

    //=====================
    case STAGE2:
      {
        checkSense();
        startButtonStage = PUSHED;
      }
      break;

    //=====================
    case STAGE3:
      {
        StageState = STARTUP;
      }
      break;

    //=====================
    case FINISHED:
      {
        //do nothing
      }
      break;

  } //END of  switch/case

} //END of   checkMachine()




//                                    r a n d o m l y ( )
//********************************************^************************************************
void randomly()
{
  //select a LED connected to pin 2,3 or 4
  LEDselected = random(LEDpin1, LEDpin10 + 1);

  //LED goes ON
  digitalWrite(LEDselected, LEDon);

  //enable the LEDon TIMER
  LEDonTimer.timerFlag = ENABLED;

  //restart this TIMER
  LEDonTimer.Time = millis();

  if(statusButton == 0){
    Serial.println("No punch");
    noPunchCounter += 1;
  }
  else{
    statusButton = 0;
  }

  int ledNumber = LEDselected - 3;
  if(ledNumber == 1 || ledNumber == 2){
    statusLED = 1;
  }
  if(ledNumber == 3 || ledNumber == 4){
    statusLED = 2;
  }
  if(ledNumber == 5 || ledNumber == 6){
    statusLED = 3;
  }
  if(ledNumber == 7 || ledNumber == 8){
    statusLED = 4;
  }
  if(ledNumber == 9 || ledNumber == 10){
    statusLED = 5;
  }
} //END of   randomly()

void checkSwitches()
{
  switch(statusLED)
  {
    case 1:
      checkButton1();
      break;
    case 2:
      checkButton2();
      break;
    case 3:
      checkButton3();
      break;
    case 4:
      checkButton4();
      break;
    case 5:
      checkButton5();
      break;
  }
} //KẾT THÚC   checkSwitches()


void checkSlowTime(){
  byte status;
  status = LEDoffTimer.timerFlag;

  if(lastOn != status){
    lastOn = status;
    if(status == ENABLED){
      timerStart = millis();
    }
  }
}
//*************************************

void checkButton1(){
  int p = 0;
  byte state;
  state = digitalRead(button1);

  if (lastIncSwitch1 != state)
  {
    lastIncSwitch1 = state;

    //*************************************
    if (state == PUSHED)
    {
      statusButton += 1;
      if (LEDonTimer.timerFlag == ENABLED && LEDoffTimer.timerFlag == DISABLED){
        
        statusOn++;
        if(statusOn == 1){
          Serial.println("Fast");
        }
        if(statusOn > 1){
          p++;
        }
      }

      if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == ENABLED){
        stopWatch = millis() - timerStart;
        statusOff++;
        int p = 0;
        if(statusOff == 1){
          Serial.print("Slow: ");
          Serial.println(stopWatch);
        }
        if(statusOff > 1){
          p++;
        }
      }
    }
  } //KẾT THÚC NÚT NÀY
}

void checkButton2(){
  int p = 0;
  byte state;

  state = digitalRead(button2);

  if (lastIncSwitch2 != state)
  {
    lastIncSwitch2 = state;

    //*************************************
    if (state == PUSHED)
    {
      statusButton += 1;
      if (LEDonTimer.timerFlag == ENABLED && LEDoffTimer.timerFlag == DISABLED){
        
        statusOn++;
        if(statusOn == 1){
          Serial.println("Fast");
        }
        if(statusOn > 1){
          p++;
        }
      }

      if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == ENABLED){
        stopWatch = millis() - timerStart;
        statusOff++;
        int p = 0;
        if(statusOff == 1){
          Serial.print("Slow: ");
          Serial.println(stopWatch);
        }
        if(statusOff > 1){
          p++;
        }
      }
    }
  } //KẾT THÚC NÚT NÀY
}

void checkButton3(){
  int p = 0;
  byte state;

  state = digitalRead(button3);

  if (lastIncSwitch3 != state)
  {
    lastIncSwitch3 = state;

    //*************************************
    if (state == PUSHED)
    {
      statusButton += 1;
      if (LEDonTimer.timerFlag == ENABLED && LEDoffTimer.timerFlag == DISABLED){
        
        statusOn++;
        if(statusOn == 1){
          Serial.println("Fast");
        }
        if(statusOn > 1){
          p++;
        }
      }

      if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == ENABLED){
        stopWatch = millis() - timerStart;
        statusOff++;
        int p = 0;
        if(statusOff == 1){
          Serial.print("Slow: ");
          Serial.println(stopWatch);
        }
        if(statusOff > 1){
          p++;
        }
      }
    }
  } //KẾT THÚC NÚT NÀY
}

void checkStartSwitches()
{
  byte state;
  state = digitalRead(button1);

  if (lastStartSwitch != state)
  {
    lastStartSwitch = state;

    //*************************************
    if (state == PUSHED)
    {
      StageState = STAGE1;
    }
  } //KẾT THÚC NÚT NÀY
}

void checkButton4(){
  int p = 0;
  byte state;

  state = digitalRead(button4);

  if (lastIncSwitch4 != state)
  {
    lastIncSwitch4 = state;

    //*************************************
    if (state == PUSHED)
    {
      statusButton += 1;
      if (LEDonTimer.timerFlag == ENABLED && LEDoffTimer.timerFlag == DISABLED){
        
        statusOn++;
        if(statusOn == 1){
          Serial.println("Fast");
        }
        if(statusOn > 1){
          p++;
        }
      }

      if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == ENABLED){
        stopWatch = millis() - timerStart;
        statusOff++;
        int p = 0;
        if(statusOff == 1){
          Serial.print("Slow: ");
          Serial.println(stopWatch);
        }
        if(statusOff > 1){
          p++;
        }
      }
    }
  } //KẾT THÚC NÚT NÀY
}

void checkButton5(){
  int p = 0;
  byte state;

  state = digitalRead(button5);

  if (lastIncSwitch5 != state)
  {
    lastIncSwitch5 = state;

    //*************************************
    if (state == PUSHED)
    {
      statusButton += 1;
      if (LEDonTimer.timerFlag == ENABLED && LEDoffTimer.timerFlag == DISABLED){
        
        statusOn++;
        if(statusOn == 1){
          Serial.println("Fast");
        }
        if(statusOn > 1){
          p++;
        }
      }

      if (LEDonTimer.timerFlag == DISABLED && LEDoffTimer.timerFlag == ENABLED){
        stopWatch = millis() - timerStart;
        statusOff++;
        int p = 0;
        if(statusOff == 1){
          Serial.print("Slow: ");
          Serial.println(stopWatch);
        }
        if(statusOff > 1){
          p++;
        }
      }
    }
  } //KẾT THÚC NÚT NÀY
}

void checkNoPunch()
{
  if(noPunchCounter == 3){
    StageState = STAGE3;
  }
}

can you check it for me? it seems like at the start, i havent punched anything, but some how it printed out this everytime i turn on the serial monitor
image

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.