Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.14 KB | None | 0 0
  1. /*
  2.    --------------------------------------------------------
  3.         Jeti Arduino RPM Sensor v 0.1
  4.    --------------------------------------------------------
  5.  
  6.     Tero Salminen RC-Thoughts.com 2017 www.rc-thoughts.com
  7.  
  8.     Ability to set amount magnets (2 is recommended for balance)
  9.     If for example headspeed is wanted you can set gear-ratio.
  10.  
  11.    --------------------------------------------------------
  12.     ALWAYS test functions thoroughly before use!
  13.    --------------------------------------------------------
  14.     Shared under MIT-license by Tero Salminen 2017
  15.    --------------------------------------------------------
  16. */
  17.  
  18. String sensVersion = "v.0.1";
  19.  
  20. // Settings according models hardware
  21. int magnets = 1; // How many magnets on your setup?
  22. float gearRatio = 1; // Gear ratio * 100, for example 18 teeth pinion and 172 main gear = 18/172 = 10.4651
  23.  
  24. // Do not touch below this
  25. #include <SoftwareSerialJeti.h>
  26. #include <JETI_EX_SENSOR.h>
  27. unsigned int rpm = 255;
  28. int hall = 2;
  29. int rps;
  30. long interval;
  31. int header = 0;
  32. int lastbtn = 240;
  33. int current_screen = 1;
  34. int current_config = 0;
  35.  
  36. char temp[LCDMaxPos / 2];
  37. char msg_line1[LCDMaxPos / 2];
  38. char msg_line2[LCDMaxPos / 2];
  39.  
  40. #define prog_char char PROGMEM
  41. #define GETCHAR_TIMEOUT_ms 20
  42.  
  43. #ifndef JETI_RX
  44. #define JETI_RX 3
  45. #endif
  46.  
  47. #ifndef JETI_TX
  48. #define JETI_TX 4
  49. #endif
  50.  
  51. #define ITEMNAME_1 F("RPM")
  52. #define ITEMTYPE_1 F("rpm")
  53. #define ITEMVAL_1 (unsigned int*)&rpm
  54.  
  55. #define ABOUT_1 F(" RCT Jeti Tools")
  56. #define ABOUT_2 F("   RPM-Sensor")
  57.  
  58. SoftwareSerial JetiSerial(JETI_RX, JETI_TX);
  59.  
  60. void JetiUartInit()
  61. {
  62.   JetiSerial.begin(9700);
  63. }
  64.  
  65. void JetiTransmitByte(unsigned char data, boolean setBit9)
  66. {
  67.   JetiSerial.set9bit = setBit9;
  68.   JetiSerial.write(data);
  69.   JetiSerial.set9bit = 0;
  70. }
  71.  
  72. unsigned char JetiGetChar(void)
  73. {
  74.   unsigned long time = millis();
  75.   while ( JetiSerial.available()  == 0 )
  76.   {
  77.     if (millis() - time >  GETCHAR_TIMEOUT_ms)
  78.       return 0;
  79.   }
  80.   int read = -1;
  81.   if (JetiSerial.available() > 0 )
  82.   { read = JetiSerial.read();
  83.   }
  84.   long wait = (millis() - time) - GETCHAR_TIMEOUT_ms;
  85.   if (wait > 0)
  86.     delay(wait);
  87.   return read;
  88. }
  89.  
  90. char * floatToString(char * outstr, float value, int places, int minwidth = 0) {
  91.   int digit;
  92.   float tens = 0.1;
  93.   int tenscount = 0;
  94.   int i;
  95.   float tempfloat = value;
  96.   int c = 0;
  97.   int charcount = 1;
  98.   int extra = 0;
  99.   float d = 0.5;
  100.   if (value < 0)
  101.     d *= -1.0;
  102.   for (i = 0; i < places; i++)
  103.     d /= 10.0;
  104.   tempfloat +=  d;
  105.   if (value < 0)
  106.     tempfloat *= -1.0;
  107.   while ((tens * 10.0) <= tempfloat) {
  108.     tens *= 10.0;
  109.     tenscount += 1;
  110.   }
  111.   if (tenscount > 0)
  112.     charcount += tenscount;
  113.   else
  114.     charcount += 1;
  115.   if (value < 0)
  116.     charcount += 1;
  117.   charcount += 1 + places;
  118.   minwidth += 1;
  119.   if (minwidth > charcount) {
  120.     extra = minwidth - charcount;
  121.     charcount = minwidth;
  122.   }
  123.   if (value < 0)
  124.     outstr[c++] = '-';
  125.   if (tenscount == 0)
  126.     outstr[c++] = '0';
  127.   for (i = 0; i < tenscount; i++) {
  128.     digit = (int) (tempfloat / tens);
  129.     itoa(digit, &outstr[c++], 10);
  130.     tempfloat = tempfloat - ((float)digit * tens);
  131.     tens /= 10.0;
  132.   }
  133.   if (places > 0)
  134.     outstr[c++] = '.';
  135.   for (i = 0; i < places; i++) {
  136.     tempfloat *= 10.0;
  137.     digit = (int) tempfloat;
  138.     itoa(digit, &outstr[c++], 10);
  139.     tempfloat = tempfloat - (float) digit;
  140.   }
  141.   if (extra > 0 ) {
  142.     for (int i = 0; i < extra; i++) {
  143.       outstr[c++] = ' ';
  144.     }
  145.   }
  146.   outstr[c++] = '\0';
  147.   return outstr;
  148. }
  149.  
  150. JETI_Box_class JB;
  151.  
  152. unsigned char SendFrame()
  153. {
  154.   boolean bit9 = false;
  155.   for (int i = 0 ; i < JB.frameSize ; i++ )
  156.   {
  157.     if (i == 0)
  158.       bit9 = false;
  159.     else if (i == JB.frameSize - 1)
  160.       bit9 = false;
  161.     else if (i == JB.middle_bit9)
  162.       bit9 = false;
  163.     else
  164.       bit9 = true;
  165.     JetiTransmitByte(JB.frame[i], bit9);
  166.   }
  167. }
  168.  
  169. #define MAX_SCREEN 1
  170. #define MAX_CONFIG 1
  171. #define COND_LES_EQUAL 1
  172. #define COND_MORE_EQUAL 2
  173.  
  174. // setup functions
  175. void setup()
  176. {
  177.   Serial.begin(9600);
  178.   pinMode(hall, INPUT_PULLUP);
  179.   Serial.println("");
  180.   Serial.print("RC-Thoughts Hall RPM-Sensor "); Serial.println(sensVersion);
  181.   Serial.println("design by Tero Salminen @ RC-Thoughts 2017");
  182.   Serial.println("Free and open-source - Released under MIT-license");
  183.   Serial.println("");
  184.   Serial.println("Ready!");
  185.   Serial.println("");
  186.   pinMode(13, OUTPUT);
  187.   digitalWrite(13, HIGH);
  188.   pinMode(JETI_RX, OUTPUT);
  189.   JetiUartInit();
  190.   JB.JetiBox(ABOUT_1, ABOUT_2);
  191.   JB.Init(F("RCT"));
  192.   JB.addData(ITEMNAME_1, ITEMTYPE_1);
  193.   JB.setValueBig(1, ITEMVAL_1);
  194.   do {
  195.     JB.createFrame(1);
  196.     SendFrame();
  197.     delay(GETCHAR_TIMEOUT_ms);
  198.   }
  199.   while (sensorFrameName != 0);
  200.   digitalWrite(13, LOW);
  201. }
  202.  
  203. // Working loop
  204. void loop()
  205. {
  206.   // Jeti Stuff
  207.   unsigned long time = millis();
  208.   SendFrame();
  209.   time = millis();
  210.   int read = 0;
  211.   pinMode(JETI_RX, INPUT);
  212.   pinMode(JETI_TX, INPUT_PULLUP);
  213.  
  214.   JetiSerial.listen();
  215.   JetiSerial.flush();
  216.  
  217.   while ( JetiSerial.available()  == 0 )
  218.   {
  219.     if (millis() - time >  5)
  220.       break;
  221.   }
  222.  
  223.   if (JetiSerial.available() > 0 )
  224.   { read = JetiSerial.read();
  225.     if (lastbtn != read)
  226.     {
  227.       lastbtn = read;
  228.       switch (read)
  229.       {
  230.         case 224 :
  231.           break;
  232.         case 112 :
  233.           break;
  234.         case 208 :
  235.           break;
  236.         case 176 :
  237.           break;
  238.       }
  239.     }
  240.   }
  241.  
  242.   if (current_screen != MAX_SCREEN)
  243.     current_config = 0;
  244.   header++;
  245.   if (header >= 5)
  246.   {
  247.     JB.createFrame(1);
  248.     header = 0;
  249.   }
  250.   else
  251.   {
  252.     JB.createFrame(0);
  253.   }
  254.  
  255.   long wait = GETCHAR_TIMEOUT_ms;
  256.   long milli = millis() - time;
  257.   if (milli > wait)
  258.     wait = 0;
  259.   else
  260.     wait = wait - milli;
  261.   pinMode(JETI_TX, OUTPUT);
  262.  
  263.   // RPM-Stuff
  264.   /*interval = pulseIn(hall, HIGH) + pulseIn(hall, LOW);
  265.   rps = 1000000UL / interval;
  266.   if (rps == -1) {
  267.     rpm = 0;
  268.   }
  269.   else {
  270.     rpm = (rps * 6 * gearRatio) / magnets;
  271.     if (rpm < 0) {
  272.       rpm = 0;
  273.     }
  274.   }*/
  275.   unsigned int rpm2=((uint8_t*)rpm);
  276.   Serial.print("Freq "); Serial.print(rps);Serial.print(" RPM ");Serial.print(rpm);Serial.print(" RPM2 "); Serial.println(rpm2);
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement