Recording button state with how much time it has taken to be in that state

Hey everybody,
First of all merry Christmas and happy new year :slight_smile:

I need a code to record the state of a momentary switch if it is high or low and also record how much time was it on a high state and low state.

For example i need this to be shown in my serial monitor:

button is on for 4000ms
button is off for 20000ms
button is on for 3000ms
button is off for 100000ms
button is on for 40000ms
button is off for 100ms
button is on for 3670ms

you get it ,but how shall i write the code for it?
thanks in advance

I need a code to record the state of a momentary switch if it is high or low and also record how much time was it on a high state and low state.

Why do you need to do this?

So i can later take the readings of the serial monitor then covert them to code that i can control an led with later

So you want to record the values with a serial monitor type of program, then send the same messages back to the Arduino and have a LED respond to the messages.

What serial program are you going to use?

i will not have the values go directly to the led i will have it in the serial monitor then i will copy it and then edit and translate it into code. I will use any serial monitor maybe arduino's builtin one or maybe something like coolterm.

See:
http://gammon.com.au/Arduino/SwitchManager.zip

Try this:

#include <SwitchManager.h>
SwitchManager modeSW;  // create the object

const byte ModeSW = 2;              //input from the Mode push button switch     

void setup ()
{
  Serial.begin(9600);  
  modeSW.begin (ModeSW, modeSwitchManager);
}
//======================== END OF setup ==========================

void loop (){
  //check to see what's happening with the Mode switch
  modeSW.check();

  // put other loop() code here



}
//======================== END OF loop ===========================



//****************************************************************
//*                          FUNCTIONS                           
//****************************************************************

//                M O D E   S W I T C H   M A N A G E R
//****************************************************************
// function looks after the Mode switch functions
void modeSwitchManager (const byte newState, const unsigned long interval)
{
  if (newState == HIGH )
  {
    // N E W S A T E   I S   H I G H
    Serial.print ( "the switch was LOW for: ");
    Serial.print (interval);
    Serial.println( " milli seconds");

    return;

  }

  // N E W S A T E   I S   L O W
  Serial.print ( "the switch was HIGH for:");
  Serial.print (interval);
  Serial.println( " milli seconds");

  return;



}  //                  END of modeSwitchManager()

My state machine library has built in timing you can use:

#include <SM.h>

SM btnTimer(off);
const byte btn = 12;

void setup(){
    Serial.begin(115200);
}//setup()

void loop(){
  EXEC(btnTimer);
}//loop()

State off(){
  if(digitalRead(btn)){
    Serial.print("Button has been off for:");
    Serial.println(btnTimer.Statetime());
    btnTimer.Set(on);
  }//if(btn)
}//off()

State on(){
  if(!digitalRead(btn)){
    Serial.print("Button has been on for:");
    Serial.println(btnTimer.Statetime());
    btnTimer.Set(off);
  }//if(!btn)
}//on()

How about keeping it really simple ?

const byte buttonPin = 8;
unsigned long waitStart;
byte buttonState = HIGH;
byte previousButtonState = HIGH;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  waitStart = millis();
}

void loop()
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != previousButtonState)
  {
    Serial.print(buttonState == HIGH ? "Released after " : "Pressed after ");
    Serial.println(millis() - waitStart);
    waitStart = millis();
  }
  previousButtonState = buttonState;
}