Advertisement
axionl

speedtest for bike

Nov 6th, 2017
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <MsTimer2.h>
  6. #include <LedControl.h>
  7.  
  8. int count = 0;
  9. int DIN = 12;
  10. int CS =  11;
  11. int CLK = 10;
  12. const float circle = 1.89; // 周长
  13. unsigned int timeCount = 0;
  14.  
  15.  
  16. byte L0[8] = {0x60,0x90,0x90,0x90,0x90,0x90,0x60,0x00};
  17. byte L1[8] = {0x20,0x60,0xa0,0x20,0x20,0x20,0xf0,0x00};
  18. byte L2[8] = {0x60,0x90,0x10,0x20,0x40,0x80,0xf0,0x00};
  19. byte L3[8] = {0x60,0x90,0x10,0x20,0x10,0x90,0x60,0x00};
  20. byte L4[8] = {0x20,0x60,0xa0,0xf0,0x20,0x20,0x20,0x00};
  21. byte L5[8] = {0xf0,0x80,0x80,0xe0,0x10,0x10,0xe0,0x00};
  22. byte L6[8] = {0x20,0x40,0x80,0xe0,0x90,0x90,0x60,0x00};
  23. byte L7[8] = {0xf0,0x90,0x10,0x20,0x40,0x40,0x40,0x00};
  24. byte L8[8] = {0x60,0x90,0x90,0x60,0x90,0x90,0x60,0x00};
  25. byte L9[8] = {0x60,0x90,0x90,0x70,0x20,0x40,0x80,0x00};
  26.  
  27. byte R0[8] = {0x06,0x09,0x09,0x09,0x09,0x09,0x06,0x00};
  28. byte R1[8] = {0x02,0x06,0x0a,0x02,0x02,0x02,0x0f,0x00};
  29. byte R2[8] = {0x06,0x09,0x01,0x02,0x04,0x08,0x0f,0x00};
  30. byte R3[8] = {0x06,0x09,0x01,0x02,0x01,0x09,0x06,0x00};
  31. byte R4[8] = {0x02,0x06,0x0a,0x0f,0x02,0x02,0x02,0x00};
  32. byte R5[8] = {0x0f,0x08,0x08,0x0e,0x01,0x01,0x0e,0x00};
  33. byte R6[8] = {0x02,0x04,0x08,0x0e,0x09,0x09,0x06,0x00};
  34. byte R7[8] = {0x0f,0x09,0x01,0x02,0x04,0x04,0x04,0x00};
  35. byte R8[8] = {0x06,0x09,0x09,0x06,0x09,0x09,0x06,0x00};
  36. byte R9[8] = {0x06,0x09,0x09,0x07,0x02,0x04,0x08,0x00};
  37.  
  38. byte STOP[8] = {0x18,0x3c,0x3c,0x18,0x18,0x00,0x18,0x18}; //  输出感叹号“!”
  39.  
  40. LedControl lc=LedControl(DIN,CLK,CS,4);
  41.  
  42. void setup() {
  43.     attachInterrupt(0, blink, LOW);
  44.     Serial.begin(9600);  // 串口通信波特率
  45.     lc.shutdown(0,false);       //启动时,MAX7219处于省电模式
  46.     lc.setIntensity(0,3);       //将亮度设置为最大值
  47.     lc.clearDisplay(0);         //清除显示
  48.     MsTimer2::set(1, timeTo);  // 设定时间间隔
  49.     MsTimer2::start();  // 启动定时器
  50. }
  51.  
  52. void loop() {
  53. }
  54.  
  55. void timeTo() {
  56.     // 计时
  57.     timeCount += 1;
  58.     if (timeCount > 4000) {
  59.         printMerge(L0,R0);
  60.     }
  61. }
  62.  
  63. void blink() {
  64.     // 阈值判断
  65.     if (timeCount > 100){
  66.         calSpeed();
  67.     }
  68.     timeCount = 0;
  69. }
  70.  
  71. // 计算速度
  72. void calSpeed() {
  73.     float speed = circle / timeCount * 3600 + 1;  // 3600为1000ms*3.6km/h, +1 为延迟补偿
  74.     if (speed > 50.0) {
  75.        printByte(STOP);
  76.     } else {
  77.         // 显示换算
  78.         int L_tmp = speed / 10;
  79.         int R_tmp = int(fmod(speed, 10));   // 浮点数取余
  80.         byte L[8];
  81.         byte R[8];
  82.         switch (L_tmp) {
  83.             case 0: memcpy(L, L0, 8); break;
  84.             case 1: memcpy(L, L1, 8); break;
  85.             case 2: memcpy(L, L2, 8); break;
  86.             case 3: memcpy(L, L3, 8); break;
  87.             case 4: memcpy(L, L4, 8); break;
  88.             case 5: memcpy(L, L5, 8); break;
  89.             case 6: memcpy(L, L6, 8); break;
  90.             case 7: memcpy(L, L7, 8); break;
  91.             case 8: memcpy(L, L8, 8); break;
  92.             case 9: memcpy(L, L9, 8); break;
  93.         }
  94.         switch (R_tmp) {
  95.             case 0: memcpy(R, R0, 8); break;
  96.             case 1: memcpy(R, R1, 8); break;
  97.             case 2: memcpy(R, R2, 8); break;
  98.             case 3: memcpy(R, R3, 8); break;
  99.             case 4: memcpy(R, R4, 8); break;
  100.             case 5: memcpy(R, R5, 8); break;
  101.             case 6: memcpy(R, R6, 8); break;
  102.             case 7: memcpy(R, R7, 8); break;
  103.             case 8: memcpy(R, R8, 8); break;
  104.             case 9: memcpy(R, R9, 8); break;
  105.         }
  106.         printMerge(L, R);
  107.     }    
  108. }
  109.  
  110. // 单字符输出
  111. void printByte(byte character []) {
  112.   for (int i = 0; i < 8; i++) {
  113.     lc.setRow(0, i, character[i]);
  114.   }
  115. }
  116.  
  117. // 双字符输出
  118. void printMerge(byte x [], byte y []) {
  119.     byte c[8];
  120.     for (int i = 0; i < 8; i++) {
  121.         c[i] = x[i] + y[i];
  122.     }
  123.     printByte(c);
  124. }
  125.  
  126. // 自行车车轮周长3.14 * 600 =1890mm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement