Etch a Sketch on the uLCD screen with wheel encoders as the knobs.
Dependencies: 4DGL-uLCD-SE mbed
main.cpp@1:876e9cc9088b, 2017-03-13 (annotated)
- Committer:
- simplyellow
- Date:
- Mon Mar 13 22:27:09 2017 +0000
- Revision:
- 1:876e9cc9088b
- Parent:
- 0:c1245c18d07e
- Child:
- 2:8752f5af08a4
both wheels with functional encoder. note to keep hall effect sensors fairly close to the wheels at all times.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simplyellow | 0:c1245c18d07e | 1 | #include "mbed.h" |
simplyellow | 0:c1245c18d07e | 2 | |
simplyellow | 0:c1245c18d07e | 3 | //#include "uLCD_4DGL.h" |
simplyellow | 0:c1245c18d07e | 4 | |
simplyellow | 0:c1245c18d07e | 5 | Serial pc(USBTX, USBRX); |
simplyellow | 0:c1245c18d07e | 6 | |
simplyellow | 1:876e9cc9088b | 7 | InterruptIn leftchA(p26); |
simplyellow | 1:876e9cc9088b | 8 | InterruptIn leftchB(p25); |
simplyellow | 1:876e9cc9088b | 9 | int leftChanA; |
simplyellow | 1:876e9cc9088b | 10 | int leftChanB; |
simplyellow | 1:876e9cc9088b | 11 | int leftCurrState; |
simplyellow | 1:876e9cc9088b | 12 | int leftPrevState; |
simplyellow | 1:876e9cc9088b | 13 | int leftChange; |
simplyellow | 0:c1245c18d07e | 14 | |
simplyellow | 1:876e9cc9088b | 15 | InterruptIn rightchA(p13); |
simplyellow | 1:876e9cc9088b | 16 | InterruptIn rightchB(p14); |
simplyellow | 1:876e9cc9088b | 17 | int rightChanA; |
simplyellow | 1:876e9cc9088b | 18 | int rightChanB; |
simplyellow | 1:876e9cc9088b | 19 | int rightCurrState; |
simplyellow | 1:876e9cc9088b | 20 | int rightPrevState; |
simplyellow | 1:876e9cc9088b | 21 | int rightChange; |
simplyellow | 1:876e9cc9088b | 22 | |
simplyellow | 1:876e9cc9088b | 23 | volatile int count1 = 0; |
simplyellow | 1:876e9cc9088b | 24 | volatile int count2 = 0; |
simplyellow | 0:c1245c18d07e | 25 | |
simplyellow | 0:c1245c18d07e | 26 | //uLCD_4GDL uLCD(p9,p10,p11); |
simplyellow | 0:c1245c18d07e | 27 | InterruptIn pb(p21); |
simplyellow | 0:c1245c18d07e | 28 | DigitalOut led(LED1); |
simplyellow | 0:c1245c18d07e | 29 | |
simplyellow | 0:c1245c18d07e | 30 | |
simplyellow | 0:c1245c18d07e | 31 | /* |
simplyellow | 0:c1245c18d07e | 32 | Implemented barebones version of "QEI.h" by Aaron Berk for the sake of |
simplyellow | 0:c1245c18d07e | 33 | understanding the concept of X4 encoding |
simplyellow | 0:c1245c18d07e | 34 | 00 01 11 10 00 |
simplyellow | 0:c1245c18d07e | 35 | 4 different states |
simplyellow | 0:c1245c18d07e | 36 | */ |
simplyellow | 0:c1245c18d07e | 37 | |
simplyellow | 1:876e9cc9088b | 38 | void encode1() { |
simplyellow | 1:876e9cc9088b | 39 | leftChange = 0; |
simplyellow | 1:876e9cc9088b | 40 | leftChanA = leftchA.read(); |
simplyellow | 1:876e9cc9088b | 41 | leftChanB = leftchB.read(); |
simplyellow | 1:876e9cc9088b | 42 | leftCurrState = (leftChanA << 1) | (leftChanB); |
simplyellow | 1:876e9cc9088b | 43 | if (((leftCurrState ^ leftPrevState) != 0x3) && (leftCurrState != leftPrevState)) { |
simplyellow | 1:876e9cc9088b | 44 | // example 01 <- 11 |
simplyellow | 1:876e9cc9088b | 45 | // change = (11&01 = 01) ^ (01&10 = 00>>1) = 1 |
simplyellow | 1:876e9cc9088b | 46 | // example 10 -> 00 |
simplyellow | 1:876e9cc9088b | 47 | // change = (10&01 = 00) ^ (00&10 = 00>>1) = 0 |
simplyellow | 1:876e9cc9088b | 48 | // change = (if(left) ^ |
simplyellow | 1:876e9cc9088b | 49 | |
simplyellow | 1:876e9cc9088b | 50 | leftChange = (leftPrevState & 0x1) ^ ((leftCurrState & 0x2) >> 1); |
simplyellow | 1:876e9cc9088b | 51 | if (leftChange == 0) { |
simplyellow | 1:876e9cc9088b | 52 | leftChange = -1; |
simplyellow | 1:876e9cc9088b | 53 | } |
simplyellow | 1:876e9cc9088b | 54 | count1 += leftChange; |
simplyellow | 1:876e9cc9088b | 55 | } |
simplyellow | 1:876e9cc9088b | 56 | leftPrevState = leftCurrState; |
simplyellow | 1:876e9cc9088b | 57 | } |
simplyellow | 1:876e9cc9088b | 58 | |
simplyellow | 1:876e9cc9088b | 59 | void encode2() { |
simplyellow | 1:876e9cc9088b | 60 | rightChange = 0; |
simplyellow | 1:876e9cc9088b | 61 | rightChanA = rightchA.read(); |
simplyellow | 1:876e9cc9088b | 62 | rightChanB = rightchB.read(); |
simplyellow | 1:876e9cc9088b | 63 | rightCurrState = (rightChanA << 1) | (rightChanB); |
simplyellow | 1:876e9cc9088b | 64 | if (((rightCurrState ^ rightPrevState) != 0x3) && (rightCurrState != rightPrevState)) { |
simplyellow | 0:c1245c18d07e | 65 | // example 01 <- 11 |
simplyellow | 0:c1245c18d07e | 66 | // change = (11&01 = 01) ^ (01&10 = 00>>1) = 1 |
simplyellow | 0:c1245c18d07e | 67 | // example 10 -> 00 |
simplyellow | 0:c1245c18d07e | 68 | // change = (10&01 = 00) ^ (00&10 = 00>>1) = 0 |
simplyellow | 0:c1245c18d07e | 69 | // change = (if(left) ^ |
simplyellow | 0:c1245c18d07e | 70 | |
simplyellow | 1:876e9cc9088b | 71 | rightChange = (rightPrevState & 0x1) ^ ((rightCurrState & 0x2) >> 1); |
simplyellow | 1:876e9cc9088b | 72 | if (rightChange == 0) { |
simplyellow | 1:876e9cc9088b | 73 | rightChange = -1; |
simplyellow | 0:c1245c18d07e | 74 | } |
simplyellow | 1:876e9cc9088b | 75 | count2 += rightChange; |
simplyellow | 0:c1245c18d07e | 76 | } |
simplyellow | 1:876e9cc9088b | 77 | rightPrevState = rightCurrState; |
simplyellow | 0:c1245c18d07e | 78 | } |
simplyellow | 1:876e9cc9088b | 79 | |
simplyellow | 1:876e9cc9088b | 80 | |
simplyellow | 0:c1245c18d07e | 81 | void clear() { |
simplyellow | 0:c1245c18d07e | 82 | //uLCD.cls(); |
simplyellow | 0:c1245c18d07e | 83 | led = !led; |
simplyellow | 0:c1245c18d07e | 84 | } |
simplyellow | 0:c1245c18d07e | 85 | |
simplyellow | 1:876e9cc9088b | 86 | void setupEncoder() { |
simplyellow | 1:876e9cc9088b | 87 | leftchA.mode(PullUp); |
simplyellow | 1:876e9cc9088b | 88 | leftchB.mode(PullUp); |
simplyellow | 1:876e9cc9088b | 89 | rightchA.mode(PullUp); |
simplyellow | 1:876e9cc9088b | 90 | rightchB.mode(PullUp); |
simplyellow | 1:876e9cc9088b | 91 | |
simplyellow | 1:876e9cc9088b | 92 | leftChanA = leftchA.read(); |
simplyellow | 1:876e9cc9088b | 93 | leftChanB = leftchB.read(); |
simplyellow | 1:876e9cc9088b | 94 | rightChanA = rightchA.read(); |
simplyellow | 1:876e9cc9088b | 95 | rightChanB = rightchB.read(); |
simplyellow | 0:c1245c18d07e | 96 | |
simplyellow | 1:876e9cc9088b | 97 | leftCurrState = (leftChanA << 1) | (leftChanB); |
simplyellow | 1:876e9cc9088b | 98 | leftPrevState = leftCurrState; |
simplyellow | 1:876e9cc9088b | 99 | rightCurrState = (rightChanA << 1) | (rightChanB); |
simplyellow | 1:876e9cc9088b | 100 | rightPrevState = rightCurrState; |
simplyellow | 0:c1245c18d07e | 101 | |
simplyellow | 1:876e9cc9088b | 102 | leftchA.rise(&encode1); |
simplyellow | 1:876e9cc9088b | 103 | leftchA.fall(&encode1); |
simplyellow | 1:876e9cc9088b | 104 | leftchB.rise(&encode1); |
simplyellow | 1:876e9cc9088b | 105 | leftchB.fall(&encode1); |
simplyellow | 0:c1245c18d07e | 106 | |
simplyellow | 1:876e9cc9088b | 107 | rightchA.rise(&encode2); |
simplyellow | 1:876e9cc9088b | 108 | rightchA.fall(&encode2); |
simplyellow | 1:876e9cc9088b | 109 | rightchB.rise(&encode2); |
simplyellow | 1:876e9cc9088b | 110 | rightchB.fall(&encode2); |
simplyellow | 1:876e9cc9088b | 111 | } |
simplyellow | 1:876e9cc9088b | 112 | |
simplyellow | 1:876e9cc9088b | 113 | int main() { |
simplyellow | 1:876e9cc9088b | 114 | setupEncoder(); |
simplyellow | 0:c1245c18d07e | 115 | pb.mode(PullUp); |
simplyellow | 0:c1245c18d07e | 116 | pb.fall(&clear); //pb to clear screen |
simplyellow | 0:c1245c18d07e | 117 | //add a pb to turn on and off pen |
simplyellow | 0:c1245c18d07e | 118 | //add a pot to go through colors |
simplyellow | 0:c1245c18d07e | 119 | //have something display current color and RGB val |
simplyellow | 0:c1245c18d07e | 120 | while(1) { |
simplyellow | 0:c1245c18d07e | 121 | pc.printf("count1: %d\r\n", count1); |
simplyellow | 1:876e9cc9088b | 122 | pc.printf("count2: %d\r\n", count2); |
simplyellow | 0:c1245c18d07e | 123 | pc.printf("---------\r\n"); |
simplyellow | 0:c1245c18d07e | 124 | wait(.1); |
simplyellow | 0:c1245c18d07e | 125 | } |
simplyellow | 0:c1245c18d07e | 126 | } |