Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. #include "mbed.h"
  2. #include "MMA7660.h"
  3. #include "C12832.h"
  4. #include "LM75B.h"
  5. #include "main.h"
  6. //#include "MSCFileSystem.h"
  7.  
  8. //Declare LEDs
  9. DigitalOut blue_led(p25);
  10. DigitalOut red_led(p23);
  11.  
  12. //Declare Speaker
  13. PwmOut spkr(p26);
  14.  
  15. //Declare LED
  16. C12832 lcd(p5, p7, p6, p8, p11);
  17.  
  18. //Accelerometer
  19. MMA7660 MMA(p28, p27);
  20. //Temp
  21. LM75B sensor(p28,p27);
  22.  
  23. //Potentiometers
  24. AnalogIn vibPot(p19);
  25. AnalogIn tempPot(p20);
  26.  
  27. Ticker red_ticker;
  28. Ticker blue_ticker;
  29. Ticker spkr_ticker;
  30.  
  31. static const float fAccelBound = 1.5;
  32. static const float fTempBound = 50;
  33. static const int VOLUME = 250;
  34.  
  35. float fCurrentTemp;
  36.  
  37. float fZaxis_p;
  38. float fZaxis_n;
  39.  
  40. float fYaxis_p;
  41. float fYaxis_n;
  42.  
  43. float fXaxis_p;
  44. float fXaxis_n;
  45.  
  46. char cAxisID;
  47.  
  48. float fAxisSenseThresh = 0.2;
  49.  
  50. float fGravity = 1.0;
  51.  
  52. float fVibrationThresholdReached = 0;
  53.  
  54. int main(){
  55. blue_led = 1;
  56. red_led = 1;
  57.  
  58. float fPosVibThresh = 0.0;
  59. float fNegVibThresh = 0.0;
  60.  
  61. while(1){
  62.  
  63. if(fVibrationThresholdReached == 0){
  64. fPosVibThresh = vibPot * fAccelBound;
  65. fNegVibThresh = -fNegVibThresh;
  66. LCD_Stat_Update(fPosVibThresh);
  67.  
  68. fZaxis_p = MMA.z();
  69. fZaxis_n = -MMA.z();
  70.  
  71. fXaxis_p = MMA.x();
  72. fXaxis_p = -MMA.x();
  73.  
  74. fYaxis_p = MMA.y();
  75. fYaxis_p = -MMA.y();
  76.  
  77. if((fZaxis_p >= fPosVibThresh && fZaxis_n <= fNegVibThresh) && (fPosVibThresh > fGravity && fNegVibThresh < -fGravity)){
  78. cAxisID = 'Z';
  79. }
  80.  
  81. else if(fYaxis_p >= fPosVibThresh && fZaxis_n <= fNegVibThresh){
  82. cAxisID = 'Y';
  83. }
  84.  
  85. else if(fXaxis_p >= fPosVibThresh && fXaxis_n <= fNegVibThresh){
  86. cAxisID = 'X';
  87. }
  88.  
  89. else{
  90. cAxisID = NULL;
  91. }
  92.  
  93. switch(cAxisID){
  94. case 'Z':
  95. case 'Y':
  96. case 'X':
  97. LCD_Vibration_Update();
  98. fVibrationThresholdReached = 1;
  99. Vibration_Threshold_Reached();
  100. break;
  101. case NULL:
  102.  
  103. break;
  104. }
  105.  
  106. }
  107.  
  108. }
  109.  
  110.  
  111.  
  112. }
  113.  
  114. void LCD_Stat_Update(float fPosVibThresh){
  115. lcd.cls();
  116. lcd.locate(0,3);
  117.  
  118. //Displays the Vibration Threshold to 2 decimal places.
  119. lcd.printf("Vibration Threshold: %.2f", fPosVibThresh);
  120.  
  121. wait(0.2);
  122. }
  123.  
  124. void LCD_Temp_Update(float fTempThreshold, float fCurrentTemp){
  125. lcd.cls();
  126. lcd.locate(0,3);
  127. lcd.printf("Temp Threshold: %.2f\nTemperature:%.2f ->", fTempThreshold, fCurrentTemp);
  128. wait(0.2);
  129. }
  130.  
  131. void LCD_Vibration_Update(){
  132. lcd.cls();
  133. lcd.locate(0,3);
  134. lcd.printf("Vibration Threshold Reached\nOn Axis: %c", cAxisID);
  135. wait(0.2);
  136. }
  137.  
  138. void LCD_Armageddon_Update(){
  139. lcd.cls();
  140. lcd.locate(10,7);
  141. lcd.printf(" !!Gloom and Doom!!");
  142. }
  143.  
  144. void Vibration_Threshold_Reached(){
  145. int iFlashController;
  146.  
  147. float fTempThreshold;
  148.  
  149. bool isBlueSet = false;
  150.  
  151. MMA.setActive(false);
  152.  
  153. while(1){
  154. fCurrentTemp = sensor.read();
  155. fTempThreshold = tempPot * fTempBound;
  156.  
  157. if(fCurrentTemp >= fTempThreshold){
  158. iFlashController = 2;
  159. Led_Control(iFlashController);
  160. LCD_Armageddon_Update();
  161. break;
  162. }
  163. else{
  164. if(!isBlueSet){
  165. iFlashController = 1;
  166. Led_Control(iFlashController);
  167. isBlueSet = true;
  168. }
  169. }
  170.  
  171. LCD_Temp_Update(fTempThreshold, fCurrentTemp);
  172. }
  173. }
  174.  
  175. void Led_Control(int iFlashController){
  176. switch(iFlashController){
  177. case 1:// Blue LED Flashing @1Hz, Red LED off
  178. blue_ticker.attach(&Blue_Toggle, 1.0);
  179. break;
  180. case 2://Red LED Flashing @5Hz Blue LED off
  181. blue_led = 1;
  182. blue_ticker.detach();
  183. red_ticker.attach(&Red_Toggle, 0.2);
  184. spkr_ticker.attach(&Speaker_On, 0.1);
  185. break;
  186. }
  187. }
  188. float Calculate_Speaker_Frequency(){
  189. float fAverage = (MMA.z() + MMA.x() + MMA.y()) / 3;
  190. fAverage *= sensor.read();
  191.  
  192. fAverage *= VOLUME;
  193.  
  194. fAverage *= tempPot;
  195.  
  196. return fAverage;
  197. }
  198.  
  199. void Speaker_On(){
  200. spkr.period(1.0 / Calculate_Speaker_Frequency()); //TODO PUT IN AVERAGE
  201.  
  202. if(spkr != 0.5)
  203. spkr = 0.5;
  204.  
  205. }
  206.  
  207. void Red_Toggle(){
  208. red_led = !red_led;
  209.  
  210. }
  211. void Blue_Toggle(){
  212. blue_led = !blue_led;
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement