user52085: int pin_state = LOW;    
struct Channel {  
    byte pin;  
    byte state;  
    byte count;  
    unsigned long fell, rose;    
};  

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)    
        {  
            c.rose = millis();
            c.count = c.count + 1;
        }
        else if (c.state == HIGH && new_state == LOW)  
        {  
            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(",");
  }

When I try to run the program, I don't get any output.
Is there anything wrong with the code, that must be solved before any output can get displayed?

user4143: Here's the catalog of errors I see at first glance:
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 }
};

You set up space for 3 entries but only fill 2.  Also, the Channel structure has 5 entries, not 4. You don't initialize the 5th (rose) entry (although it will be initialized to 0 for you - best to be explicit though).

for (int i = 2; i <= CHANNEL_COUNT; ++i) {
    pinMode(channels[i].pin, INPUT);
}

You start from entry number 2 (the third entry) and iterate to the 4th (non-existent) entry to set the pin mode of either undefined or invalid pins.

for (int i = 0; i < CHANNEL_COUNT; ++i) 

You have only filled in 2 of the three entries. Yet you use the third as if it were filled in.

Serial.print(minutes);

If seconds is less than 60 then minutes is undefined here.

I would suggest changing your code so that Channel is a class and embed the code to manage that class within it. 


Bonus Time

Here's your program re-written using a class. As an added bonus I have added automatic allocation of a channel number to each object as it's created.  To add more channels just add the pin number into the initialization list.
class Channel {
    private:
        uint8_t _pin;
        uint8_t _state;
        uint32_t _rise;
        uint32_t _fall;
        uint8_t _count;
        uint8_t _channel;

        static uint8_t channelCount;

        void display() {
            uint32_t diff = (_fall - _rise) / 1000;
            uint32_t secs = diff % 60;
            uint32_t mins = diff / 60;
            Serial.print(_channel);
            Serial.print(",");
            Serial.print(mins);
            Serial.print(",");
            Serial.print(secs);
            Serial.print(",");
            Serial.println(_count);
        }

    public:
        Channel(int pin) : _pin(pin), _count(0) {
            _pin = pin;
            _count = 0;
            _channel = channelCount;
            channelCount++;
        }
        void process() {
            int s = digitalRead(_pin);
            if (s != _state) {
                _state = s;
                if (s == LOW) { // Fell
                    _fall = millis();
                    _count++;
                    display();
                } else {
                    _rise = millis();
                }
            }
        }
        void begin() {
            pinMode(_pin, INPUT);
        }
};

uint8_t Channel::channelCount = 0;

Channel channels[] = { 2, 3 };
#define NUM_CHANNELS (sizeof(channels) / sizeof(channels[0]))

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

    Serial.println("Channel,Minutes,Seconds,Count");

    for (unsigned int i = 0; i < NUM_CHANNELS; i++) {
        channels[i].begin();
    }
}

void loop() {
    for (unsigned int i = 0; i < NUM_CHANNELS; i++) {
        channels[i].process();
    }   
}


52085: <pre><code>int pin_state = LOW;    
struct Channel {  
    byte pin;  
    byte state;  
    byte count;  
    unsigned long fell, rose;    
};  

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 &lt;= 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 &lt; CHANNEL_COUNT; ++i) 
    {
        Channel &amp;c = channels[i];
        byte new_state = digitalRead(c.pin);
        if (c.state == LOW &amp;&amp; new_state == HIGH)    
        {  
            c.rose = millis();
            c.count = c.count + 1;
        }
        else if (c.state == HIGH &amp;&amp; new_state == LOW)  
        {  
            c.fell = millis();
            display_channel(c);
        }
        c.state = new_state;
    }
}

void display_channel(const Channel &amp;c)
{
  int minutes, seconds;
  unsigned long difference;
  difference = c.fell - c.rose;
  seconds = int(difference/1000);
  if (seconds &gt;= 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(",");
  }
</code></pre>

<p>When I try to run the program, I don't get any output.
Is there anything wrong with the code, that must be solved before any output can get displayed?</p>
 4143: <p>Here's the catalog of errors I see at first glance:</p>

<pre><code>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 }
};
</code></pre>

<p>You set up space for 3 entries but only fill 2.  Also, the Channel structure has 5 entries, not 4. You don't initialize the 5th (<code>rose</code>) entry (although it will be initialized to 0 for you - best to be explicit though).</p>

<hr>

<pre><code>for (int i = 2; i &lt;= CHANNEL_COUNT; ++i) {
    pinMode(channels[i].pin, INPUT);
}
</code></pre>

<p>You start from entry number 2 (the third entry) and iterate to the 4th (non-existent) entry to set the pin mode of either undefined or invalid pins.</p>

<hr>

<pre><code>for (int i = 0; i &lt; CHANNEL_COUNT; ++i) 
</code></pre>

<p>You have only filled in 2 of the three entries. Yet you use the third as if it were filled in.</p>

<hr>

<pre><code>Serial.print(minutes);
</code></pre>

<p>If <code>seconds</code> is less than 60 then <code>minutes</code> is undefined here.</p>

<hr>

<p>I would suggest changing your code so that <code>Channel</code> is a class and embed the code to manage that class within it. </p>

<hr>

<ul>
<li>Bonus Time</li>
</ul>

<p>Here's your program re-written using a class. As an added bonus I have added automatic allocation of a channel number to each object as it's created.  To add more channels just add the pin number into the initialization list.</p>

<pre><code>class Channel {
    private:
        uint8_t _pin;
        uint8_t _state;
        uint32_t _rise;
        uint32_t _fall;
        uint8_t _count;
        uint8_t _channel;

        static uint8_t channelCount;

        void display() {
            uint32_t diff = (_fall - _rise) / 1000;
            uint32_t secs = diff % 60;
            uint32_t mins = diff / 60;
            Serial.print(_channel);
            Serial.print(",");
            Serial.print(mins);
            Serial.print(",");
            Serial.print(secs);
            Serial.print(",");
            Serial.println(_count);
        }

    public:
        Channel(int pin) : _pin(pin), _count(0) {
            _pin = pin;
            _count = 0;
            _channel = channelCount;
            channelCount++;
        }
        void process() {
            int s = digitalRead(_pin);
            if (s != _state) {
                _state = s;
                if (s == LOW) { // Fell
                    _fall = millis();
                    _count++;
                    display();
                } else {
                    _rise = millis();
                }
            }
        }
        void begin() {
            pinMode(_pin, INPUT);
        }
};

uint8_t Channel::channelCount = 0;

Channel channels[] = { 2, 3 };
#define NUM_CHANNELS (sizeof(channels) / sizeof(channels[0]))

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

    Serial.println("Channel,Minutes,Seconds,Count");

    for (unsigned int i = 0; i &lt; NUM_CHANNELS; i++) {
        channels[i].begin();
    }
}

void loop() {
    for (unsigned int i = 0; i &lt; NUM_CHANNELS; i++) {
        channels[i].process();
    }   
}
</code></pre>
