0
int pin_state = LOW;    
struct Channel {  
    byte pin;  
    byte state;  
    byte count;  
    unsigned long fell, rose;  // times of fall and rise  
};  

const int CHANNEL_COUNT = 3;  
Channel channels[CHANNEL_COUNT] = {  
    { 2, LOW, 0, 0 },  
    { 3, LOW, 0, 0 },  
    //{ 4, LOW, 0, 0 },  
    //{ 5, LOW, 0, 0 },
    //{ 6, LOW, 0, 0 }
};

void setup()
{
    for (int i = 2; i <= CHANNEL_COUNT; ++i) {
        pinMode(channels[i].pin, INPUT);
    }
    Serial.begin(9600);
    //Serial.println("pin,fell,rose,low time");
    Serial.println("Minutes, Seconds, Count");
}

void loop()
{
    for (int i = 0; i < CHANNEL_COUNT; ++i) 
    {
        Channel &c = channels[i];
        byte new_state = digitalRead(c.pin);
        if (c.state == LOW && new_state == HIGH)   // input rose 
        {  
            c.rose = millis();
            c.count = c.count + 1;
        }
        else if (c.state == HIGH && new_state == LOW)  // input fell
        {  
            c.fell = millis();
            display_channel(c);
        }
        c.state = new_state;
    }
}

void display_channel(const Channel &c)
{
  int minutes, seconds;
  unsigned long difference;
  difference = c.fell - c.rose;
  seconds = int(difference/1000);
  if (seconds >= 60)
  {
       minutes = seconds / 60;
       seconds = seconds % 60;
  }
    //Serial.print(difference/1000);
    //Serial.print(",");
    Serial.print(minutes);
    Serial.print(",");
    Serial.print(seconds/1000);
    Serial.print(",");
    Serial.println(c.count-1);
    //Serial.print(",");
  }

In this code, I tried to measure the time taken between the two events (when the ON signal comes and when the signal goes OFF) in two different channels simultaneously. But the code doesn't give any output and the serial monitor is empty except for the statement in the main function.

It would be useful if anyone could find where exact;y the problem is and a valid solution to it would be helpful.

edit : I made the following changes but still output is never displayed. Any error is still ON the program?

2

You have the same state being tested twice:

if (c.state == LOW && new_state == HIGH)   // input rose 

and:

else if (c.state == LOW && new_state == HIGH)  // input fell

If "A", else if "A". It makes no sense. I think you wanted the second one to be the opposite:

else if (c.state == HIGH && new_state == LOW)  // input fell

Also, this calculation in display_channel doesn't work:

if (seconds >= 60)
{
  seconds = seconds % 60;
  minutes = minutes + 1;
}

For example, if seconds is 378, then seconds ends up as 18, and minutes as some undefined number plus 1.

That whole block should be replaced with:

minutes = seconds / 60;
seconds = seconds % 60;

In your code:

  • minutes starts off undefined
  • You add only 1 to minutes regardless of how many minutes have passed.
  • I have made the following changes as you suggested, but still, output doesnt gets displayed, Is there anything else left? – Rajesh Anand10 Jan 2 at 12:54

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.