automotive simulation thingy
Dependencies: N5110 ShiftReg Tone mbed
Fork of 1620_Project_Template by
ModeD/ModeD.cpp@2:e7e39d27b11f, 2017-06-29 (annotated)
- Committer:
- Al_Husien_Dabashi
- Date:
- Thu Jun 29 22:41:30 2017 +0000
- Revision:
- 2:e7e39d27b11f
- Parent:
- 0:d5312060f649
1620 Final Automotive simulation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 0:d5312060f649 | 1 | #include "ModeD.h" |
eencae | 0:d5312060f649 | 2 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 3 | void mode_D() |
Al_Husien_Dabashi | 2:e7e39d27b11f | 4 | { |
eencae | 0:d5312060f649 | 5 | lcd.clear(); |
eencae | 0:d5312060f649 | 6 | lcd.printString("Mode D",0,0); |
eencae | 0:d5312060f649 | 7 | lcd.refresh(); |
eencae | 0:d5312060f649 | 8 | wait_ms(250); |
eencae | 0:d5312060f649 | 9 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 10 | AnalogIn joy_v(p20); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 11 | AnalogIn joy_h(p19); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 12 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 13 | shift.write(0x3f); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 14 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 15 | float gear_ratio; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 16 | float speed; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 17 | float RPM; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 18 | |
eencae | 0:d5312060f649 | 19 | while (button_d.read() == 0) { |
eencae | 0:d5312060f649 | 20 | // code goes in here - this acts like the main while(1) loop |
eencae | 0:d5312060f649 | 21 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 22 | lcd.clear(); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 23 | lcd.printString("Mode D",0,0); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 24 | lcd.printString("0",2,17); |
eencae | 0:d5312060f649 | 25 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 26 | float pot2_val = pot_2.read(); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 27 | RPM = pot2_val*5300+700; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 28 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 29 | char RPM_val[27]; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 30 | sprintf(RPM_val,"%.0f",RPM); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 31 | lcd.printString(RPM_val,25,2); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 32 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 33 | lcd.drawRect(5,8,74,7,FILL_TRANSPARENT); // RPM bar |
Al_Husien_Dabashi | 2:e7e39d27b11f | 34 | lcd.drawRect(5,30,74,7,FILL_TRANSPARENT); // Speed bar |
Al_Husien_Dabashi | 2:e7e39d27b11f | 35 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 36 | float pot2_acc = pot_2.read(); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 37 | lcd.drawRect(6,9,pot2_val*74,6,FILL_BLACK); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 38 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 39 | lcd.printString("0",3,5); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 40 | lcd.printString("137",65,5); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 41 | lcd.printString("0",3,2); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 42 | lcd.printString("6000",60,2); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 43 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 44 | float xr = joy_h.read(); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 45 | float yr = joy_v.read(); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 46 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 47 | if (button_a.read() == 1) { |
Al_Husien_Dabashi | 2:e7e39d27b11f | 48 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 49 | if((xr<0.2)&& (yr<0.2)) { |
Al_Husien_Dabashi | 2:e7e39d27b11f | 50 | shift.write(0x31); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 51 | gear_ratio=3.62; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 52 | } else if((0.8<xr)&& (0.8<yr)) { |
Al_Husien_Dabashi | 2:e7e39d27b11f | 53 | shift.write(0x06); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 54 | gear_ratio=3.85; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 55 | } else if((0.8<xr)&& (yr<0.2)) { |
Al_Husien_Dabashi | 2:e7e39d27b11f | 56 | shift.write(0x5B); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 57 | gear_ratio=2.04; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 58 | } else if((0.4<xr)&& (xr<0.6)&& (0.8<yr)) { |
Al_Husien_Dabashi | 2:e7e39d27b11f | 59 | shift.write(0x4F); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 60 | gear_ratio=1.28; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 61 | } else if((0.4<xr)&&(xr<0.6)&& (yr<0.2)) { |
Al_Husien_Dabashi | 2:e7e39d27b11f | 62 | shift.write(0x66); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 63 | gear_ratio=0.95; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 64 | } else if((xr<0.2)&& (0.8<yr)) { |
Al_Husien_Dabashi | 2:e7e39d27b11f | 65 | shift.write(0x6D); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 66 | gear_ratio=0.76; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 67 | } else { |
Al_Husien_Dabashi | 2:e7e39d27b11f | 68 | shift.write(0x3f); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 69 | gear_ratio=1000000000; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 70 | } |
Al_Husien_Dabashi | 2:e7e39d27b11f | 71 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 72 | } |
Al_Husien_Dabashi | 2:e7e39d27b11f | 73 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 74 | speed=(0.001174392*RPM*60)/(gear_ratio*4.07); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 75 | printf("RPM = %1.0f speed = %.0f mph\n",RPM,speed); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 76 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 77 | lcd.drawRect(5,30,speed*37/68,7,FILL_BLACK); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 78 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 79 | char speed_val[27]; |
Al_Husien_Dabashi | 2:e7e39d27b11f | 80 | sprintf(speed_val,"%.0f",speed); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 81 | lcd.printString(speed_val,27,5); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 82 | |
Al_Husien_Dabashi | 2:e7e39d27b11f | 83 | lcd.refresh(); |
Al_Husien_Dabashi | 2:e7e39d27b11f | 84 | wait_ms(100); |
eencae | 0:d5312060f649 | 85 | } |
Al_Husien_Dabashi | 2:e7e39d27b11f | 86 | |
eencae | 0:d5312060f649 | 87 | } |