Hi somasudra,
i´m not sure if the student version has the event structure, but when than i recommend to use it. If the digital value changes, then you can write your value. You don´t need to create the channel everytime in the loop. Create them ones before the while loop and clear it after. To get the duration you can simply subtract one timestamp from the other.
Hope it helps.
Mike