Hauptprogramm
Dependencies: ILI9340_Driver_Lib PM2_Libary Lib_DFPlayerMini
main.cpp@19:9ccd591cb637, 2021-04-22 (annotated)
- Committer:
- haefeman
- Date:
- Thu Apr 22 14:43:14 2021 +0000
- Revision:
- 19:9ccd591cb637
- Parent:
- 18:39be9e19768f
- Child:
- 20:f825e91784fe
implement servo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 1:93d997d6b232 | 1 | #include "mbed.h" |
ackerden | 16:f0480e9c5039 | 2 | #include "Servo.h" |
ackerden | 16:f0480e9c5039 | 3 | //Eigene Header einbinden |
pmic | 6:e1fa1a2d7483 | 4 | |
ackerden | 16:f0480e9c5039 | 5 | //include Zeitfunktion |
ackerden | 13:096e5dc3ac23 | 6 | #include "realtimer.h" |
haefeman | 11:be62f37f3a98 | 7 | |
ackerden | 16:f0480e9c5039 | 8 | //include Servosteuerung |
ackerden | 16:f0480e9c5039 | 9 | #include "servo_bewegung.h" |
ackerden | 16:f0480e9c5039 | 10 | |
ackerden | 13:096e5dc3ac23 | 11 | |
ackerden | 15:babdd038715a | 12 | //LED Anzeige |
ackerden | 16:f0480e9c5039 | 13 | //#include "Adafruit_LED_Backpack.h" |
ackerden | 16:f0480e9c5039 | 14 | |
ackerden | 16:f0480e9c5039 | 15 | |
ackerden | 16:f0480e9c5039 | 16 | |
ackerden | 15:babdd038715a | 17 | |
ackerden | 16:f0480e9c5039 | 18 | #define MID 1500 |
ackerden | 16:f0480e9c5039 | 19 | #define MIN 1000 |
ackerden | 16:f0480e9c5039 | 20 | #define MAX 2000 |
ackerden | 16:f0480e9c5039 | 21 | #define STEP 50 |
ackerden | 16:f0480e9c5039 | 22 | //Time delay between steps in milliseconds |
ackerden | 16:f0480e9c5039 | 23 | #define TIME 100 |
pmic | 6:e1fa1a2d7483 | 24 | |
pmic | 6:e1fa1a2d7483 | 25 | using namespace std::chrono; |
pmic | 6:e1fa1a2d7483 | 26 | |
pmic | 8:9bb806a7f585 | 27 | InterruptIn user_button(USER_BUTTON); |
pmic | 8:9bb806a7f585 | 28 | DigitalOut led(LED1); |
pmic | 6:e1fa1a2d7483 | 29 | |
ackerden | 12:dfa3591affef | 30 | |
ackerden | 16:f0480e9c5039 | 31 | |
ackerden | 16:f0480e9c5039 | 32 | //I2C i2c(I2C_SDA , I2C_SCL); |
ackerden | 16:f0480e9c5039 | 33 | |
ackerden | 16:f0480e9c5039 | 34 | |
pmic | 6:e1fa1a2d7483 | 35 | bool executeMainTask = false; |
pmic | 6:e1fa1a2d7483 | 36 | Timer user_button_timer, loop_timer; |
ackerden | 13:096e5dc3ac23 | 37 | |
pmic | 6:e1fa1a2d7483 | 38 | |
pmic | 6:e1fa1a2d7483 | 39 | /* declaration of custom button functions */ |
pmic | 6:e1fa1a2d7483 | 40 | void button_fall(); |
pmic | 6:e1fa1a2d7483 | 41 | void button_rise(); |
pmic | 6:e1fa1a2d7483 | 42 | |
ackerden | 16:f0480e9c5039 | 43 | /*void setup() { |
ackerden | 16:f0480e9c5039 | 44 | #ifndef __AVR_ATtiny85__ |
ackerden | 16:f0480e9c5039 | 45 | Serial.begin(9600); |
ackerden | 16:f0480e9c5039 | 46 | Serial.println("7 Segment Backpack Test"); |
ackerden | 16:f0480e9c5039 | 47 | #endif |
ackerden | 16:f0480e9c5039 | 48 | matrix.begin(0x70); |
ackerden | 16:f0480e9c5039 | 49 | }*/ |
ackerden | 16:f0480e9c5039 | 50 | //Adafruit_7segment matrix = Adafruit_7segment(); |
ackerden | 15:babdd038715a | 51 | |
pmic | 1:93d997d6b232 | 52 | int main() |
pmic | 9:f10b974d01e0 | 53 | { |
ackerden | 13:096e5dc3ac23 | 54 | set_time(1618332129); //Zeit setzen |
ackerden | 16:f0480e9c5039 | 55 | |
ackerden | 16:f0480e9c5039 | 56 | //Laufvariablen |
ackerden | 14:153f377f4030 | 57 | int i = 0; |
haefeman | 19:9ccd591cb637 | 58 | Servo servo1(PA_0); |
haefeman | 19:9ccd591cb637 | 59 | servo1.Enable(1000,20000); |
ackerden | 14:153f377f4030 | 60 | |
pmic | 6:e1fa1a2d7483 | 61 | user_button.fall(&button_fall); |
pmic | 6:e1fa1a2d7483 | 62 | user_button.rise(&button_rise); |
pmic | 6:e1fa1a2d7483 | 63 | loop_timer.start(); |
pmic | 6:e1fa1a2d7483 | 64 | |
pmic | 1:93d997d6b232 | 65 | while (true) { |
pmic | 6:e1fa1a2d7483 | 66 | |
pmic | 6:e1fa1a2d7483 | 67 | loop_timer.reset(); |
ackerden | 12:dfa3591affef | 68 | |
ackerden | 16:f0480e9c5039 | 69 | /*matrix.print(10000, DEC); |
ackerden | 16:f0480e9c5039 | 70 | matrix.writeDisplay(); |
ackerden | 16:f0480e9c5039 | 71 | delay(500); |
ackerden | 16:f0480e9c5039 | 72 | |
ackerden | 16:f0480e9c5039 | 73 | // print a hex number |
ackerden | 16:f0480e9c5039 | 74 | matrix.print(0xBEEF, HEX); |
ackerden | 16:f0480e9c5039 | 75 | matrix.writeDisplay(); |
ackerden | 16:f0480e9c5039 | 76 | delay(500); |
ackerden | 16:f0480e9c5039 | 77 | |
ackerden | 16:f0480e9c5039 | 78 | // print a floating point |
ackerden | 16:f0480e9c5039 | 79 | matrix.print(12.34); |
ackerden | 16:f0480e9c5039 | 80 | matrix.writeDisplay(); |
ackerden | 16:f0480e9c5039 | 81 | delay(500); |
ackerden | 16:f0480e9c5039 | 82 | |
ackerden | 16:f0480e9c5039 | 83 | // print with print/println |
ackerden | 16:f0480e9c5039 | 84 | for (uint16_t counter = 0; counter < 9999; counter++) { |
ackerden | 16:f0480e9c5039 | 85 | matrix.println(counter); |
ackerden | 16:f0480e9c5039 | 86 | matrix.writeDisplay(); |
ackerden | 16:f0480e9c5039 | 87 | delay(10);*/ |
pmic | 6:e1fa1a2d7483 | 88 | |
ackerden | 12:dfa3591affef | 89 | |
pmic | 6:e1fa1a2d7483 | 90 | |
pmic | 6:e1fa1a2d7483 | 91 | if (executeMainTask) { |
ackerden | 16:f0480e9c5039 | 92 | |
ackerden | 17:621c4212fd4e | 93 | //Zeitfunktion |
ackerden | 17:621c4212fd4e | 94 | uhrzeit(time(NULL)); |
ackerden | 17:621c4212fd4e | 95 | |
ackerden | 17:621c4212fd4e | 96 | switch(i){ |
ackerden | 17:621c4212fd4e | 97 | //2 Minuten |
ackerden | 18:39be9e19768f | 98 | case 120: i += 5; |
ackerden | 17:621c4212fd4e | 99 | //Seesaw Programm |
ackerden | 17:621c4212fd4e | 100 | ThisThread::sleep_for(5s); //Wartet 5s.. |
ackerden | 17:621c4212fd4e | 101 | break; |
ackerden | 17:621c4212fd4e | 102 | //5 Minuten |
haefeman | 19:9ccd591cb637 | 103 | case 30: i += 5; |
ackerden | 17:621c4212fd4e | 104 | //Servo Bewegung |
haefeman | 19:9ccd591cb637 | 105 | for(int j=0; j<2; j++){ |
haefeman | 19:9ccd591cb637 | 106 | for (int pos = 800; pos < 1200; pos += 25) { |
haefeman | 19:9ccd591cb637 | 107 | servo1.SetPosition(pos); |
haefeman | 19:9ccd591cb637 | 108 | thread_sleep_for(STEP); |
haefeman | 19:9ccd591cb637 | 109 | } |
haefeman | 19:9ccd591cb637 | 110 | for (int pos = 1200; pos > 800; pos -= 25) { |
haefeman | 19:9ccd591cb637 | 111 | servo1.SetPosition(pos); |
haefeman | 19:9ccd591cb637 | 112 | thread_sleep_for(STEP); |
haefeman | 19:9ccd591cb637 | 113 | } |
haefeman | 19:9ccd591cb637 | 114 | } |
haefeman | 19:9ccd591cb637 | 115 | servo1.SetPosition(1000); |
ackerden | 17:621c4212fd4e | 116 | ThisThread::sleep_for(5s); //Wartet 5s.. |
ackerden | 17:621c4212fd4e | 117 | break; |
ackerden | 17:621c4212fd4e | 118 | //7 Minuten |
ackerden | 18:39be9e19768f | 119 | case 420: i += 5; |
ackerden | 17:621c4212fd4e | 120 | //Audio Signal |
ackerden | 17:621c4212fd4e | 121 | ThisThread::sleep_for(5s); //Wartet 5s.. |
ackerden | 17:621c4212fd4e | 122 | break; |
ackerden | 17:621c4212fd4e | 123 | //10 Minuten |
ackerden | 18:39be9e19768f | 124 | case 600: i = 0; |
haefeman | 19:9ccd591cb637 | 125 | ThisThread::sleep_for(5s); //Wartet 5s.. |
ackerden | 17:621c4212fd4e | 126 | break; |
ackerden | 17:621c4212fd4e | 127 | default : i += 5; |
ackerden | 17:621c4212fd4e | 128 | ThisThread::sleep_for(5s); //Wartet 5s.. |
ackerden | 17:621c4212fd4e | 129 | break; |
ackerden | 18:39be9e19768f | 130 | } |
ackerden | 17:621c4212fd4e | 131 | |
ackerden | 17:621c4212fd4e | 132 | |
ackerden | 18:39be9e19768f | 133 | /* for (int pos = 1000; pos < 2000; pos += 25) { |
ackerden | 16:f0480e9c5039 | 134 | Servo1.SetPosition(pos); |
ackerden | 16:f0480e9c5039 | 135 | wait_ns(50); |
ackerden | 16:f0480e9c5039 | 136 | } |
ackerden | 16:f0480e9c5039 | 137 | for (int pos = 2000; pos > 1000; pos -= 25) { |
ackerden | 16:f0480e9c5039 | 138 | Servo1.SetPosition(pos); |
ackerden | 16:f0480e9c5039 | 139 | wait_ns(50); |
ackerden | 18:39be9e19768f | 140 | }*/ |
ackerden | 18:39be9e19768f | 141 | //bewegung(zeit); |
ackerden | 16:f0480e9c5039 | 142 | |
ackerden | 13:096e5dc3ac23 | 143 | led = !led; |
ackerden | 18:39be9e19768f | 144 | |
ackerden | 18:39be9e19768f | 145 | }} |
pmic | 1:93d997d6b232 | 146 | } |
pmic | 6:e1fa1a2d7483 | 147 | |
pmic | 6:e1fa1a2d7483 | 148 | void button_fall() |
pmic | 6:e1fa1a2d7483 | 149 | { |
pmic | 6:e1fa1a2d7483 | 150 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 151 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 152 | } |
pmic | 6:e1fa1a2d7483 | 153 | |
pmic | 6:e1fa1a2d7483 | 154 | void button_rise() |
pmic | 6:e1fa1a2d7483 | 155 | { |
pmic | 6:e1fa1a2d7483 | 156 | int t_button_ms = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 157 | user_button_timer.stop(); |
pmic | 8:9bb806a7f585 | 158 | if (t_button_ms > 200) { |
pmic | 6:e1fa1a2d7483 | 159 | executeMainTask = !executeMainTask; |
pmic | 8:9bb806a7f585 | 160 | } |
ackerden | 16:f0480e9c5039 | 161 | } |
ackerden | 16:f0480e9c5039 | 162 | |
ackerden | 16:f0480e9c5039 | 163 | /*uint16_t blinkcounter = 0; |
ackerden | 16:f0480e9c5039 | 164 | boolean drawDots = false; |
ackerden | 16:f0480e9c5039 | 165 | for (uint16_t counter = 0; counter < 9999; counter ++) { |
ackerden | 16:f0480e9c5039 | 166 | matrix.writeDigitNum(0, (counter / 1000), drawDots); |
ackerden | 16:f0480e9c5039 | 167 | matrix.writeDigitNum(1, (counter / 100) % 10, drawDots); |
ackerden | 16:f0480e9c5039 | 168 | matrix.drawColon(drawDots); |
ackerden | 16:f0480e9c5039 | 169 | matrix.writeDigitNum(3, (counter / 10) % 10, drawDots); |
ackerden | 16:f0480e9c5039 | 170 | matrix.writeDigitNum(4, counter % 10, drawDots); |
ackerden | 16:f0480e9c5039 | 171 | |
ackerden | 16:f0480e9c5039 | 172 | blinkcounter+=50; |
ackerden | 16:f0480e9c5039 | 173 | if (blinkcounter < 500) { |
ackerden | 16:f0480e9c5039 | 174 | drawDots = false; |
ackerden | 16:f0480e9c5039 | 175 | } else if (blinkcounter < 1000) { |
ackerden | 16:f0480e9c5039 | 176 | drawDots = true; |
ackerden | 16:f0480e9c5039 | 177 | } else { |
ackerden | 16:f0480e9c5039 | 178 | blinkcounter = 0; |
ackerden | 16:f0480e9c5039 | 179 | } |
ackerden | 16:f0480e9c5039 | 180 | matrix.writeDisplay(); |
ackerden | 16:f0480e9c5039 | 181 | delay(10); |
ackerden | 16:f0480e9c5039 | 182 | }*/ |