Stanley Cohen
/
AnalogMax_v2
example to read an analog input ultrasonic sensor.
amaxmain2.cpp@0:f2441ea29233, 2015-04-22 (annotated)
- Committer:
- scohennm
- Date:
- Wed Apr 22 18:07:44 2015 +0000
- Revision:
- 0:f2441ea29233
example to read an analog input ultrasonic sensor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scohennm | 0:f2441ea29233 | 1 | #include "mbed.h" |
scohennm | 0:f2441ea29233 | 2 | |
scohennm | 0:f2441ea29233 | 3 | //#define PRINTDBUG |
scohennm | 0:f2441ea29233 | 4 | #define WAITTIME 100 |
scohennm | 0:f2441ea29233 | 5 | #define NEARLIMIT 0.025 |
scohennm | 0:f2441ea29233 | 6 | #define FARLIMIT 0.40 |
scohennm | 0:f2441ea29233 | 7 | #define LEDON false |
scohennm | 0:f2441ea29233 | 8 | #define LEDOFF true |
scohennm | 0:f2441ea29233 | 9 | #define LEDFULLBRITE 0.0 |
scohennm | 0:f2441ea29233 | 10 | #define LEDNOBRITE 1.0 |
scohennm | 0:f2441ea29233 | 11 | #define HALFBRITE 0.5 |
scohennm | 0:f2441ea29233 | 12 | #define PROGNAME "AnalogMAX v2\r\n" // puTTY does not automatically add carriage return |
scohennm | 0:f2441ea29233 | 13 | |
scohennm | 0:f2441ea29233 | 14 | AnalogIn input(PTC2); |
scohennm | 0:f2441ea29233 | 15 | // Set up PWM on all three colors |
scohennm | 0:f2441ea29233 | 16 | PwmOut greenLED(LED_GREEN); // green led |
scohennm | 0:f2441ea29233 | 17 | PwmOut blueLED(LED_BLUE); |
scohennm | 0:f2441ea29233 | 18 | PwmOut redLED(LED_RED); // red led |
scohennm | 0:f2441ea29233 | 19 | Serial pc(USBTX, USBRX); |
scohennm | 0:f2441ea29233 | 20 | Timer millis; |
scohennm | 0:f2441ea29233 | 21 | |
scohennm | 0:f2441ea29233 | 22 | int ledState = false; |
scohennm | 0:f2441ea29233 | 23 | |
scohennm | 0:f2441ea29233 | 24 | void takeData(float spanScale) { |
scohennm | 0:f2441ea29233 | 25 | |
scohennm | 0:f2441ea29233 | 26 | float samples; |
scohennm | 0:f2441ea29233 | 27 | float scaledPWM; |
scohennm | 0:f2441ea29233 | 28 | |
scohennm | 0:f2441ea29233 | 29 | samples = input.read(); |
scohennm | 0:f2441ea29233 | 30 | #ifdef PRINTDBUG |
scohennm | 0:f2441ea29233 | 31 | pc.printf("%f\r\n", samples); |
scohennm | 0:f2441ea29233 | 32 | #endif |
scohennm | 0:f2441ea29233 | 33 | if ((samples > NEARLIMIT) && (samples < FARLIMIT)){ |
scohennm | 0:f2441ea29233 | 34 | scaledPWM = (samples - NEARLIMIT)*spanScale; |
scohennm | 0:f2441ea29233 | 35 | redLED = 1.0 - scaledPWM; |
scohennm | 0:f2441ea29233 | 36 | // greenLED = LEDOFF; // on |
scohennm | 0:f2441ea29233 | 37 | blueLED = scaledPWM; |
scohennm | 0:f2441ea29233 | 38 | #ifdef PRINTDBUG |
scohennm | 0:f2441ea29233 | 39 | pc.printf("RED -> %f BLUE -> %f\r\n", redLED.read(), blueLED.read()); |
scohennm | 0:f2441ea29233 | 40 | #endif |
scohennm | 0:f2441ea29233 | 41 | }else{ |
scohennm | 0:f2441ea29233 | 42 | redLED = LEDNOBRITE; // off |
scohennm | 0:f2441ea29233 | 43 | // greenLED = HALFBRITE; |
scohennm | 0:f2441ea29233 | 44 | blueLED = LEDNOBRITE; |
scohennm | 0:f2441ea29233 | 45 | } |
scohennm | 0:f2441ea29233 | 46 | return; |
scohennm | 0:f2441ea29233 | 47 | } |
scohennm | 0:f2441ea29233 | 48 | // end takeData() |
scohennm | 0:f2441ea29233 | 49 | // ---------------------------- |
scohennm | 0:f2441ea29233 | 50 | int main() { |
scohennm | 0:f2441ea29233 | 51 | float spanScale; |
scohennm | 0:f2441ea29233 | 52 | |
scohennm | 0:f2441ea29233 | 53 | // initialization |
scohennm | 0:f2441ea29233 | 54 | // calulate scaling for red/blue LED |
scohennm | 0:f2441ea29233 | 55 | |
scohennm | 0:f2441ea29233 | 56 | |
scohennm | 0:f2441ea29233 | 57 | spanScale = 1.0/(FARLIMIT-NEARLIMIT); |
scohennm | 0:f2441ea29233 | 58 | pc.printf(PROGNAME); |
scohennm | 0:f2441ea29233 | 59 | |
scohennm | 0:f2441ea29233 | 60 | greenLED = HALFBRITE; // 1/2 on |
scohennm | 0:f2441ea29233 | 61 | redLED = LEDNOBRITE; // pwm off |
scohennm | 0:f2441ea29233 | 62 | blueLED = LEDFULLBRITE; // pwm on |
scohennm | 0:f2441ea29233 | 63 | millis.start(); |
scohennm | 0:f2441ea29233 | 64 | millis.reset(); |
scohennm | 0:f2441ea29233 | 65 | // Loop forever |
scohennm | 0:f2441ea29233 | 66 | while(true){ |
scohennm | 0:f2441ea29233 | 67 | if (millis.read_ms() > WAITTIME) { |
scohennm | 0:f2441ea29233 | 68 | takeData(spanScale); |
scohennm | 0:f2441ea29233 | 69 | millis.reset(); |
scohennm | 0:f2441ea29233 | 70 | } |
scohennm | 0:f2441ea29233 | 71 | } |
scohennm | 0:f2441ea29233 | 72 | |
scohennm | 0:f2441ea29233 | 73 | } // end main |