Guest User

Untitled

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