Assignment 3 and 4. Both in commits.

Dependencies:   MODSERIAL mbed

Committer:
vsluiter
Date:
Wed Sep 02 12:33:55 2015 +0000
Revision:
0:86aaef96170e
Child:
1:958fbf7ad850
Assignment 3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsluiter 0:86aaef96170e 1 #include "mbed.h"
vsluiter 0:86aaef96170e 2 #include "MODSERIAL.h"
vsluiter 0:86aaef96170e 3
vsluiter 0:86aaef96170e 4 DigitalOut led(LED_GREEN);
vsluiter 0:86aaef96170e 5 DigitalIn button(PTA4);
vsluiter 0:86aaef96170e 6 MODSERIAL pc(USBTX, USBRX);
vsluiter 0:86aaef96170e 7
vsluiter 0:86aaef96170e 8 const int baudrate = 115200;
vsluiter 0:86aaef96170e 9 const int ms_wait = 100;
vsluiter 0:86aaef96170e 10
vsluiter 0:86aaef96170e 11 const float period_led = 0.1;
vsluiter 0:86aaef96170e 12 const float t_on = 0.1;
vsluiter 0:86aaef96170e 13 const float t_off = period_led - t_on;
vsluiter 0:86aaef96170e 14
vsluiter 0:86aaef96170e 15 const int led_on = 0;
vsluiter 0:86aaef96170e 16 const int led_off = 1;
vsluiter 0:86aaef96170e 17
vsluiter 0:86aaef96170e 18 const int button_pressed = 0;
vsluiter 0:86aaef96170e 19
vsluiter 0:86aaef96170e 20
vsluiter 0:86aaef96170e 21 void blink(int number_of_blinks)
vsluiter 0:86aaef96170e 22 {
vsluiter 0:86aaef96170e 23 for(int i = 0; i < 5 ; i++) {
vsluiter 0:86aaef96170e 24 led.write(led_on); //turn led on
vsluiter 0:86aaef96170e 25 wait(t_on);
vsluiter 0:86aaef96170e 26 led.write(led_off); // toggle led
vsluiter 0:86aaef96170e 27 wait(t_off);
vsluiter 0:86aaef96170e 28 }
vsluiter 0:86aaef96170e 29 }
vsluiter 0:86aaef96170e 30
vsluiter 0:86aaef96170e 31 int main()
vsluiter 0:86aaef96170e 32 {
vsluiter 0:86aaef96170e 33 int waittime = 0;
vsluiter 0:86aaef96170e 34 led.write(led_off);
vsluiter 0:86aaef96170e 35 pc.baud(baudrate);
vsluiter 0:86aaef96170e 36 pc.printf("Hello World!\n");
vsluiter 0:86aaef96170e 37
vsluiter 0:86aaef96170e 38 while (true) {
vsluiter 0:86aaef96170e 39 wait_ms(ms_wait); //const value
vsluiter 0:86aaef96170e 40 if(button.read() == button_pressed) //button pressed
vsluiter 0:86aaef96170e 41 {
vsluiter 0:86aaef96170e 42 waittime++;
vsluiter 0:86aaef96170e 43 pc.printf("Pressed for %d ms\n", waittime * ms_wait); //const used again -> updating it at the top gives consistent results!!
vsluiter 0:86aaef96170e 44 }
vsluiter 0:86aaef96170e 45 else //button released
vsluiter 0:86aaef96170e 46 {
vsluiter 0:86aaef96170e 47 waittime = 0;
vsluiter 0:86aaef96170e 48 }
vsluiter 0:86aaef96170e 49 if(pc.readable()) //if character available. If expresseion is non-zero, it's true
vsluiter 0:86aaef96170e 50 {
vsluiter 0:86aaef96170e 51 pc.rxBufferFlush(); //flush the buffer. Otherwise pc.readable() will stay larger than zero
vsluiter 0:86aaef96170e 52 blink(1);
vsluiter 0:86aaef96170e 53 }
vsluiter 0:86aaef96170e 54 }
vsluiter 0:86aaef96170e 55 }