I am currently working on a project which is read a digital input signal from a machine. The machine operation is as below:

when the mold close, the machine gives the "HIGH" signal, when mold is open the machine gives "LOW" signal. A cycle is counted as one cycle when from the mold close --> open --> close.

My goal is to count the cycle and cycle time of each cycle. I am struggling when the Input comes from HIGH to LOW. Appreciate your help to give me some guildance on this project.

int MoldInput = 2;
int MoldState = 0;
int lastMoldState = 0;
int CycleTimeCounter = 0;
int CycleCount = 0;
unsigned long CycleTime;
volatile unsigned long TimeTemp1 = 0;
volatile unsigned long TimeTemp2 = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode (MoldInput,INPUT);
  attachInterrupt(0, RealTimeMoldClose, RISING);

void loop() {
  // put your main code here, to run repeatedly:
void RealTimeMoldClose(){
  unsigned long t = millis();
  TimeTemp2 = t;
  CycleTime = TimeTemp2 - TimeTemp1;
  TimeTemp1 = TimeTemp2;
  • What is connected to pin 5 and what is connected to pin 2? For the cycle time store the millis value and the next time the MoldState turns HIGH, substract the previous millis value from the current millis value.
    – Jot
    Apr 11 '19 at 7:08
  • This is my mistake. Only digital signal connected to pin 2. Apr 11 '19 at 8:01
  • Not sure, but pulseIn seems to be appropriate Apr 11 '19 at 9:41
  • upvote for presenting properly formatted code listing
    – jsotola
    Apr 11 '19 at 17:50
  • 1
    I have updated a new code from myself after remember the attachInterrupt function. Appreciate your guys comment on this. Apr 12 '19 at 4:20

You don't need to do anything on HIGH to LOW. For example, if you are counting how often a door opens you don't also need to count how often it closes, because it must close before it re-opens.

I don't see the point of the interrupt since your code is doing nothing else than checking if the interrupt occurred, so it may as well be checking if the input has changed anyway. This code is simpler:

const int MoldInput = 2;

int lastMoldState = 0;
unsigned long startTime = 0;
unsigned long TotalTime = 0;
unsigned long CycleTimeCounter = 0;

void setup() {
  pinMode (MoldInput, INPUT);

unsigned long lastReportedCycleCount = 0;

void loop() {
  int MoldState = digitalRead(MoldInput);
  if (MoldState != lastMoldState) {
    if (MoldState == HIGH) {
      CycleTimeCounter++;  // count times it was high
      TotalTime += millis() - startTime;  // add up time interval
      startTime = millis ();
      delay (100);    // debounce
    }  // end of state being HIGH
    lastMoldState = MoldState;
  }   // end of state change

  if (CycleTimeCounter % 10 == 0 && CycleTimeCounter != lastReportedCycleCount)
    Serial.print (F("Average time after "));
    Serial.print (CycleTimeCounter);
    Serial.print (F(" cycles is "));
    Serial.print (TotalTime / CycleTimeCounter);
    Serial.println (F(" milliseconds"));
    lastReportedCycleCount = CycleTimeCounter;
} // end of loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.