![](/media/cache/group/Embedded_software_in_context_icon.png.50x50_q85.png)
Task 4.3.6 Solution
Dependencies: mbed
main.cpp@0:315c40dcb63f, 2015-09-24 (annotated)
- Committer:
- noutram
- Date:
- Thu Sep 24 12:30:54 2015 +0000
- Revision:
- 0:315c40dcb63f
Initial version 24-09-2015
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:315c40dcb63f | 1 | #include "mbed.h" |
noutram | 0:315c40dcb63f | 2 | |
noutram | 0:315c40dcb63f | 3 | //Constants |
noutram | 0:315c40dcb63f | 4 | #define N 10 |
noutram | 0:315c40dcb63f | 5 | |
noutram | 0:315c40dcb63f | 6 | //Function Prototype |
noutram | 0:315c40dcb63f | 7 | void doCaptureSamples(); |
noutram | 0:315c40dcb63f | 8 | float updateAverage(float newSample, float buffer[]); |
noutram | 0:315c40dcb63f | 9 | |
noutram | 0:315c40dcb63f | 10 | //Global objects |
noutram | 0:315c40dcb63f | 11 | Serial pc(USBTX, USBRX); |
noutram | 0:315c40dcb63f | 12 | |
noutram | 0:315c40dcb63f | 13 | BusOut binaryOutput(D5, D6, D7); |
noutram | 0:315c40dcb63f | 14 | AnalogIn POT_ADC_In(A0); |
noutram | 0:315c40dcb63f | 15 | AnalogIn LDR_ADC_In(A1); |
noutram | 0:315c40dcb63f | 16 | DigitalOut strobe(D8); |
noutram | 0:315c40dcb63f | 17 | |
noutram | 0:315c40dcb63f | 18 | float xPOT[N]; //Aray of potentiometer samples |
noutram | 0:315c40dcb63f | 19 | float xLDR[N]; //Aray of LDR samples |
noutram | 0:315c40dcb63f | 20 | |
noutram | 0:315c40dcb63f | 21 | //These variables hold the next sample |
noutram | 0:315c40dcb63f | 22 | static float nextLDRSample, nextPOTSample; |
noutram | 0:315c40dcb63f | 23 | |
noutram | 0:315c40dcb63f | 24 | //The location of the oldest sample |
noutram | 0:315c40dcb63f | 25 | static int indexOfOldest=(N-1); |
noutram | 0:315c40dcb63f | 26 | |
noutram | 0:315c40dcb63f | 27 | //These variables hold the average values |
noutram | 0:315c40dcb63f | 28 | float fPOTSum = 0.0; |
noutram | 0:315c40dcb63f | 29 | float fLDRSum = 0.0; |
noutram | 0:315c40dcb63f | 30 | |
noutram | 0:315c40dcb63f | 31 | //The ticker, used to sample data at a fixed rate |
noutram | 0:315c40dcb63f | 32 | Ticker t; |
noutram | 0:315c40dcb63f | 33 | |
noutram | 0:315c40dcb63f | 34 | //Main function |
noutram | 0:315c40dcb63f | 35 | int main() |
noutram | 0:315c40dcb63f | 36 | { |
noutram | 0:315c40dcb63f | 37 | //Set baud rate to 115200 |
noutram | 0:315c40dcb63f | 38 | pc.baud(115200); |
noutram | 0:315c40dcb63f | 39 | |
noutram | 0:315c40dcb63f | 40 | //Print header |
noutram | 0:315c40dcb63f | 41 | pc.printf("POT,LDR,avPOT, acLDR\n\n"); |
noutram | 0:315c40dcb63f | 42 | |
noutram | 0:315c40dcb63f | 43 | //Set up the ticker - 100Hz |
noutram | 0:315c40dcb63f | 44 | t.attach(doCaptureSamples, 0.01); |
noutram | 0:315c40dcb63f | 45 | |
noutram | 0:315c40dcb63f | 46 | while(1) { |
noutram | 0:315c40dcb63f | 47 | |
noutram | 0:315c40dcb63f | 48 | //Sleep |
noutram | 0:315c40dcb63f | 49 | sleep(); |
noutram | 0:315c40dcb63f | 50 | |
noutram | 0:315c40dcb63f | 51 | //Update sums |
noutram | 0:315c40dcb63f | 52 | fPOTSum = fPOTSum + nextPOTSample - xPOT[indexOfOldest]; |
noutram | 0:315c40dcb63f | 53 | fLDRSum = fLDRSum + nextLDRSample - xLDR[indexOfOldest]; |
noutram | 0:315c40dcb63f | 54 | |
noutram | 0:315c40dcb63f | 55 | //Write new sample over the oldest |
noutram | 0:315c40dcb63f | 56 | xPOT[indexOfOldest] = nextPOTSample; |
noutram | 0:315c40dcb63f | 57 | xLDR[indexOfOldest] = nextLDRSample; |
noutram | 0:315c40dcb63f | 58 | |
noutram | 0:315c40dcb63f | 59 | //update positon of oldest sample |
noutram | 0:315c40dcb63f | 60 | indexOfOldest = (indexOfOldest==0) ? (N-1) : indexOfOldest-1; |
noutram | 0:315c40dcb63f | 61 | |
noutram | 0:315c40dcb63f | 62 | //Write to terminal via Serial interface |
noutram | 0:315c40dcb63f | 63 | int middle = (indexOfOldest - (N/2)) % N; |
noutram | 0:315c40dcb63f | 64 | pc.printf("%6.4f,%6.4f,%6.4f,%6.4f\n", xPOT[middle], xLDR[middle], fPOTSum/(float)N, fLDRSum/(float)N); |
noutram | 0:315c40dcb63f | 65 | |
noutram | 0:315c40dcb63f | 66 | //Check the threshold |
noutram | 0:315c40dcb63f | 67 | if (fLDRSum > fPOTSum) { |
noutram | 0:315c40dcb63f | 68 | binaryOutput = 0; //Binary 000 |
noutram | 0:315c40dcb63f | 69 | } else { |
noutram | 0:315c40dcb63f | 70 | binaryOutput = 7; //Binary 111 |
noutram | 0:315c40dcb63f | 71 | } |
noutram | 0:315c40dcb63f | 72 | |
noutram | 0:315c40dcb63f | 73 | } //end while(1) |
noutram | 0:315c40dcb63f | 74 | } //end main |
noutram | 0:315c40dcb63f | 75 | |
noutram | 0:315c40dcb63f | 76 | //ISR for the ticker - simply there to perform sampling |
noutram | 0:315c40dcb63f | 77 | void doCaptureSamples() |
noutram | 0:315c40dcb63f | 78 | { |
noutram | 0:315c40dcb63f | 79 | //Toggle digital out |
noutram | 0:315c40dcb63f | 80 | strobe = !strobe; |
noutram | 0:315c40dcb63f | 81 | |
noutram | 0:315c40dcb63f | 82 | //READ ADC - store in static globals |
noutram | 0:315c40dcb63f | 83 | //(static globals are shared with all functions in the same C or CPP file) |
noutram | 0:315c40dcb63f | 84 | nextLDRSample = LDR_ADC_In; |
noutram | 0:315c40dcb63f | 85 | nextPOTSample = POT_ADC_In; |
noutram | 0:315c40dcb63f | 86 | } |
noutram | 0:315c40dcb63f | 87 |