Tobore Denedo
/
ADCandticker_program_Tobore
Lab 3 ADC code
Fork of ADCandticker_sample by
Revision 2:689f31376bb3, committed 2018-02-09
- Comitter:
- Tobden
- Date:
- Fri Feb 09 21:39:03 2018 +0000
- Parent:
- 1:126dd2f5fc2d
- Commit message:
- ADCandTicker
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Jan 24 21:55:43 2018 +0000 +++ b/main.cpp Fri Feb 09 21:39:03 2018 +0000 @@ -7,7 +7,48 @@ Ticker tick; // Ticker for reading analog AnalogIn ain(A0) ; // Analog input -DigitalOut led1(LED_RED); // Red LED +DigitalOut led1(D8); // LED 1 +DigitalOut led2(D9); //LED 2 +DigitalOut led3(D10); //LED 3 +DigitalOut led4(D11); //LED 4 +DigitalOut led5(D12); //LED 5 + +DigitalIn b1(PTE1, PullUp); //button + +Thread pollT ; // thread to poll +volatile int pressEvent = 0 ; // Variabe set by the polling thread + +enum buttonPos { up, down, bounce }; // Button positions +void polling() { + buttonPos pos = up ; + int bcounter = 0 ; + while (true) { + switch (pos) { + case up : + if (!b1.read()) { // now down + pressEvent = 1 ; // transition occurred + pos = down ; + } + break ; + case down : + if (b1 == 1) { // no longer down + bcounter = 3 ; // wait four cycles + pos = bounce ; + } + break ; + case bounce : + if (b1 == 0) { // down again - button has bounced + pos = down ; // no event + } else if (bcounter == 0) { + pos = up ; // delay passed - reset to up + } else { + bcounter-- ; // continue waiting + } + break ; + } + Thread::wait(30); + } +} Serial pc(USBTX, USBRX); // tx, rx, for debugging @@ -53,9 +94,18 @@ // Attach ISR for ticker // Procss messages from mailbox int main() { - led1 = 1 ; // turn off + + + pollT.start(callback(polling)); +int firstled; +int secondled; +int thirdled; +int fourthled; +int fifthled; +volatile int max = 330; //initial max voltage level required to light up the last LED + int volts = 0 ; - const int threshold = 100 ; + int counter = 0 ; char vstring[] = "X.XX\r\n" ; @@ -65,8 +115,24 @@ if (evt.status == osEventMail) { message_t* mess = (message_t*)evt.value.p ; volts = (mess->analog * 330) / 0xffff ; + if (pressEvent) { + pressEvent = 0 ; // clear the event variable + max=volts ; //set the present voltage as the new maximum voltage to light up the last LED + } mailbox.free(mess) ; // free the message space - if (volts > threshold) led1 = 0 ; else led1 = 1 ; + +firstled = max*2/6; //voltage required to light up the first led +secondled = max*3/6; //voltage required to light up the second led +thirdled = max*4/6; //voltage required to light up the third led +fourthled = max*5/6; //voltage required to light up the fourth led +fifthled = max; //voltage required to light up the fifth led + + if (volts >= firstled) led1 = 1 ; else led1 = 0 ; + if (volts >= secondled) led2 = 1; else led2 = 0; + if (volts >=thirdled) led3 = 1; else led3 = 0; + if (volts >= fourthled) led4 = 1; else led4 = 0; + if (volts >= fifthled) led5 = 1; else led5 = 0; + vToString(volts, vstring) ; counter++ ; if (counter == 10) { // limit bandwidth of serial