Task 1.3.5 Solution

Committer:
noutram
Date:
Thu Jul 13 14:47:09 2017 +0000
Revision:
1:9c98ce8dfdc3
Parent:
0:9333cd80614f
updated for mbed-os 5.5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:9333cd80614f 1 //This is known as a “header file”
noutram 0:9333cd80614f 2 //In short, this copies and pastes the text file
noutram 0:9333cd80614f 3 //mbed.h into this code
noutram 0:9333cd80614f 4 #include "mbed.h"
noutram 0:9333cd80614f 5
noutram 0:9333cd80614f 6 #define kRED 1
noutram 0:9333cd80614f 7 #define kYELLOW 2
noutram 0:9333cd80614f 8 #define kGREEN 4
noutram 0:9333cd80614f 9
noutram 0:9333cd80614f 10 BusOut binaryOutput(D7, D6, D5); //Outputs as an integer
noutram 0:9333cd80614f 11
noutram 0:9333cd80614f 12 unsigned int state = 0;
noutram 0:9333cd80614f 13 void toggleYellow()
noutram 0:9333cd80614f 14 {
noutram 0:9333cd80614f 15 if (state == 0) {
noutram 0:9333cd80614f 16 binaryOutput = kYELLOW;
noutram 0:9333cd80614f 17 state = 1;
noutram 0:9333cd80614f 18 } else {
noutram 0:9333cd80614f 19 binaryOutput = 0;
noutram 0:9333cd80614f 20 state = 0;
noutram 0:9333cd80614f 21 }
noutram 0:9333cd80614f 22 }
noutram 0:9333cd80614f 23
noutram 0:9333cd80614f 24 //The main function - all executable C / C++
noutram 0:9333cd80614f 25 //applications have a main function. This is
noutram 0:9333cd80614f 26 //out entry point in the software
noutram 0:9333cd80614f 27 Ticker T;
noutram 0:9333cd80614f 28
noutram 0:9333cd80614f 29 int main() {
noutram 0:9333cd80614f 30
noutram 0:9333cd80614f 31 binaryOutput = 0;
noutram 0:9333cd80614f 32
noutram 0:9333cd80614f 33 // ALL the code is contained in a
noutram 0:9333cd80614f 34 // “while loop"
noutram 0:9333cd80614f 35
noutram 0:9333cd80614f 36 // THIS IS NOT AN IDEAL SOLUTION. HOWEVER IT IS SIMPLE
noutram 0:9333cd80614f 37
noutram 0:9333cd80614f 38
noutram 0:9333cd80614f 39 while(1)
noutram 0:9333cd80614f 40 {
noutram 0:9333cd80614f 41 //The code between the { curly braces }
noutram 0:9333cd80614f 42 //is the code that is repeated
noutram 0:9333cd80614f 43
noutram 0:9333cd80614f 44 //STATE 1 (R)
noutram 0:9333cd80614f 45 binaryOutput = kRED;
noutram 0:9333cd80614f 46 wait(4.0);
noutram 0:9333cd80614f 47
noutram 0:9333cd80614f 48 //STATE 2 (RA)
noutram 0:9333cd80614f 49 binaryOutput = kRED + kYELLOW;
noutram 0:9333cd80614f 50 wait(4.0);
noutram 0:9333cd80614f 51
noutram 0:9333cd80614f 52 //STATE 3 (G)
noutram 0:9333cd80614f 53 binaryOutput = kGREEN;
noutram 0:9333cd80614f 54 wait(4.0);
noutram 0:9333cd80614f 55
noutram 0:9333cd80614f 56 //STATE 4 (Flashing A)
noutram 0:9333cd80614f 57 T.attach(toggleYellow, 0.25);
noutram 0:9333cd80614f 58 wait(4.0);
noutram 0:9333cd80614f 59 T.detach();
noutram 0:9333cd80614f 60
noutram 0:9333cd80614f 61 }
noutram 0:9333cd80614f 62 }