Divya Vadivel
/
Lab3_2
Lab 3 Part 2.2
Fork of ADCandticker_sample by
Diff: main.cpp
- Revision:
- 2:bb80af536422
- Parent:
- 1:126dd2f5fc2d
- Child:
- 3:b20239a6bd91
diff -r 126dd2f5fc2d -r bb80af536422 main.cpp --- a/main.cpp Wed Jan 24 21:55:43 2018 +0000 +++ b/main.cpp Fri Feb 09 23:34:47 2018 +0000 @@ -7,15 +7,58 @@ Ticker tick; // Ticker for reading analog AnalogIn ain(A0) ; // Analog input -DigitalOut led1(LED_RED); // Red LED +DigitalIn b1(D15, PullUp); +//DigitalOut led1(LED_RED); // Red LED +DigitalOut ledy(PTD5); +DigitalOut ledb(PTA13); +DigitalOut ledr(PTD0); +DigitalOut ledw(PTD2); +DigitalOut ledg(PTD3); +volatile int pressEvent = 0 ; +Serial pc(USBTX, USBRX); // tx, rx, for debugging + + -Serial pc(USBTX, USBRX); // tx, rx, for debugging +enum buttonPos { up, down, bounce }; // Button positions +void polling() { + buttonPos pos = up ;//chose positing in enum + 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); + } +} // Message type typedef struct { uint16_t analog; /* Analog input value */ } message_t; - +Thread pollT ; // thread to poll +void buttonCallback(){ + pressEvent = 1 ; +} // Mail box Mail<message_t, 2> mailbox; @@ -24,6 +67,7 @@ // Every 10th value is sent to mailbox volatile int samples = 0 ; volatile uint16_t smoothed = 0 ; + void readA0() { smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; samples++ ; @@ -53,20 +97,75 @@ // Attach ISR for ticker // Procss messages from mailbox int main() { - led1 = 1 ; // turn off + bool change_detect = false; + bool max_volt = false; + pollT.start(callback(polling));//start thread to check button int volts = 0 ; - const int threshold = 100 ; + int threshold[6] = {55,110,165,220,275,330} ; int counter = 0 ; char vstring[] = "X.XX\r\n" ; - tick.attach_us(callback(&readA0), 10000); // ticks every 10ms while (true) { + + if(pressEvent) + { + pressEvent=0 + max = !max_volt; + change_detect=false; + } osEvent evt = mailbox.get(); // wait for mail if (evt.status == osEventMail) { message_t* mess = (message_t*)evt.value.p ; volts = (mess->analog * 330) / 0xffff ; mailbox.free(mess) ; // free the message space - if (volts > threshold) led1 = 0 ; else led1 = 1 ; + + if(volts <=(threshold[0]) and max_volt=false){ + ledy = 0; + ledb = 0; + ledr = 0; + ledw = 0; + ledg = 0; + } + else if(volts >=(threshold[0])&&volts <=(threshold[1] and max_volt=false)){//1.1 && volts < 1.65){ + ledy = 1; + ledb = 0; + ledr = 0; + ledw = 0; + ledg = 0; + } + else if(volts >=(threshold[1])&&volts <=(threshold[2] and max_volt=false)){ + ledy = 1; + ledb = 1; + ledr = 0; + ledw = 0; + ledg = 0; + } + else if(volts >=(threshold[2])&&volts <=(threshold[3]) and max_volt=false){ + ledy = 1; + ledb = 1; + ledr = 1; + ledw = 0; + ledg = 0; + } + else if(volts >=(threshold[3])&&volts <=(threshold[4]) and max_volt=false){ + ledy = 1; + ledb = 1; + ledr = 1; + ledw = 1; + ledg = 0; + } + else if(volts >=(threshold[4])&&volts <=(threshold[5]) and max_volt=true){ + ledy = 1; + ledb = 1; + ledr = 1; + ledw = 1; + ledg = 1; + } + if(volts>=(threshold[5])){ + max_power=false; + + } + else{} vToString(volts, vstring) ; counter++ ; if (counter == 10) { // limit bandwidth of serial @@ -74,6 +173,5 @@ counter = 0 ; } } - } }