4180 Electric Bike

Dependencies:   mbed mbed-rtos 4DGL-uLCD-SE

Committer:
ZellofisH
Date:
Mon Apr 29 14:43:52 2019 +0000
Revision:
0:0344add43397
4180 Electric Bike

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ZellofisH 0:0344add43397 1 #include "mbed.h"
ZellofisH 0:0344add43397 2 #include "uLCD_4DGL.h"
ZellofisH 0:0344add43397 3 #include "rtos.h"
ZellofisH 0:0344add43397 4
ZellofisH 0:0344add43397 5 //DigitalOut myled(LED1);
ZellofisH 0:0344add43397 6 DigitalIn brakeSwitch(p5);
ZellofisH 0:0344add43397 7 DigitalIn blinkerSwitchL(p7);
ZellofisH 0:0344add43397 8 DigitalIn blinkerSwitchR(p6);
ZellofisH 0:0344add43397 9 //AnalogIn throttleIn(p15);
ZellofisH 0:0344add43397 10 AnalogIn battVoltIn(p15);
ZellofisH 0:0344add43397 11 AnalogIn photoresistIn(p16);
ZellofisH 0:0344add43397 12 AnalogIn rpmIn(p17);
ZellofisH 0:0344add43397 13
ZellofisH 0:0344add43397 14 uLCD_4DGL uLCD(p9,p10,p11);
ZellofisH 0:0344add43397 15 //PwmOut throttleOut(p21);
ZellofisH 0:0344add43397 16 DigitalOut leftBlinker(p22);
ZellofisH 0:0344add43397 17 DigitalOut rightBlinker(p23);
ZellofisH 0:0344add43397 18 DigitalOut brakeLight(p24);
ZellofisH 0:0344add43397 19 DigitalOut headlight(p25);
ZellofisH 0:0344add43397 20 int brakeRunningLight = 0;
ZellofisH 0:0344add43397 21 Ticker left;
ZellofisH 0:0344add43397 22 Ticker right;
ZellofisH 0:0344add43397 23 int blinked = 0;
ZellofisH 0:0344add43397 24 //double speed = 0;
ZellofisH 0:0344add43397 25 double batteryPercent = .5;
ZellofisH 0:0344add43397 26 double throttlePercent = 0;
ZellofisH 0:0344add43397 27 double battMax = .55;
ZellofisH 0:0344add43397 28 //double throttleMin = 0.86;
ZellofisH 0:0344add43397 29 //double throttleMax = 3.29;
ZellofisH 0:0344add43397 30 double rpm = 0;
ZellofisH 0:0344add43397 31 int offOnL = 0;
ZellofisH 0:0344add43397 32 int offOnR = 0;
ZellofisH 0:0344add43397 33 double photo = photoresistIn;
ZellofisH 0:0344add43397 34
ZellofisH 0:0344add43397 35 void Brake(void const *args)
ZellofisH 0:0344add43397 36 {
ZellofisH 0:0344add43397 37 while(true) {
ZellofisH 0:0344add43397 38 if (!brakeRunningLight) {
ZellofisH 0:0344add43397 39 if (brakeSwitch == 1) {
ZellofisH 0:0344add43397 40 //throttleOut = 0;
ZellofisH 0:0344add43397 41 if (blinked == 0) {
ZellofisH 0:0344add43397 42 //int i = 0;
ZellofisH 0:0344add43397 43 for (int i = 0; i < 6; i++){
ZellofisH 0:0344add43397 44 brakeLight = !brakeLight;
ZellofisH 0:0344add43397 45 wait(.1);
ZellofisH 0:0344add43397 46 }
ZellofisH 0:0344add43397 47 blinked = 1;
ZellofisH 0:0344add43397 48 }
ZellofisH 0:0344add43397 49 //stay on
ZellofisH 0:0344add43397 50 brakeLight = 1;
ZellofisH 0:0344add43397 51 //switch off throttle
ZellofisH 0:0344add43397 52 } else {
ZellofisH 0:0344add43397 53 //brake light off
ZellofisH 0:0344add43397 54 blinked = 0;
ZellofisH 0:0344add43397 55 brakeLight = 0;
ZellofisH 0:0344add43397 56 }
ZellofisH 0:0344add43397 57 } else {
ZellofisH 0:0344add43397 58 if (brakeSwitch == 1) {
ZellofisH 0:0344add43397 59 //throttleOut = 0;
ZellofisH 0:0344add43397 60 if (blinked == 0) {
ZellofisH 0:0344add43397 61 //int i = 0;
ZellofisH 0:0344add43397 62 for (int i = 0; i < 6; i++){
ZellofisH 0:0344add43397 63 leftBlinker = !leftBlinker;
ZellofisH 0:0344add43397 64 rightBlinker = !rightBlinker;
ZellofisH 0:0344add43397 65 wait(.1);
ZellofisH 0:0344add43397 66 }
ZellofisH 0:0344add43397 67 blinked = 1;
ZellofisH 0:0344add43397 68 }
ZellofisH 0:0344add43397 69 //stay on
ZellofisH 0:0344add43397 70 leftBlinker = 1;
ZellofisH 0:0344add43397 71 rightBlinker = 1;
ZellofisH 0:0344add43397 72 //switch off throttle
ZellofisH 0:0344add43397 73 } else {
ZellofisH 0:0344add43397 74 //brake light off
ZellofisH 0:0344add43397 75 blinked = 0;
ZellofisH 0:0344add43397 76 leftBlinker = 0;
ZellofisH 0:0344add43397 77 rightBlinker = 0;
ZellofisH 0:0344add43397 78 }
ZellofisH 0:0344add43397 79 }
ZellofisH 0:0344add43397 80 Thread::wait(200);
ZellofisH 0:0344add43397 81 }
ZellofisH 0:0344add43397 82 }
ZellofisH 0:0344add43397 83
ZellofisH 0:0344add43397 84 //void Throttle(void const *args)
ZellofisH 0:0344add43397 85 //{
ZellofisH 0:0344add43397 86 // while(true) {
ZellofisH 0:0344add43397 87 // if (brakeSwitch == 1) {
ZellofisH 0:0344add43397 88 // //throttleOut = 0;
ZellofisH 0:0344add43397 89 // } else {
ZellofisH 0:0344add43397 90 // //throttleOut = throttleIn;
ZellofisH 0:0344add43397 91 // //throttlePercent = (double)throttleIn / throttleMax;
ZellofisH 0:0344add43397 92 // }
ZellofisH 0:0344add43397 93 // Thread::wait(100);
ZellofisH 0:0344add43397 94 // }
ZellofisH 0:0344add43397 95 //}
ZellofisH 0:0344add43397 96
ZellofisH 0:0344add43397 97 void lights(void const *args)
ZellofisH 0:0344add43397 98 {
ZellofisH 0:0344add43397 99 while(true) {
ZellofisH 0:0344add43397 100 if (photoresistIn <= .15) {
ZellofisH 0:0344add43397 101 headlight = 1;
ZellofisH 0:0344add43397 102 brakeRunningLight = 1;
ZellofisH 0:0344add43397 103 } else if (photoresistIn >= .35){
ZellofisH 0:0344add43397 104 headlight = 0;
ZellofisH 0:0344add43397 105 brakeRunningLight = 0;
ZellofisH 0:0344add43397 106 }
ZellofisH 0:0344add43397 107 Thread::wait(200);
ZellofisH 0:0344add43397 108 }
ZellofisH 0:0344add43397 109 }
ZellofisH 0:0344add43397 110
ZellofisH 0:0344add43397 111 void battery(void const *args)
ZellofisH 0:0344add43397 112 {
ZellofisH 0:0344add43397 113 while(1) {
ZellofisH 0:0344add43397 114 batteryPercent = (double)((double)battVoltIn / battMax);
ZellofisH 0:0344add43397 115 Thread::wait(5000);
ZellofisH 0:0344add43397 116 }
ZellofisH 0:0344add43397 117 }
ZellofisH 0:0344add43397 118
ZellofisH 0:0344add43397 119 void RPM(void const *args)
ZellofisH 0:0344add43397 120 {
ZellofisH 0:0344add43397 121 rpm = (float(rpmIn) / 2.3);
ZellofisH 0:0344add43397 122 }
ZellofisH 0:0344add43397 123
ZellofisH 0:0344add43397 124 void blinker(void const *args) {
ZellofisH 0:0344add43397 125 if (offOnL) {
ZellofisH 0:0344add43397 126 if (leftBlinker) {
ZellofisH 0:0344add43397 127 leftBlinker = 0;
ZellofisH 0:0344add43397 128 } else {
ZellofisH 0:0344add43397 129 leftBlinker = 1;
ZellofisH 0:0344add43397 130 }
ZellofisH 0:0344add43397 131 } else {
ZellofisH 0:0344add43397 132 leftBlinker = 0;
ZellofisH 0:0344add43397 133 }
ZellofisH 0:0344add43397 134
ZellofisH 0:0344add43397 135 if (offOnR) {
ZellofisH 0:0344add43397 136 if (rightBlinker) {
ZellofisH 0:0344add43397 137 rightBlinker = 0;
ZellofisH 0:0344add43397 138 } else {
ZellofisH 0:0344add43397 139 rightBlinker = 1;
ZellofisH 0:0344add43397 140 }
ZellofisH 0:0344add43397 141 } else {
ZellofisH 0:0344add43397 142 rightBlinker = 0;
ZellofisH 0:0344add43397 143 }
ZellofisH 0:0344add43397 144 Thread::wait(500);
ZellofisH 0:0344add43397 145 }
ZellofisH 0:0344add43397 146
ZellofisH 0:0344add43397 147 int main() {
ZellofisH 0:0344add43397 148 uLCD.locate(0,0);
ZellofisH 0:0344add43397 149 uLCD.printf("Speed: ");
ZellofisH 0:0344add43397 150 uLCD.locate(0,3);
ZellofisH 0:0344add43397 151 uLCD.printf("Battery:");
ZellofisH 0:0344add43397 152 blinkerSwitchL.mode(PullUp);
ZellofisH 0:0344add43397 153 blinkerSwitchR.mode(PullUp);
ZellofisH 0:0344add43397 154 brakeSwitch.mode(PullDown);
ZellofisH 0:0344add43397 155 brakeLight = 1;
ZellofisH 0:0344add43397 156 Thread t1(Brake);
ZellofisH 0:0344add43397 157 Thread t2(RPM);
ZellofisH 0:0344add43397 158 Thread t3(lights);
ZellofisH 0:0344add43397 159 Thread t4(battery);
ZellofisH 0:0344add43397 160 while(1) {
ZellofisH 0:0344add43397 161 photo = photoresistIn;
ZellofisH 0:0344add43397 162 //start threads
ZellofisH 0:0344add43397 163 Thread t5(blinker);
ZellofisH 0:0344add43397 164
ZellofisH 0:0344add43397 165 if (!blinkerSwitchL) {
ZellofisH 0:0344add43397 166 if (offOnL) {
ZellofisH 0:0344add43397 167 offOnL = 0;
ZellofisH 0:0344add43397 168 } else {
ZellofisH 0:0344add43397 169 offOnL = 1;
ZellofisH 0:0344add43397 170 }
ZellofisH 0:0344add43397 171 }
ZellofisH 0:0344add43397 172 if (!blinkerSwitchR) {
ZellofisH 0:0344add43397 173 if (offOnR) {
ZellofisH 0:0344add43397 174 offOnR = 0;
ZellofisH 0:0344add43397 175 } else {
ZellofisH 0:0344add43397 176 offOnR = 1;
ZellofisH 0:0344add43397 177 }
ZellofisH 0:0344add43397 178 }
ZellofisH 0:0344add43397 179 uLCD.locate(8,0);
ZellofisH 0:0344add43397 180 uLCD.printf("%f", photo);//rpm*100);
ZellofisH 0:0344add43397 181 uLCD.rectangle(0,8, 127, 16, WHITE);
ZellofisH 0:0344add43397 182 uLCD.filled_rectangle(1,9, 126, 15, BLACK);
ZellofisH 0:0344add43397 183 uLCD.filled_rectangle(1,9, rpm*127, 15, BLUE);
ZellofisH 0:0344add43397 184 uLCD.locate(8,3);
ZellofisH 0:0344add43397 185 uLCD.printf("%f", batteryPercent);
ZellofisH 0:0344add43397 186 uLCD.rectangle(0,33, 127, 41, WHITE);
ZellofisH 0:0344add43397 187 uLCD.filled_rectangle(1,34, 126, 40, BLACK);
ZellofisH 0:0344add43397 188 uLCD.filled_rectangle(1,34, batteryPercent*127, 40, BLUE);
ZellofisH 0:0344add43397 189 uLCD.locate(11,3);
ZellofisH 0:0344add43397 190 Thread::wait(.1);
ZellofisH 0:0344add43397 191 }
ZellofisH 0:0344add43397 192 }