Guest User

Untitled

a guest
Oct 17th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1.  
  2. /*
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5. LiquidCrystal_I2C lcd(0x3F,16,2);
  6. */
  7.  
  8. //stepper motor stuff
  9. #include <SwitecX25.h>
  10. SwitecX25 motor1(945, 13,14,15,16);
  11. SwitecX25 motor2(945, 17,18,19,12);
  12.  
  13.  
  14.  
  15. //gear pins
  16. int gear1=1;
  17. int gear2=3;
  18. int gear3=4;
  19. int gear4=5;
  20. int gear5=6;
  21. int gear6=7;
  22. int gear0=8;
  23.  
  24. int wheeloffset=16;
  25.  
  26. //interrupt pin
  27. int ignitionpin=2;
  28.  
  29. //ignition values
  30. long deltat=0;
  31. long RPM=0;
  32. long currentRPM=0;
  33. int pulsecounter=0;
  34. long timea=0;
  35. long timeb=0;
  36.  
  37. //needle variable
  38. int needleposition=0;
  39.  
  40. //array -- modify RPMarraysize and RPMrr to alter needle smoothness
  41. const int RPMarraysize=4;
  42. int RPMrr=20; //display RPM refresh rate
  43.  
  44. int RPMarray[RPMarraysize];
  45. int arrayposition=0;
  46.  
  47. //tachsweeping
  48. int tachswept=0;
  49. long sweeptime;
  50.  
  51. void setup() {
  52. /*
  53. // start 16x2 parallel lcd
  54. lcd.init();
  55. lcd.backlight();
  56. */
  57.  
  58. //ignition pulse interrupt on digital pin 2
  59. pinMode(ignitionpin, INPUT_PULLUP);
  60. attachInterrupt(digitalPinToInterrupt(ignitionpin),ignitionpulse, FALLING);
  61.  
  62.  
  63. //gear sensor pins
  64. pinMode(gear1, INPUT_PULLUP);
  65. pinMode(gear2, INPUT_PULLUP);
  66. pinMode(gear3, INPUT_PULLUP);
  67. pinMode(gear4, INPUT_PULLUP);
  68. pinMode(gear5, INPUT_PULLUP);
  69. pinMode(gear6, INPUT_PULLUP);
  70. pinMode(gear0, INPUT_PULLUP);
  71.  
  72.  
  73.  
  74. //zero the stepper motor
  75. motor1.currentStep = motor1.steps;
  76. motor2.currentStep = motor2.steps;
  77. motor1.setPosition(0);
  78. motor2.setPosition(0);
  79. while (motor1.currentStep > 0 || motor2.currentStep > 0) {
  80. motor1.update();
  81. motor2.update();
  82. }
  83.  
  84.  
  85.  
  86. delay(100);
  87. motor1.setPosition(690);
  88. motor2.setPosition(663);
  89. //motor2.setPosition(0+wheeloffset);
  90. sweeptime=millis();
  91. }
  92.  
  93.  
  94.  
  95. void loop() {
  96. /*
  97. lcd.setCursor(0,0);
  98. lcd.print(currentRPM);
  99. lcd.print(" ");
  100.  
  101. lcd.setCursor(0,1);
  102. lcd.print(RPM);
  103. lcd.print(" ");
  104. */
  105.  
  106.  
  107. if (tachswept==0){
  108. tachsweeper();
  109. }
  110.  
  111. if (tachswept==1){
  112. checkRPM();
  113. checkgear();
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120. motor1.update();
  121. motor2.update();
  122.  
  123. }
  124.  
  125.  
  126. void tachsweeper(){
  127.  
  128. if (millis()-sweeptime > 900){
  129. tachswept=1;
  130. motor1.setPosition(0);
  131. motor2.setPosition(0);
  132. }
  133.  
  134. }
  135.  
  136.  
  137. void checkRPM(){
  138. if (millis()-timea >= 150){
  139. currentRPM=0;
  140. timea=millis(); //this line can be removed???
  141. }
  142.  
  143. if (millis()-timeb >= RPMrr){
  144.  
  145. RPMarray[arrayposition]=currentRPM;
  146. arrayposition=arrayposition+1;
  147.  
  148. if (arrayposition > RPMarraysize-1){
  149. arrayposition=0;
  150.  
  151. RPM=0;
  152. for (int x=0; x<RPMarraysize; x++){
  153. RPM=RPM+RPMarray[x];
  154. }
  155. RPM=RPM/RPMarraysize;
  156. }
  157. timeb=millis();
  158.  
  159. }
  160.  
  161. needleposition=(690*RPM/12500);//set the needle to position. motor position 690=12500RPM if 0=0.
  162.  
  163. if (needleposition > 944){ //protect against running off the scale. Max RPM that can be displayed is 17119rpm
  164. needleposition=944;
  165. }
  166.  
  167. motor1.setPosition(needleposition);
  168. }
  169.  
  170.  
  171.  
  172. void ignitionpulse(){
  173.  
  174. currentRPM=60000/(millis()-deltat);
  175. deltat=millis();
  176. timea=millis();
  177.  
  178. }
  179.  
  180.  
  181.  
  182. void checkgear(){
  183.  
  184. if (digitalRead(gear1) == LOW){
  185. motor2.setPosition(124);
  186. }
  187. if (digitalRead(gear2) == LOW){
  188. motor2.setPosition(232);
  189. }
  190. if (digitalRead(gear3) == LOW){
  191. motor2.setPosition(340);
  192. }
  193. if (digitalRead(gear4) == LOW){
  194. motor2.setPosition(448);
  195. }
  196. if (digitalRead(gear5) == LOW){
  197. motor2.setPosition(557);
  198. }
  199. if (digitalRead(gear6) == LOW){
  200. motor2.setPosition(663);
  201. }
  202. if (digitalRead(gear0) == LOW){
  203. motor2.setPosition(16);
  204. }
  205.  
  206.  
  207. }
Advertisement
Add Comment
Please, Sign In to add comment