ECEM119 assignment 2 problem 4

main.cpp

Committer:
natasha41575
Date:
2019-04-17
Revision:
0:44f80f935e35
Child:
1:eee108fd0b4b

File content as of revision 0:44f80f935e35:

/* mbed Microcontroller Library
 * Copyright (c) 2018 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "stats_report.h"
//
PwmOut output(PTA10);
DigitalIn input(PTD9);
Timer timer1;
Timer timer2;

Serial pc(USBTX, USBRX); //create a Serial object

int main() {
    int period_ms = 500;
    output.period_ms(period_ms);
    output.write(0.5);
    timer1.start();
    timer2.start();
     
    int prev = 0;
    int count = 0;
    int t = 0;
    float d = 0;
    int time_on = 0;
   
    while(1) {
        pc.baud(115200);
        int curr = input.read();
        if (curr) {
            if (prev == 0) {
                timer2.reset();
                if (count == 0) {
                    t = timer1.read_us();
                    d = float(time_on) / t;
                    pc.printf("mbed> %i, %f\n", t, d);
                }
                count = (count + 1) % 10;
                timer1.reset();
            }
            prev = 1;
        } else {
            if (prev == 1) {
                time_on = timer2.read_us();
            }
            prev = 0;
        }
    }
}