Mathias Riis
/
UltrasonicCasper
Porn
Fork of UltrasonicCasper by
main.cpp@2:39179781a18c, 2015-04-10 (annotated)
- Committer:
- CTxD
- Date:
- Fri Apr 10 23:54:59 2015 +0000
- Revision:
- 2:39179781a18c
- Parent:
- 1:a8d4271903ac
- Child:
- 3:bebb73b82a17
Ultrasonic Riiiiiis
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DavidMeza | 0:7eded57f1445 | 1 | #include "mbed.h" |
DavidMeza | 0:7eded57f1445 | 2 | #include "HCSR04.h" |
DavidMeza | 0:7eded57f1445 | 3 | |
CTxD | 2:39179781a18c | 4 | // Set's the Serial port |
oscargrodri | 1:a8d4271903ac | 5 | Serial pc(USBTX, USBRX); |
CTxD | 2:39179781a18c | 6 | |
CTxD | 2:39179781a18c | 7 | // Defines the LED colors |
DavidMeza | 0:7eded57f1445 | 8 | DigitalOut led(LED_RED); |
DavidMeza | 0:7eded57f1445 | 9 | DigitalOut led2(LED_GREEN); |
DavidMeza | 0:7eded57f1445 | 10 | |
CTxD | 2:39179781a18c | 11 | // Defines the sensors (Left and Right) |
CTxD | 2:39179781a18c | 12 | HCSR04 sensorLEFT(PTA12, PTD4); |
CTxD | 2:39179781a18c | 13 | HCSR04 sensorRIGHT(PTA4, PTA5); |
DavidMeza | 0:7eded57f1445 | 14 | |
CTxD | 2:39179781a18c | 15 | // Get the left distance variable |
CTxD | 2:39179781a18c | 16 | int distLeft(int dLEFT){ |
CTxD | 2:39179781a18c | 17 | return dLEFT; |
CTxD | 2:39179781a18c | 18 | } |
CTxD | 2:39179781a18c | 19 | |
CTxD | 2:39179781a18c | 20 | // Get the right distance variable |
CTxD | 2:39179781a18c | 21 | int distRight(int dRIGHT){ |
CTxD | 2:39179781a18c | 22 | return dRIGHT; |
CTxD | 2:39179781a18c | 23 | } |
CTxD | 2:39179781a18c | 24 | |
CTxD | 2:39179781a18c | 25 | |
CTxD | 2:39179781a18c | 26 | // The main() function |
DavidMeza | 0:7eded57f1445 | 27 | int main() |
DavidMeza | 0:7eded57f1445 | 28 | { |
CTxD | 2:39179781a18c | 29 | |
CTxD | 2:39179781a18c | 30 | // The loop() function |
oscargrodri | 1:a8d4271903ac | 31 | while(1) { |
CTxD | 2:39179781a18c | 32 | //left and right distance variables |
CTxD | 2:39179781a18c | 33 | int dLEFT = sensorLEFT.distance(CM); |
CTxD | 2:39179781a18c | 34 | int dRIGHT = sensorRIGHT.distance(CM); |
oscargrodri | 1:a8d4271903ac | 35 | |
CTxD | 2:39179781a18c | 36 | // Writes the left and right distance variable |
CTxD | 2:39179781a18c | 37 | pc.printf("SENSOR Left: %d \n\r\v",distLeft(dLEFT)); |
CTxD | 2:39179781a18c | 38 | pc.printf("SENSOR Right: %d \n\r\v",distRight(dRIGHT)); |
CTxD | 2:39179781a18c | 39 | |
CTxD | 2:39179781a18c | 40 | // Delay |
CTxD | 2:39179781a18c | 41 | wait(0.5); |
CTxD | 2:39179781a18c | 42 | |
CTxD | 2:39179781a18c | 43 | //LED control |
CTxD | 2:39179781a18c | 44 | if(dRIGHT<=10) { |
oscargrodri | 1:a8d4271903ac | 45 | led = 0; |
oscargrodri | 1:a8d4271903ac | 46 | led2 = 1; |
oscargrodri | 1:a8d4271903ac | 47 | } |
oscargrodri | 1:a8d4271903ac | 48 | |
CTxD | 2:39179781a18c | 49 | if(dRIGHT>=40) { |
oscargrodri | 1:a8d4271903ac | 50 | led2 = 0; |
oscargrodri | 1:a8d4271903ac | 51 | led = 1; |
oscargrodri | 1:a8d4271903ac | 52 | } |
CTxD | 2:39179781a18c | 53 | if(dRIGHT>=11 && dRIGHT<=39) { |
oscargrodri | 1:a8d4271903ac | 54 | led2 = 0; |
oscargrodri | 1:a8d4271903ac | 55 | led=0; |
oscargrodri | 1:a8d4271903ac | 56 | } |
oscargrodri | 1:a8d4271903ac | 57 | } |
CTxD | 2:39179781a18c | 58 | |
oscargrodri | 1:a8d4271903ac | 59 | } |
oscargrodri | 1:a8d4271903ac | 60 | |
oscargrodri | 1:a8d4271903ac | 61 | |
oscargrodri | 1:a8d4271903ac | 62 | |
oscargrodri | 1:a8d4271903ac | 63 | |
CTxD | 2:39179781a18c | 64 |