Hey everybody,
First of all merry Christmas and happy new year
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
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.
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.
#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()
#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()
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;
}