Test ok
Dependencies: mbed MPU6050 DS1820
main.cpp@3:3dcdaf0d09a1, 2019-10-20 (annotated)
- Committer:
- wf
- Date:
- Sun Oct 20 13:41:52 2019 +0000
- Revision:
- 3:3dcdaf0d09a1
- Parent:
- 2:0e87ebc53aa8
Test ok
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wf | 3:3dcdaf0d09a1 | 1 | // Diese Software testet die verschiedenen Funktionen des NUClight V3 Boards |
wf | 3:3dcdaf0d09a1 | 2 | // BULME Graz, |
wf | 3:3dcdaf0d09a1 | 3 | // by F. Wolf 20.10.2019 |
wf | 0:f007d2f125be | 4 | /* |
wf | 0:f007d2f125be | 5 | PIN-OUT-NUClight |
wf | 0:f007d2f125be | 6 | NUCLEO-L432KC |
wf | 2:0e87ebc53aa8 | 7 | RGB-rot D1|-------| VIn |
wf | 2:0e87ebc53aa8 | 8 | RGB-gruen D0| | GND |
wf | 2:0e87ebc53aa8 | 9 | NRST| | RST |
wf | 2:0e87ebc53aa8 | 10 | GND| | 5V0 |
wf | 2:0e87ebc53aa8 | 11 | LED1 <-D2| | A7 |
wf | 2:0e87ebc53aa8 | 12 | LED2 <-D3| | A6 -> LED7 |
wf | 2:0e87ebc53aa8 | 13 | SDA (I2C) (MPU6050 gyro) <- D4| | A5 |
wf | 2:0e87ebc53aa8 | 14 | SCL (I2C) (MPU6050 gyro) <- D5| | A4 |
wf | 2:0e87ebc53aa8 | 15 | LED3 <-D6| | A3 -> POTI |
wf | 2:0e87ebc53aa8 | 16 | nc D7| | A2 -> Taster |
wf | 2:0e87ebc53aa8 | 17 | nc D8| | A1 -> Taster |
wf | 2:0e87ebc53aa8 | 18 | LED4 D9| | A0 -> DS18B20 |
wf | 2:0e87ebc53aa8 | 19 | RGB-blau <- D10| | ARF |
wf | 2:0e87ebc53aa8 | 20 | LED5 <- D11| | 3V0 |
wf | 2:0e87ebc53aa8 | 21 | LED6 <- D12|-------| D13 -> LED8 |
wf | 0:f007d2f125be | 22 | |
wf | 0:f007d2f125be | 23 | RGB LED aktiv hight (1) |
wf | 0:f007d2f125be | 24 | */ |
wf | 0:f007d2f125be | 25 | #include <mbed.h> |
wf | 0:f007d2f125be | 26 | #include "MPU6050.h" |
wf | 0:f007d2f125be | 27 | #include "DS1820.h" |
wf | 0:f007d2f125be | 28 | |
wf | 3:3dcdaf0d09a1 | 29 | //****** Definitionen ********************** |
wf | 3:3dcdaf0d09a1 | 30 | Serial pc(SERIAL_TX,SERIAL_RX); //nucleo |
wf | 0:f007d2f125be | 31 | |
wf | 0:f007d2f125be | 32 | // Definition der Taster (Switches) |
wf | 0:f007d2f125be | 33 | InterruptIn sw1(A1); |
wf | 0:f007d2f125be | 34 | InterruptIn sw2(A2); |
wf | 2:0e87ebc53aa8 | 35 | AnalogIn ain(A3); |
wf | 0:f007d2f125be | 36 | |
wf | 2:0e87ebc53aa8 | 37 | // Definition der 8 LED's |
wf | 0:f007d2f125be | 38 | DigitalOut led1(D2); |
wf | 0:f007d2f125be | 39 | DigitalOut led2(D3); |
wf | 0:f007d2f125be | 40 | DigitalOut led3(D6); |
wf | 2:0e87ebc53aa8 | 41 | DigitalOut led4(D9); |
wf | 2:0e87ebc53aa8 | 42 | DigitalOut led5(D11); |
wf | 2:0e87ebc53aa8 | 43 | DigitalOut led6(D12); |
wf | 2:0e87ebc53aa8 | 44 | DigitalOut led7(A6); |
wf | 2:0e87ebc53aa8 | 45 | DigitalOut led8(D13); // on Board LED |
wf | 0:f007d2f125be | 46 | |
wf | 0:f007d2f125be | 47 | // RGB LED |
wf | 2:0e87ebc53aa8 | 48 | PwmOut RGBg(D0); // gruen LED |
wf | 2:0e87ebc53aa8 | 49 | PwmOut RGBr(D1); // rote LED |
wf | 3:3dcdaf0d09a1 | 50 | PwmOut RGBb(D10); // blaue LED |
wf | 0:f007d2f125be | 51 | |
wf | 3:3dcdaf0d09a1 | 52 | // DS1820 Temperatursensor |
wf | 3:3dcdaf0d09a1 | 53 | #define DATA_PIN A0 // DS18B20 |
wf | 3:3dcdaf0d09a1 | 54 | DS1820 ds1820(DATA_PIN); // create a ds1820 sensor |
wf | 3:3dcdaf0d09a1 | 55 | |
wf | 3:3dcdaf0d09a1 | 56 | // GY-521 Module MPU-6050 3-Achsen-Gyroskop + 3 Achsen Accelerometre |
wf | 0:f007d2f125be | 57 | MPU6050 ark(D4,D5); // MPU6050(PinName sda, PinName scl); |
wf | 0:f007d2f125be | 58 | |
wf | 0:f007d2f125be | 59 | // ********** Deklarationen ************** |
wf | 0:f007d2f125be | 60 | void RGBtest(); |
wf | 0:f007d2f125be | 61 | void LEDtest(); |
wf | 0:f007d2f125be | 62 | void LEDonoff(); |
wf | 0:f007d2f125be | 63 | |
wf | 3:3dcdaf0d09a1 | 64 | // variable |
wf | 3:3dcdaf0d09a1 | 65 | int a; |
wf | 3:3dcdaf0d09a1 | 66 | int num_devices = 0; |
wf | 3:3dcdaf0d09a1 | 67 | |
wf | 0:f007d2f125be | 68 | // ********** Hauptprogramm ************** |
wf | 0:f007d2f125be | 69 | int main() |
wf | 0:f007d2f125be | 70 | { |
wf | 3:3dcdaf0d09a1 | 71 | pc.printf("******** TEST-SW *************\r\n"); //HTerm Welcome Message |
wf | 3:3dcdaf0d09a1 | 72 | pc.printf("TEST-SW Welcome to NUClight V3 \r\n"); //HTerm Welcome Message |
wf | 3:3dcdaf0d09a1 | 73 | pc.printf("*******************************\r\n"); //HTerm Welcome Message |
wf | 0:f007d2f125be | 74 | while(1) |
wf | 0:f007d2f125be | 75 | { |
wf | 3:3dcdaf0d09a1 | 76 | // attach the address of the flip function to the rising edge |
wf | 3:3dcdaf0d09a1 | 77 | sw1.fall(&RGBtest); //RGB-Test Unterprogramm aufrufen |
wf | 3:3dcdaf0d09a1 | 78 | sw2.fall(&LEDonoff); //LED ON OFF - Test Unterprogramm aufrufen |
wf | 3:3dcdaf0d09a1 | 79 | pc.printf("percentage: %3.0f%%\r\n", ain.read()*100.0f); |
wf | 3:3dcdaf0d09a1 | 80 | RGBr.period_ms(20.0f); // 20 mili second period |
wf | 3:3dcdaf0d09a1 | 81 | RGBr.pulsewidth_ms(ain.read()*10.0f); // 5 mili second pulse (on) |
wf | 3:3dcdaf0d09a1 | 82 | |
wf | 3:3dcdaf0d09a1 | 83 | wait(1); |
wf | 3:3dcdaf0d09a1 | 84 | // *** MPU6050 (gyro) TEST |
wf | 3:3dcdaf0d09a1 | 85 | // reading Temprature |
wf | 3:3dcdaf0d09a1 | 86 | float temp = ark.getTemp(); |
wf | 0:f007d2f125be | 87 | |
wf | 3:3dcdaf0d09a1 | 88 | pc.printf("MPU6050-temprature = %0.2f ^C\r\n",temp); |
wf | 3:3dcdaf0d09a1 | 89 | pc.printf("__________________\r\n"); |
wf | 3:3dcdaf0d09a1 | 90 | //reading Gyrometer readings |
wf | 0:f007d2f125be | 91 | float gyro[3]; |
wf | 0:f007d2f125be | 92 | ark.getGyro(gyro); |
wf | 3:3dcdaf0d09a1 | 93 | pc.printf("Gyroscope\r\n"); |
wf | 3:3dcdaf0d09a1 | 94 | pc.printf("__________________\r\n"); |
wf | 3:3dcdaf0d09a1 | 95 | pc.printf("Gyro0=%0.3f, Gyro1=%0.3f, Gyro2=%0.3f\r\n",gyro[0],gyro[1],gyro[2]); |
wf | 0:f007d2f125be | 96 | //reading Acclerometer readings |
wf | 0:f007d2f125be | 97 | float acce[3]; |
wf | 0:f007d2f125be | 98 | ark.getAccelero(acce); |
wf | 3:3dcdaf0d09a1 | 99 | pc.printf("Accelerometer\r\n"); |
wf | 3:3dcdaf0d09a1 | 100 | pc.printf("__________________\r\n"); |
wf | 3:3dcdaf0d09a1 | 101 | pc.printf("Acce0=%0.3f, Acce1=%0.3f, Acce2=%0.3f\r\n",acce[0],acce[1],acce[2]); |
wf | 0:f007d2f125be | 102 | wait(1); //wait 1000ms |
wf | 3:3dcdaf0d09a1 | 103 | |
wf | 3:3dcdaf0d09a1 | 104 | //DS18B20 |
wf | 3:3dcdaf0d09a1 | 105 | if(ds1820.begin()) |
wf | 3:3dcdaf0d09a1 | 106 | { |
wf | 3:3dcdaf0d09a1 | 107 | ds1820.startConversion(); // start temperature conversion |
wf | 3:3dcdaf0d09a1 | 108 | wait(1.0); // let DS1820 complete the temperature conversion |
wf | 3:3dcdaf0d09a1 | 109 | pc.printf("temprature DS1820 = %3.1f ^C\r\n", ds1820.read(),248); // read temperature |
wf | 3:3dcdaf0d09a1 | 110 | pc.printf("__________________\r\n"); |
wf | 3:3dcdaf0d09a1 | 111 | } |
wf | 3:3dcdaf0d09a1 | 112 | else |
wf | 3:3dcdaf0d09a1 | 113 | { |
wf | 3:3dcdaf0d09a1 | 114 | pc.printf("No DS1820 sensor found!\r\n"); |
wf | 3:3dcdaf0d09a1 | 115 | } |
wf | 3:3dcdaf0d09a1 | 116 | } |
wf | 3:3dcdaf0d09a1 | 117 | } |
wf | 0:f007d2f125be | 118 | |
wf | 3:3dcdaf0d09a1 | 119 | |
wf | 3:3dcdaf0d09a1 | 120 | // ********** Funktionen ************** |
wf | 3:3dcdaf0d09a1 | 121 | // TEST-RGB |
wf | 3:3dcdaf0d09a1 | 122 | //------------------------------------ |
wf | 0:f007d2f125be | 123 | void RGBtest() |
wf | 0:f007d2f125be | 124 | { |
wf | 2:0e87ebc53aa8 | 125 | pc.printf("RGBTEST\r\n"); //HTerm Ausgabe |
wf | 2:0e87ebc53aa8 | 126 | LEDtest(); |
wf | 2:0e87ebc53aa8 | 127 | for (int t=1; t<15;t++) |
wf | 2:0e87ebc53aa8 | 128 | { |
wf | 2:0e87ebc53aa8 | 129 | RGBr.period_ms(20.0f); // 20 mili second period |
wf | 2:0e87ebc53aa8 | 130 | RGBr.pulsewidth_ms(t); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 131 | wait(0.3); |
wf | 2:0e87ebc53aa8 | 132 | } |
wf | 2:0e87ebc53aa8 | 133 | |
wf | 2:0e87ebc53aa8 | 134 | RGBr.pulsewidth_ms(0); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 135 | for (int t=1; t<15;t++) |
wf | 2:0e87ebc53aa8 | 136 | { |
wf | 2:0e87ebc53aa8 | 137 | RGBg.period_ms(20.0f); // 20 mili second period |
wf | 2:0e87ebc53aa8 | 138 | RGBg.pulsewidth_ms(t); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 139 | wait(0.3); |
wf | 2:0e87ebc53aa8 | 140 | } |
wf | 3:3dcdaf0d09a1 | 141 | |
wf | 2:0e87ebc53aa8 | 142 | RGBr.pulsewidth_ms(0); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 143 | RGBg.pulsewidth_ms(0); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 144 | for (int t=1; t<15;t++) |
wf | 2:0e87ebc53aa8 | 145 | { |
wf | 2:0e87ebc53aa8 | 146 | RGBb.period_ms(20.0f); // 20 mili second period |
wf | 2:0e87ebc53aa8 | 147 | RGBb.pulsewidth_ms(t); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 148 | wait(0.3); |
wf | 2:0e87ebc53aa8 | 149 | } |
wf | 2:0e87ebc53aa8 | 150 | wait(1); |
wf | 2:0e87ebc53aa8 | 151 | RGBr.pulsewidth_ms(0); // RGB-LED off |
wf | 2:0e87ebc53aa8 | 152 | RGBg.pulsewidth_ms(0); // |
wf | 2:0e87ebc53aa8 | 153 | RGBb.pulsewidth_ms(0); // |
wf | 2:0e87ebc53aa8 | 154 | |
wf | 2:0e87ebc53aa8 | 155 | int rr=(rand()%15); |
wf | 2:0e87ebc53aa8 | 156 | int rg=(rand()%15); |
wf | 2:0e87ebc53aa8 | 157 | int rb=(rand()%15); |
wf | 2:0e87ebc53aa8 | 158 | |
wf | 2:0e87ebc53aa8 | 159 | RGBr.period_ms(20.0f); // 20 mili second period |
wf | 2:0e87ebc53aa8 | 160 | RGBr.pulsewidth_ms(rr); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 161 | |
wf | 2:0e87ebc53aa8 | 162 | RGBg.period_ms(20.0f); // 20 mili second period |
wf | 2:0e87ebc53aa8 | 163 | RGBg.pulsewidth_ms(rg); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 164 | |
wf | 2:0e87ebc53aa8 | 165 | RGBb.period_ms(20.0f); // 20 mili second period |
wf | 2:0e87ebc53aa8 | 166 | RGBb.pulsewidth_ms(rb); // 5 mili second pulse (on) |
wf | 2:0e87ebc53aa8 | 167 | |
wf | 2:0e87ebc53aa8 | 168 | wait(1); |
wf | 2:0e87ebc53aa8 | 169 | RGBr.pulsewidth_ms(0); // RGB-LED off |
wf | 2:0e87ebc53aa8 | 170 | RGBg.pulsewidth_ms(0); // |
wf | 2:0e87ebc53aa8 | 171 | RGBb.pulsewidth_ms(0); // |
wf | 2:0e87ebc53aa8 | 172 | |
wf | 0:f007d2f125be | 173 | } |
wf | 0:f007d2f125be | 174 | |
wf | 3:3dcdaf0d09a1 | 175 | // TEST-LED |
wf | 3:3dcdaf0d09a1 | 176 | //------------------------------------ |
wf | 0:f007d2f125be | 177 | void LEDtest() |
wf | 0:f007d2f125be | 178 | { |
wf | 2:0e87ebc53aa8 | 179 | pc.printf("LAUFLICHT UEBER 8 LED (5-mal) \r\n"); //HTerm Ausgabe |
wf | 0:f007d2f125be | 180 | a=0; |
wf | 0:f007d2f125be | 181 | while (a<5) { |
wf | 0:f007d2f125be | 182 | led1=1; //Led1 einschalten |
wf | 0:f007d2f125be | 183 | wait(0.2); |
wf | 0:f007d2f125be | 184 | led1=0; //Led1 ausschalten |
wf | 0:f007d2f125be | 185 | |
wf | 0:f007d2f125be | 186 | led2=1; //Led2 einschalten |
wf | 0:f007d2f125be | 187 | wait(0.2); |
wf | 0:f007d2f125be | 188 | led2=0; //Led2 ausschalten |
wf | 0:f007d2f125be | 189 | |
wf | 0:f007d2f125be | 190 | led3=1; //Led3 einschalten |
wf | 0:f007d2f125be | 191 | wait(0.2); |
wf | 0:f007d2f125be | 192 | led3=0; //Led3 ausschalten |
wf | 0:f007d2f125be | 193 | |
wf | 2:0e87ebc53aa8 | 194 | led4=1; //Led1 einschalten |
wf | 2:0e87ebc53aa8 | 195 | wait(0.2); |
wf | 2:0e87ebc53aa8 | 196 | led4=0; //Led1 ausschalten |
wf | 2:0e87ebc53aa8 | 197 | |
wf | 2:0e87ebc53aa8 | 198 | led5=1; //Led2 einschalten |
wf | 2:0e87ebc53aa8 | 199 | wait(0.2); |
wf | 2:0e87ebc53aa8 | 200 | led5=0; //Led2 ausschalten |
wf | 2:0e87ebc53aa8 | 201 | |
wf | 2:0e87ebc53aa8 | 202 | led6=1; //Led3 einschalten |
wf | 2:0e87ebc53aa8 | 203 | wait(0.2); |
wf | 2:0e87ebc53aa8 | 204 | led6=0; //Led3 ausschalten |
wf | 2:0e87ebc53aa8 | 205 | |
wf | 2:0e87ebc53aa8 | 206 | led7=1; //Led2 einschalten |
wf | 2:0e87ebc53aa8 | 207 | wait(0.2); |
wf | 2:0e87ebc53aa8 | 208 | led7=0; //Led2 ausschalten |
wf | 2:0e87ebc53aa8 | 209 | |
wf | 2:0e87ebc53aa8 | 210 | led8=1; //Led3 einschalten |
wf | 2:0e87ebc53aa8 | 211 | wait(0.2); |
wf | 2:0e87ebc53aa8 | 212 | led8=0; //Led3 ausschalten |
wf | 0:f007d2f125be | 213 | a++; |
wf | 0:f007d2f125be | 214 | } |
wf | 0:f007d2f125be | 215 | } |
wf | 0:f007d2f125be | 216 | |
wf | 3:3dcdaf0d09a1 | 217 | // TEST-LED-on-off |
wf | 3:3dcdaf0d09a1 | 218 | //------------------------------------ |
wf | 0:f007d2f125be | 219 | void LEDonoff() |
wf | 0:f007d2f125be | 220 | { |
wf | 0:f007d2f125be | 221 | led1=!led1; //Led1 einschalten |
wf | 0:f007d2f125be | 222 | led2=!led2; //Led1 einschalten |
wf | 0:f007d2f125be | 223 | led3=!led3; //Led1 einschalten |
wf | 2:0e87ebc53aa8 | 224 | led4=!led4; //Led1 einschalten |
wf | 2:0e87ebc53aa8 | 225 | led5=!led5; //Led1 einschalten |
wf | 2:0e87ebc53aa8 | 226 | led6=!led6; //Led1 einschalten |
wf | 2:0e87ebc53aa8 | 227 | led7=!led7; //Led1 einschalten |
wf | 2:0e87ebc53aa8 | 228 | led8=!led8; //Led1 einschalten |
wf | 0:f007d2f125be | 229 | } |
wf | 0:f007d2f125be | 230 | /******************** ENDE ***********************/ |