Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@5:7a63cb7ec666, 2019-12-06 (annotated)
- Committer:
- guilhemMBED
- Date:
- Fri Dec 06 14:29:16 2019 +0000
- Revision:
- 5:7a63cb7ec666
- Parent:
- 3:f41f244de0b3
- Child:
- 6:9aeb18bebe85
prog 1 capteur avec detection de prox et de couleur
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
guilhemMBED | 0:eace82fb39de | 1 | #include "mbed.h" |
guilhemMBED | 0:eace82fb39de | 2 | #include "glibr.h" |
guilhemMBED | 1:7cd9426abb58 | 3 | |
guilhemMBED | 0:eace82fb39de | 4 | Serial USB_link(USBTX, USBRX); // USB initialization |
guilhemMBED | 3:f41f244de0b3 | 5 | |
guilhemMBED | 5:7a63cb7ec666 | 6 | PwmOut LED(A2); // LED initialization |
guilhemMBED | 5:7a63cb7ec666 | 7 | |
guilhemMBED | 3:f41f244de0b3 | 8 | glibr capt1(D4,D5); // I²C initialization : D4 = SDA ; D5 = SCL |
guilhemMBED | 5:7a63cb7ec666 | 9 | |
guilhemMBED | 5:7a63cb7ec666 | 10 | //CAN can1(D0, D1); // CAN initialization |
guilhemMBED | 0:eace82fb39de | 11 | |
guilhemMBED | 3:f41f244de0b3 | 12 | bool initialization(void) |
guilhemMBED | 3:f41f244de0b3 | 13 | { |
guilhemMBED | 3:f41f244de0b3 | 14 | // USB initialization |
guilhemMBED | 3:f41f244de0b3 | 15 | USB_link.baud(115200); |
guilhemMBED | 3:f41f244de0b3 | 16 | USB_link.format(8, SerialBase::None, 1); |
guilhemMBED | 3:f41f244de0b3 | 17 | |
guilhemMBED | 5:7a63cb7ec666 | 18 | // LED initialization |
guilhemMBED | 5:7a63cb7ec666 | 19 | LED.period_ms(10); |
guilhemMBED | 5:7a63cb7ec666 | 20 | LED.write(0.5); |
guilhemMBED | 5:7a63cb7ec666 | 21 | |
guilhemMBED | 5:7a63cb7ec666 | 22 | // sensor initalization |
guilhemMBED | 5:7a63cb7ec666 | 23 | if( (capt1.ginit()) && (capt1.enableLightSensor(true)) && (capt1.enableProximitySensor(true)) ) { |
guilhemMBED | 3:f41f244de0b3 | 24 | return true; |
guilhemMBED | 3:f41f244de0b3 | 25 | } else { |
guilhemMBED | 3:f41f244de0b3 | 26 | return false; |
guilhemMBED | 3:f41f244de0b3 | 27 | } |
guilhemMBED | 3:f41f244de0b3 | 28 | } |
guilhemMBED | 0:eace82fb39de | 29 | |
guilhemMBED | 1:7cd9426abb58 | 30 | int main (void) |
guilhemMBED | 1:7cd9426abb58 | 31 | { |
guilhemMBED | 5:7a63cb7ec666 | 32 | char color; |
guilhemMBED | 5:7a63cb7ec666 | 33 | uint16_t r,g,b; |
guilhemMBED | 5:7a63cb7ec666 | 34 | uint8_t a; |
guilhemMBED | 5:7a63cb7ec666 | 35 | if (initialization()) USB_link.printf("Initialization complete \r\n"); |
guilhemMBED | 5:7a63cb7ec666 | 36 | else USB_link.printf("Error during initialization\r\n"); |
guilhemMBED | 5:7a63cb7ec666 | 37 | |
guilhemMBED | 0:eace82fb39de | 38 | while(1) { |
guilhemMBED | 5:7a63cb7ec666 | 39 | capt1.readRedLight(r); |
guilhemMBED | 5:7a63cb7ec666 | 40 | capt1.readGreenLight(g); |
guilhemMBED | 5:7a63cb7ec666 | 41 | capt1.readBlueLight(b); |
guilhemMBED | 5:7a63cb7ec666 | 42 | capt1.readProximity(a); |
guilhemMBED | 5:7a63cb7ec666 | 43 | |
guilhemMBED | 5:7a63cb7ec666 | 44 | //display of red, green and proximty variables |
guilhemMBED | 5:7a63cb7ec666 | 45 | USB_link.printf("r: %hu g : %hu prox : %hu \r\n ",r,g,a); |
guilhemMBED | 3:f41f244de0b3 | 46 | |
guilhemMBED | 5:7a63cb7ec666 | 47 | if (a<100) { |
guilhemMBED | 5:7a63cb7ec666 | 48 | color = 0 ; // 0 means no object being measured |
guilhemMBED | 5:7a63cb7ec666 | 49 | } else if (r > g ) { |
guilhemMBED | 5:7a63cb7ec666 | 50 | color = 1 ; // 1 means red |
guilhemMBED | 5:7a63cb7ec666 | 51 | } else if (g > r ) { |
guilhemMBED | 5:7a63cb7ec666 | 52 | color = 2 ; // 2 means green |
guilhemMBED | 5:7a63cb7ec666 | 53 | } |
guilhemMBED | 5:7a63cb7ec666 | 54 | //can1.write(CANMessage(1200,&color,1)); |
guilhemMBED | 5:7a63cb7ec666 | 55 | |
guilhemMBED | 5:7a63cb7ec666 | 56 | // USB_link.printf("color : %c", color); |
guilhemMBED | 5:7a63cb7ec666 | 57 | } |
guilhemMBED | 3:f41f244de0b3 | 58 | |
guilhemMBED | 0:eace82fb39de | 59 | return 0; |
guilhemMBED | 1:7cd9426abb58 | 60 | } |