Exercise from subject Programming for Embedded systems

Dependencies:   MODSERIAL mbed

Committer:
Mirjam
Date:
Wed Sep 12 10:16:23 2018 +0000
Revision:
3:3cc8759a826f
Parent:
2:51248529975d
Publish exercise 3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mirjam 0:9ac802a0d2d9 1 #include "mbed.h"
Mirjam 0:9ac802a0d2d9 2 #include "MODSERIAL.h"
Mirjam 1:b483225a5411 3 MODSERIAL pc(USBTX, USBRX);\
Mirjam 1:b483225a5411 4
Mirjam 0:9ac802a0d2d9 5 // Make a BlinkLEd function using Ticker
Mirjam 0:9ac802a0d2d9 6 //Wait for a character to arrive pc.getc() in the main loop,
Mirjam 3:3cc8759a826f 7 //if character == ‘r’ blink red LED
Mirjam 3:3cc8759a826f 8 //if character == ‘b’ blink blue LED
Mirjam 3:3cc8759a826f 9 //if character == ‘g’ blink green LED
Mirjam 1:b483225a5411 10
Mirjam 3:3cc8759a826f 11 Ticker blink; //Create the ticker
Mirjam 0:9ac802a0d2d9 12
Mirjam 3:3cc8759a826f 13 char currentState; //Make character currentState
Mirjam 0:9ac802a0d2d9 14
Mirjam 3:3cc8759a826f 15 DigitalOut ledr(LED_RED); //Define DigitalOuts for every colour
Mirjam 0:9ac802a0d2d9 16 DigitalOut ledb(LED_BLUE);
Mirjam 0:9ac802a0d2d9 17 DigitalOut ledg(LED_GREEN);
Mirjam 0:9ac802a0d2d9 18
Mirjam 3:3cc8759a826f 19
Mirjam 3:3cc8759a826f 20
Mirjam 3:3cc8759a826f 21 void BlinkLed(void) //Define the function which makes the led blink
Mirjam 0:9ac802a0d2d9 22 {
Mirjam 3:3cc8759a826f 23 switch (currentState) //Make a switch which takes the character from the keyboard
Mirjam 0:9ac802a0d2d9 24 {
Mirjam 3:3cc8759a826f 25 case 'r': //If the input character is r..
Mirjam 3:3cc8759a826f 26 ledr = !ledr; //let the red light blink..
Mirjam 3:3cc8759a826f 27 ledb=1; //and turn off the rest
Mirjam 2:51248529975d 28 ledg=1;
Mirjam 0:9ac802a0d2d9 29 break;
Mirjam 0:9ac802a0d2d9 30
Mirjam 1:b483225a5411 31 case 'b': //If the input character is b
Mirjam 0:9ac802a0d2d9 32 ledb = !ledb;
Mirjam 2:51248529975d 33 ledr=1;
Mirjam 2:51248529975d 34 ledg=1;
Mirjam 0:9ac802a0d2d9 35 break;
Mirjam 0:9ac802a0d2d9 36
Mirjam 1:b483225a5411 37 case 'g': //If the input character is g
Mirjam 0:9ac802a0d2d9 38 ledg= !ledg;
Mirjam 2:51248529975d 39 ledb=1;
Mirjam 2:51248529975d 40 ledr=1;
Mirjam 0:9ac802a0d2d9 41 break;
Mirjam 0:9ac802a0d2d9 42
Mirjam 0:9ac802a0d2d9 43 default:
Mirjam 0:9ac802a0d2d9 44 break;
Mirjam 0:9ac802a0d2d9 45 } // End of switch
Mirjam 1:b483225a5411 46 }
Mirjam 0:9ac802a0d2d9 47
Mirjam 0:9ac802a0d2d9 48 int main()
Mirjam 0:9ac802a0d2d9 49 {
Mirjam 3:3cc8759a826f 50 //Start with the LED switched off
Mirjam 3:3cc8759a826f 51 ledr = 1;
Mirjam 3:3cc8759a826f 52 ledb = 1;
Mirjam 3:3cc8759a826f 53 ledg = 1;
Mirjam 3:3cc8759a826f 54
Mirjam 0:9ac802a0d2d9 55 blink.attach(BlinkLed,1); //Gaat elke seconde checken
Mirjam 0:9ac802a0d2d9 56 pc.baud(115200); //Instelling voor pc
Mirjam 2:51248529975d 57
Mirjam 0:9ac802a0d2d9 58 while (true) {
Mirjam 2:51248529975d 59 currentState = pc.getc();
Mirjam 0:9ac802a0d2d9 60 }
Mirjam 0:9ac802a0d2d9 61 }