Denise Ackermann
/
Plant-Buddy
Plant-Buddy
main.cpp@0:3be1ed290a50, 2021-04-14 (annotated)
- Committer:
- ackerden
- Date:
- Wed Apr 14 13:12:27 2021 +0000
- Revision:
- 0:3be1ed290a50
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ackerden | 0:3be1ed290a50 | 1 | #include "mbed.h" |
ackerden | 0:3be1ed290a50 | 2 | |
ackerden | 0:3be1ed290a50 | 3 | |
ackerden | 0:3be1ed290a50 | 4 | #include "mbed_rtc_time.h" |
ackerden | 0:3be1ed290a50 | 5 | |
ackerden | 0:3be1ed290a50 | 6 | DigitalOut myled(LED1); |
ackerden | 0:3be1ed290a50 | 7 | InterruptIn user_button(USER_BUTTON); |
ackerden | 0:3be1ed290a50 | 8 | |
ackerden | 0:3be1ed290a50 | 9 | bool executeMainTask = false; |
ackerden | 0:3be1ed290a50 | 10 | Timer user_button_timer, loop_timer; |
ackerden | 0:3be1ed290a50 | 11 | int Ts_ms = 50; |
ackerden | 0:3be1ed290a50 | 12 | |
ackerden | 0:3be1ed290a50 | 13 | /* declaration of custom button functions */ |
ackerden | 0:3be1ed290a50 | 14 | void button_fall(); |
ackerden | 0:3be1ed290a50 | 15 | void button_rise(); |
ackerden | 0:3be1ed290a50 | 16 | |
ackerden | 0:3be1ed290a50 | 17 | int loops_per_second = static_cast<int>(ceilf(1.0f/(0.001f*(float)Ts_ms))); |
ackerden | 0:3be1ed290a50 | 18 | |
ackerden | 0:3be1ed290a50 | 19 | |
ackerden | 0:3be1ed290a50 | 20 | int main(){ |
ackerden | 0:3be1ed290a50 | 21 | set_time(1618332129); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
ackerden | 0:3be1ed290a50 | 22 | user_button.fall(&button_fall); |
ackerden | 0:3be1ed290a50 | 23 | user_button.rise(&button_rise); |
ackerden | 0:3be1ed290a50 | 24 | loop_timer.start(); |
ackerden | 0:3be1ed290a50 | 25 | |
ackerden | 0:3be1ed290a50 | 26 | while(1){ |
ackerden | 0:3be1ed290a50 | 27 | if(user_button){ |
ackerden | 0:3be1ed290a50 | 28 | myled = 1;} |
ackerden | 0:3be1ed290a50 | 29 | else{ |
ackerden | 0:3be1ed290a50 | 30 | myled = 0;} |
ackerden | 0:3be1ed290a50 | 31 | wait(0.5); |
ackerden | 0:3be1ed290a50 | 32 | loop_timer.reset(); |
ackerden | 0:3be1ed290a50 | 33 | |
ackerden | 0:3be1ed290a50 | 34 | |
ackerden | 0:3be1ed290a50 | 35 | |
ackerden | 0:3be1ed290a50 | 36 | |
ackerden | 0:3be1ed290a50 | 37 | |
ackerden | 0:3be1ed290a50 | 38 | if (executeMainTask) { |
ackerden | 0:3be1ed290a50 | 39 | time_t seconds = time(NULL); |
ackerden | 0:3be1ed290a50 | 40 | |
ackerden | 0:3be1ed290a50 | 41 | printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds); |
ackerden | 0:3be1ed290a50 | 42 | |
ackerden | 0:3be1ed290a50 | 43 | printf("Time as a basic string = %s", ctime(&seconds)); |
ackerden | 0:3be1ed290a50 | 44 | |
ackerden | 0:3be1ed290a50 | 45 | char buffer[32]; |
ackerden | 0:3be1ed290a50 | 46 | strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
ackerden | 0:3be1ed290a50 | 47 | printf("Time as a custom formatted string = %s", buffer); |
ackerden | 0:3be1ed290a50 | 48 | |
ackerden | 0:3be1ed290a50 | 49 | //ThisThread::sleep_for(50); |
ackerden | 0:3be1ed290a50 | 50 | }} |
ackerden | 0:3be1ed290a50 | 51 | |
ackerden | 0:3be1ed290a50 | 52 | void button_fall() |
ackerden | 0:3be1ed290a50 | 53 | { |
ackerden | 0:3be1ed290a50 | 54 | user_button_timer.reset(); |
ackerden | 0:3be1ed290a50 | 55 | user_button_timer.start()=08:00; |
ackerden | 0:3be1ed290a50 | 56 | } |
ackerden | 0:3be1ed290a50 | 57 | |
ackerden | 0:3be1ed290a50 | 58 | void button_rise() |
ackerden | 0:3be1ed290a50 | 59 | { |
ackerden | 0:3be1ed290a50 | 60 | int t_button_ms = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
ackerden | 0:3be1ed290a50 | 61 | user_button_timer.stop(); |
ackerden | 0:3be1ed290a50 | 62 | if (t_button_ms > 200) { |
ackerden | 0:3be1ed290a50 | 63 | executeMainTask = !executeMainTask; |
ackerden | 0:3be1ed290a50 | 64 | } |