Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:f81b40e04a58, 2015-04-20 (annotated)
- Committer:
- tyleralt
- Date:
- Mon Apr 20 19:55:59 2015 +0000
- Revision:
- 4:f81b40e04a58
- Parent:
- 3:1e9b4a4bf177
- Child:
- 5:6d8f9165021e
this ish works. All tested out
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tyleralt | 0:d9730cf42ecc | 1 | #include "mbed.h" |
tyleralt | 1:ea12b9153c51 | 2 | #include <vector> |
tyleralt | 0:d9730cf42ecc | 3 | #define BUFFER_SIZE 16 |
tyleralt | 0:d9730cf42ecc | 4 | #define NUMBER_OF_SLICES 90 |
tyleralt | 0:d9730cf42ecc | 5 | |
tyleralt | 0:d9730cf42ecc | 6 | //DigitalOut Pins |
tyleralt | 0:d9730cf42ecc | 7 | DigitalOut pushRegister(p23); |
tyleralt | 0:d9730cf42ecc | 8 | DigitalOut pushBit(p24); |
tyleralt | 0:d9730cf42ecc | 9 | |
tyleralt | 0:d9730cf42ecc | 10 | DigitalOut dataArmOne(p15); |
tyleralt | 0:d9730cf42ecc | 11 | DigitalOut dataArmTwo(p16); |
tyleralt | 0:d9730cf42ecc | 12 | DigitalOut dataArmThree(p17); |
tyleralt | 0:d9730cf42ecc | 13 | DigitalOut dataArmFour(p18); |
tyleralt | 0:d9730cf42ecc | 14 | DigitalOut dataArmFive(p19); |
tyleralt | 0:d9730cf42ecc | 15 | DigitalOut dataArmSix(p20); |
tyleralt | 0:d9730cf42ecc | 16 | DigitalOut dataArmSeven(p21); |
tyleralt | 0:d9730cf42ecc | 17 | DigitalOut dataArmEight(p22); |
tyleralt | 2:554edc4b0cf2 | 18 | DigitalOut led(LED1); |
tyleralt | 0:d9730cf42ecc | 19 | |
tyleralt | 0:d9730cf42ecc | 20 | //Enable Interupt |
tyleralt | 0:d9730cf42ecc | 21 | InterruptIn hallSensor(p25); |
tyleralt | 0:d9730cf42ecc | 22 | |
tyleralt | 1:ea12b9153c51 | 23 | //Set Up Timer and ticker |
tyleralt | 0:d9730cf42ecc | 24 | Timer rotationTime; |
tyleralt | 1:ea12b9153c51 | 25 | Ticker updateLeds; |
tyleralt | 0:d9730cf42ecc | 26 | |
tyleralt | 0:d9730cf42ecc | 27 | //Declare global vars |
tyleralt | 2:554edc4b0cf2 | 28 | double slice_time; |
tyleralt | 2:554edc4b0cf2 | 29 | double rotate_time; |
tyleralt | 0:d9730cf42ecc | 30 | int current_slice; |
tyleralt | 4:f81b40e04a58 | 31 | char slice_data [NUMBER_OF_SLICES][16]; //[slice][specific led distance] (0 is closest) & with approppriate bit for each arm |
tyleralt | 2:554edc4b0cf2 | 32 | double firstTime; |
tyleralt | 0:d9730cf42ecc | 33 | |
tyleralt | 0:d9730cf42ecc | 34 | Serial pc(USBTX, USBRX); // tx, rx |
tyleralt | 3:1e9b4a4bf177 | 35 | Serial bt(p9, p10);// tx, rx |
tyleralt | 0:d9730cf42ecc | 36 | |
tyleralt | 0:d9730cf42ecc | 37 | |
tyleralt | 4:f81b40e04a58 | 38 | void pushData (char bits [16]){ |
tyleralt | 4:f81b40e04a58 | 39 | //pc.printf("got here"); |
tyleralt | 4:f81b40e04a58 | 40 | //for (int i = 0; i < 16; i ++){ |
tyleralt | 4:f81b40e04a58 | 41 | // pc.printf("%i %u ", i, bits[i]); |
tyleralt | 4:f81b40e04a58 | 42 | // } |
tyleralt | 4:f81b40e04a58 | 43 | for (int i = 8; i < 16; i ++){ |
tyleralt | 4:f81b40e04a58 | 44 | dataArmOne = bits [i] & 0x01; |
tyleralt | 4:f81b40e04a58 | 45 | dataArmTwo = bits [i] & 0x02; |
tyleralt | 4:f81b40e04a58 | 46 | dataArmThree = bits [i]& 0x04; |
tyleralt | 4:f81b40e04a58 | 47 | dataArmFour = bits [i]& 0x08; |
tyleralt | 4:f81b40e04a58 | 48 | dataArmFive = bits [i] & 0x10; |
tyleralt | 4:f81b40e04a58 | 49 | dataArmSix = bits [i] & 0x20; |
tyleralt | 4:f81b40e04a58 | 50 | dataArmSeven = bits [i] & 0x40; |
tyleralt | 4:f81b40e04a58 | 51 | dataArmEight = bits [i] & 0x80; |
tyleralt | 1:ea12b9153c51 | 52 | |
tyleralt | 1:ea12b9153c51 | 53 | pushBit = 1; |
tyleralt | 1:ea12b9153c51 | 54 | pushBit = 0; |
tyleralt | 1:ea12b9153c51 | 55 | } |
tyleralt | 4:f81b40e04a58 | 56 | for (int i = 7; i >= 0; i --){ |
tyleralt | 4:f81b40e04a58 | 57 | dataArmOne = bits [i] & 0x01; |
tyleralt | 4:f81b40e04a58 | 58 | dataArmTwo = bits [i] & 0x02; |
tyleralt | 4:f81b40e04a58 | 59 | dataArmThree = bits [i]& 0x04; |
tyleralt | 4:f81b40e04a58 | 60 | dataArmFour = bits [i]& 0x08; |
tyleralt | 4:f81b40e04a58 | 61 | dataArmFive = bits [i] & 0x10; |
tyleralt | 4:f81b40e04a58 | 62 | dataArmSix = bits [i] & 0x20; |
tyleralt | 4:f81b40e04a58 | 63 | dataArmSeven = bits [i] & 0x40; |
tyleralt | 4:f81b40e04a58 | 64 | dataArmEight = bits [i] & 0x80; |
tyleralt | 4:f81b40e04a58 | 65 | |
tyleralt | 4:f81b40e04a58 | 66 | pushBit = 1; |
tyleralt | 4:f81b40e04a58 | 67 | pushBit = 0; |
tyleralt | 4:f81b40e04a58 | 68 | } |
tyleralt | 4:f81b40e04a58 | 69 | |
tyleralt | 4:f81b40e04a58 | 70 | pushRegister = 1; |
tyleralt | 4:f81b40e04a58 | 71 | pushRegister = 0; |
tyleralt | 1:ea12b9153c51 | 72 | } |
tyleralt | 1:ea12b9153c51 | 73 | |
tyleralt | 1:ea12b9153c51 | 74 | |
tyleralt | 1:ea12b9153c51 | 75 | //periodic display interrupt |
tyleralt | 1:ea12b9153c51 | 76 | void nextLedPush(){ |
tyleralt | 1:ea12b9153c51 | 77 | if (current_slice < NUMBER_OF_SLICES){ |
tyleralt | 4:f81b40e04a58 | 78 | pushData(slice_data[current_slice]); |
tyleralt | 1:ea12b9153c51 | 79 | current_slice ++; |
tyleralt | 2:554edc4b0cf2 | 80 | } else { |
tyleralt | 2:554edc4b0cf2 | 81 | updateLeds.detach(); |
tyleralt | 1:ea12b9153c51 | 82 | } |
tyleralt | 1:ea12b9153c51 | 83 | } |
tyleralt | 1:ea12b9153c51 | 84 | |
tyleralt | 0:d9730cf42ecc | 85 | //Hall sensor interupt |
tyleralt | 0:d9730cf42ecc | 86 | void rotate_sense(){ |
tyleralt | 3:1e9b4a4bf177 | 87 | pc.printf("interupt"); |
tyleralt | 0:d9730cf42ecc | 88 | if (firstTime){ |
tyleralt | 0:d9730cf42ecc | 89 | rotationTime.reset(); |
tyleralt | 0:d9730cf42ecc | 90 | rotationTime.start(); |
tyleralt | 0:d9730cf42ecc | 91 | firstTime = 0; |
tyleralt | 3:1e9b4a4bf177 | 92 | pc.printf("first time"); |
tyleralt | 3:1e9b4a4bf177 | 93 | led = !led; |
tyleralt | 2:554edc4b0cf2 | 94 | return; |
tyleralt | 4:f81b40e04a58 | 95 | } else if(current_slice < NUMBER_OF_SLICES / 4){ |
tyleralt | 2:554edc4b0cf2 | 96 | return; |
tyleralt | 2:554edc4b0cf2 | 97 | } |
tyleralt | 3:1e9b4a4bf177 | 98 | pc.printf("seconod time \n"); |
tyleralt | 3:1e9b4a4bf177 | 99 | led = !led; |
tyleralt | 0:d9730cf42ecc | 100 | rotate_time = rotationTime.read_us(); |
tyleralt | 0:d9730cf42ecc | 101 | rotationTime.reset(); |
tyleralt | 0:d9730cf42ecc | 102 | rotationTime.start(); |
tyleralt | 0:d9730cf42ecc | 103 | |
tyleralt | 2:554edc4b0cf2 | 104 | slice_time = (double) rotate_time/NUMBER_OF_SLICES; |
tyleralt | 0:d9730cf42ecc | 105 | current_slice = 0; |
tyleralt | 2:554edc4b0cf2 | 106 | updateLeds.attach_us(&nextLedPush, slice_time); |
tyleralt | 0:d9730cf42ecc | 107 | } |
tyleralt | 0:d9730cf42ecc | 108 | |
tyleralt | 0:d9730cf42ecc | 109 | |
tyleralt | 0:d9730cf42ecc | 110 | int main() { |
tyleralt | 3:1e9b4a4bf177 | 111 | pc.printf("started"); |
tyleralt | 0:d9730cf42ecc | 112 | firstTime = 1; |
tyleralt | 2:554edc4b0cf2 | 113 | current_slice = 90; |
tyleralt | 0:d9730cf42ecc | 114 | |
tyleralt | 3:1e9b4a4bf177 | 115 | hallSensor.fall(&rotate_sense); |
tyleralt | 2:554edc4b0cf2 | 116 | for (int i = 0; i < 45; i ++){ |
tyleralt | 4:f81b40e04a58 | 117 | slice_data [i][0] = 0x01; // bit 0 is high on arm 0 |
tyleralt | 4:f81b40e04a58 | 118 | slice_data [i][1] = 0x00; |
tyleralt | 4:f81b40e04a58 | 119 | slice_data [i][2] = 0x00; |
tyleralt | 4:f81b40e04a58 | 120 | slice_data [i][3] = 0x00; |
tyleralt | 4:f81b40e04a58 | 121 | slice_data [i][4] = 0x00; |
tyleralt | 4:f81b40e04a58 | 122 | slice_data [i][5] = 0x00; |
tyleralt | 4:f81b40e04a58 | 123 | slice_data [i][6] = 0x00; |
tyleralt | 4:f81b40e04a58 | 124 | slice_data [i][9] = 0x00; |
tyleralt | 4:f81b40e04a58 | 125 | slice_data [i][10] = 0x00; |
tyleralt | 4:f81b40e04a58 | 126 | slice_data [i][11] = 0x00; |
tyleralt | 4:f81b40e04a58 | 127 | slice_data [i][12] = 0x00; |
tyleralt | 4:f81b40e04a58 | 128 | slice_data [i][13] = 0x00; |
tyleralt | 4:f81b40e04a58 | 129 | slice_data [i][14] = 0x00; |
tyleralt | 4:f81b40e04a58 | 130 | slice_data [i][15] = 0x00; |
tyleralt | 1:ea12b9153c51 | 131 | //{1,1,1,1,0,0,0,0,1,1,1,0,0,0,1,0}; |
tyleralt | 0:d9730cf42ecc | 132 | } |
tyleralt | 2:554edc4b0cf2 | 133 | for (int i = 45; i < 90; i++){ |
tyleralt | 4:f81b40e04a58 | 134 | slice_data [i][0] = 0x00; // bit 0 is high on arm 0 |
tyleralt | 4:f81b40e04a58 | 135 | slice_data [i][1] = 0x00; |
tyleralt | 4:f81b40e04a58 | 136 | slice_data [i][2] = 0x00; |
tyleralt | 4:f81b40e04a58 | 137 | slice_data [i][3] = 0x00; |
tyleralt | 4:f81b40e04a58 | 138 | slice_data [i][4] = 0x00; |
tyleralt | 4:f81b40e04a58 | 139 | slice_data [i][5] = 0x00; |
tyleralt | 4:f81b40e04a58 | 140 | slice_data [i][6] = 0x00; |
tyleralt | 4:f81b40e04a58 | 141 | slice_data [i][9] = 0x00; |
tyleralt | 4:f81b40e04a58 | 142 | slice_data [i][10] = 0x00; |
tyleralt | 4:f81b40e04a58 | 143 | slice_data [i][11] = 0x00; |
tyleralt | 4:f81b40e04a58 | 144 | slice_data [i][12] = 0x00; |
tyleralt | 4:f81b40e04a58 | 145 | slice_data [i][13] = 0x00; |
tyleralt | 4:f81b40e04a58 | 146 | slice_data [i][14] = 0x00; |
tyleralt | 4:f81b40e04a58 | 147 | slice_data [i][15] = 0x00; |
tyleralt | 1:ea12b9153c51 | 148 | //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
tyleralt | 0:d9730cf42ecc | 149 | } |
tyleralt | 4:f81b40e04a58 | 150 | pushData(slice_data[20]); |
tyleralt | 0:d9730cf42ecc | 151 | |
tyleralt | 0:d9730cf42ecc | 152 | while(1) { |
tyleralt | 3:1e9b4a4bf177 | 153 | if (bt.readable()){ |
tyleralt | 4:f81b40e04a58 | 154 | pc.printf("Just read %c", bt.getc()); |
tyleralt | 4:f81b40e04a58 | 155 | } |
tyleralt | 4:f81b40e04a58 | 156 | |
tyleralt | 0:d9730cf42ecc | 157 | } |
tyleralt | 0:d9730cf42ecc | 158 | } |