A simple timer

Dependencies:   mbed

Committer:
pythonworld
Date:
Sun Sep 17 22:25:35 2017 +0000
Revision:
0:d962bf5b5f53
Child:
1:943070f97366
a simple timer with 4 keys control.; 10s, 300s, 600s, 900s.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pythonworld 0:d962bf5b5f53 1 #include "mbed.h"
pythonworld 0:d962bf5b5f53 2
pythonworld 0:d962bf5b5f53 3 int status = 0;
pythonworld 0:d962bf5b5f53 4 int second =5;
pythonworld 0:d962bf5b5f53 5 const float interval1 = 0.3;
pythonworld 0:d962bf5b5f53 6 const float interval0 = 0.5;
pythonworld 0:d962bf5b5f53 7 DigitalOut my_led(D13);
pythonworld 0:d962bf5b5f53 8 DigitalOut my_buzzer(D8);
pythonworld 0:d962bf5b5f53 9
pythonworld 0:d962bf5b5f53 10 InterruptIn my_button1(D6);
pythonworld 0:d962bf5b5f53 11 InterruptIn my_button2(D5);
pythonworld 0:d962bf5b5f53 12 InterruptIn my_button3(D4);
pythonworld 0:d962bf5b5f53 13 InterruptIn my_button4(D3);
pythonworld 0:d962bf5b5f53 14
pythonworld 0:d962bf5b5f53 15 Timeout my_timer;
pythonworld 0:d962bf5b5f53 16
pythonworld 0:d962bf5b5f53 17 void key1_pressed()
pythonworld 0:d962bf5b5f53 18 {
pythonworld 0:d962bf5b5f53 19
pythonworld 0:d962bf5b5f53 20 my_timer.detach();
pythonworld 0:d962bf5b5f53 21 status = 1;
pythonworld 0:d962bf5b5f53 22 second = 10;
pythonworld 0:d962bf5b5f53 23
pythonworld 0:d962bf5b5f53 24 }
pythonworld 0:d962bf5b5f53 25
pythonworld 0:d962bf5b5f53 26 void key2_pressed()
pythonworld 0:d962bf5b5f53 27 {
pythonworld 0:d962bf5b5f53 28
pythonworld 0:d962bf5b5f53 29 my_timer.detach();
pythonworld 0:d962bf5b5f53 30 status = 1;
pythonworld 0:d962bf5b5f53 31
pythonworld 0:d962bf5b5f53 32 second = 300;
pythonworld 0:d962bf5b5f53 33 }
pythonworld 0:d962bf5b5f53 34
pythonworld 0:d962bf5b5f53 35 void key3_pressed()
pythonworld 0:d962bf5b5f53 36 {
pythonworld 0:d962bf5b5f53 37
pythonworld 0:d962bf5b5f53 38 my_timer.detach();
pythonworld 0:d962bf5b5f53 39 status = 1;
pythonworld 0:d962bf5b5f53 40
pythonworld 0:d962bf5b5f53 41 second = 600;
pythonworld 0:d962bf5b5f53 42 }
pythonworld 0:d962bf5b5f53 43
pythonworld 0:d962bf5b5f53 44 void key4_pressed()
pythonworld 0:d962bf5b5f53 45 {
pythonworld 0:d962bf5b5f53 46
pythonworld 0:d962bf5b5f53 47 my_timer.detach();
pythonworld 0:d962bf5b5f53 48 status = 1;
pythonworld 0:d962bf5b5f53 49
pythonworld 0:d962bf5b5f53 50 second = 900;
pythonworld 0:d962bf5b5f53 51 }
pythonworld 0:d962bf5b5f53 52
pythonworld 0:d962bf5b5f53 53 void buzzer()
pythonworld 0:d962bf5b5f53 54 {
pythonworld 0:d962bf5b5f53 55 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 56 my_led = !my_led;
pythonworld 0:d962bf5b5f53 57 wait(interval1);
pythonworld 0:d962bf5b5f53 58 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 59 wait(interval0);
pythonworld 0:d962bf5b5f53 60
pythonworld 0:d962bf5b5f53 61 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 62 my_led = !my_led;
pythonworld 0:d962bf5b5f53 63 wait(interval1);
pythonworld 0:d962bf5b5f53 64 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 65 wait(interval0);
pythonworld 0:d962bf5b5f53 66
pythonworld 0:d962bf5b5f53 67 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 68 my_led = !my_led;
pythonworld 0:d962bf5b5f53 69 wait(interval1);
pythonworld 0:d962bf5b5f53 70 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 71 wait(interval0);
pythonworld 0:d962bf5b5f53 72
pythonworld 0:d962bf5b5f53 73 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 74 my_led = !my_led;
pythonworld 0:d962bf5b5f53 75 wait(interval1);
pythonworld 0:d962bf5b5f53 76 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 77 wait(interval0);
pythonworld 0:d962bf5b5f53 78
pythonworld 0:d962bf5b5f53 79 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 80 my_led = !my_led;
pythonworld 0:d962bf5b5f53 81 wait(interval1);
pythonworld 0:d962bf5b5f53 82 my_buzzer = !my_buzzer;
pythonworld 0:d962bf5b5f53 83 wait(interval0);
pythonworld 0:d962bf5b5f53 84 status = 0;
pythonworld 0:d962bf5b5f53 85
pythonworld 0:d962bf5b5f53 86 }
pythonworld 0:d962bf5b5f53 87
pythonworld 0:d962bf5b5f53 88
pythonworld 0:d962bf5b5f53 89 int main()
pythonworld 0:d962bf5b5f53 90 {
pythonworld 0:d962bf5b5f53 91 my_buzzer.write(1);
pythonworld 0:d962bf5b5f53 92 // Set button
pythonworld 0:d962bf5b5f53 93 my_button1.fall(&key1_pressed);
pythonworld 0:d962bf5b5f53 94 // my_button1.rise(&key1_pressed);
pythonworld 0:d962bf5b5f53 95
pythonworld 0:d962bf5b5f53 96 my_button2.fall(&key2_pressed);
pythonworld 0:d962bf5b5f53 97 // my_button2.rise(&key2_pressed);
pythonworld 0:d962bf5b5f53 98
pythonworld 0:d962bf5b5f53 99 my_button3.fall(&key3_pressed);
pythonworld 0:d962bf5b5f53 100 // my_button3.rise(&key3_pressed);
pythonworld 0:d962bf5b5f53 101
pythonworld 0:d962bf5b5f53 102 my_button4.fall(&key4_pressed);
pythonworld 0:d962bf5b5f53 103 // my_button4.rise(&key4_pressed);
pythonworld 0:d962bf5b5f53 104
pythonworld 0:d962bf5b5f53 105
pythonworld 0:d962bf5b5f53 106 while (1) {
pythonworld 0:d962bf5b5f53 107 if(status == 1) {
pythonworld 0:d962bf5b5f53 108 my_timer.attach(&buzzer,second);
pythonworld 0:d962bf5b5f53 109 status = 2;
pythonworld 0:d962bf5b5f53 110 }
pythonworld 0:d962bf5b5f53 111 if(status == 2)
pythonworld 0:d962bf5b5f53 112 { wait(0.5);
pythonworld 0:d962bf5b5f53 113 my_led = !my_led;
pythonworld 0:d962bf5b5f53 114 }
pythonworld 0:d962bf5b5f53 115 }
pythonworld 0:d962bf5b5f53 116
pythonworld 0:d962bf5b5f53 117 }