Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. // Nathan Croxton
  2.  
  3. // Further upgrades
  4. // - write data in format for csv dump
  5. // - save data to eprom every x presses
  6.  
  7. #include <AccelStepper.h>
  8. #include <MultiStepper.h>
  9. #include <LiquidCrystal.h>
  10.  
  11.  
  12. #define testCycles 1000
  13. #define noDelay true
  14. #define testFreq 2 //Hz
  15. #define motorRes 200
  16. #define screwTravel 1
  17. #define screwLead 4
  18.  
  19. const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6;
  20. LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  21.  
  22. int travel = screwTravel*(motorRes/screwLead);
  23.  
  24.  
  25. #define nemaStep 13
  26. #define nemaDir 1
  27. #define nemaSleep 7
  28.  
  29. #define keyPressInt 2
  30.  
  31. // Setup Stepper Motor
  32. AccelStepper stepper(AccelStepper::DRIVER, nemaStep, nemaDir);
  33.  
  34. // Init varibale
  35. unsigned int timePerTest;
  36. unsigned int detectCount =0;
  37. unsigned int pressCount = 0;
  38.  
  39. unsigned long time;
  40. unsigned long lastDetectTime = 0;
  41.  
  42. int testCyclesSerial;
  43. int testTravelSerial;
  44. char junk = ' ';
  45.  
  46. // Interupt function
  47. void detectPress() {
  48. //if (time-lastDetectTime > 100){
  49. // increment count
  50. detectCount ++;
  51. lastDetectTime = time;
  52. //}
  53. }
  54.  
  55.  
  56. void input() {
  57. // Serial Input function obtains partameters for rotation
  58. Serial.print("Key Tester. Version 0.1.\nSONDER.\n\n");
  59.  
  60. Serial.println("\n\nEnter number of tests, Press ENTER");
  61. while (Serial.available() == 0) ; // Wait here until input buffer has a character
  62. {
  63. testCyclesSerial = Serial.parseInt(); // new command in 1.0 forward
  64. Serial.print("Test Cycles = "); Serial.println(testCyclesSerial, DEC);
  65. while (Serial.available() > 0) // .parseFloat() can leave non-numeric characters
  66. { junk = Serial.read() ; } // clear the keyboard buffer
  67. }
  68.  
  69. lcd.clear();
  70. lcd.setCursor(0,0);
  71. lcd.print("T.Cycles:");
  72. lcd.print(testCyclesSerial);
  73.  
  74.  
  75.  
  76. Serial.println("Enter travel for each test (mm), Press ENTER");
  77. while (Serial.available() == 0) ;
  78. {
  79. testTravelSerial = Serial.parseInt();
  80. Serial.print("Test Travel= "); Serial.println(testTravelSerial, DEC);
  81. while (Serial.available() > 0)
  82. { junk = Serial.read() ; }
  83. Serial.print("\n\Test will begin program in 3 seconds\n");
  84. }
  85.  
  86. lcd.setCursor(0,1);
  87. lcd.print("T.Travel:");
  88. lcd.print(testTravelSerial);
  89.  
  90. // Short delay following user input to prepare for photography
  91. delay(2000);
  92.  
  93. lcd.clear();
  94. lcd.setCursor(0,0);
  95. lcd.print("Beginning test shortly");
  96.  
  97. delay(2000);
  98.  
  99. }
  100.  
  101. // Move stepper to press key
  102. void downStroke(){
  103. // Change this value to modify travel of system
  104. stepper.moveTo(travel);
  105.  
  106. digitalWrite(nemaSleep, LOW);
  107. stepper.runToPosition();
  108. digitalWrite(nemaSleep, HIGH);
  109. }
  110.  
  111.  
  112. // Return stepper to starting postion
  113. void upStroke(){
  114. stepper.moveTo(500);
  115.  
  116. digitalWrite(nemaSleep, LOW);
  117. stepper.runToPosition();
  118. digitalWrite(nemaSleep, HIGH);
  119. }
  120.  
  121. void setup() {
  122. // put your setup code here, to run once:
  123.  
  124. stepper.setMaxSpeed(1000);
  125. stepper.setAcceleration(500);
  126.  
  127. // Begin Serial
  128. Serial.begin(9600);
  129.  
  130. // Setup interupt to trigger at keypress. May need to be debounced depending on outcome of testing.
  131. attachInterrupt(digitalPinToInterrupt(keyPressInt), detectPress, FALLING);
  132.  
  133. // Used to sleep stepper motor when not in use
  134. pinMode(nemaSleep, OUTPUT);
  135. digitalWrite(nemaSleep, HIGH);
  136.  
  137.  
  138. lcd.begin(16, 2);
  139. // Print a message to the LCD.
  140. lcd.print("Key Tester");
  141. lcd.setCursor(0,1);
  142. lcd.print("Use 9600 Serial");
  143.  
  144. }
  145.  
  146. void loop() {
  147. // put your main code here, to run repeatedly:
  148. input();
  149. while (pressCount < testCyclesSerial) {
  150.  
  151. // Drive the stepper
  152. downStroke();
  153. upStroke();
  154. pressCount++;
  155.  
  156. // Use short delay to achieve the desired frequency
  157. if (noDelay == false){
  158. delay((1/testFreq) *1000);
  159. }
  160.  
  161. //timePerTest = testCycles - pressCount;
  162.  
  163.  
  164. // Print results of press
  165.  
  166. lcd.clear();
  167. lcd.setCursor(0,0);
  168. lcd.print("P. Count: ");
  169. lcd.print(pressCount);
  170. lcd.setCursor(0,1);
  171. lcd.print("D. Count: ");
  172. lcd.print(detectCount);
  173.  
  174.  
  175. Serial.print("Press ");
  176. Serial.print(pressCount);
  177. Serial.print(" Complete - ");
  178. Serial.print(detectCount);
  179. Serial.println(" presses detected.");
  180.  
  181. }
  182.  
  183. // Print info following test
  184. Serial.println("Test is complete");
  185. Serial.print("pressCount: ");
  186. Serial.println(pressCount);
  187. Serial.print("detectCount: ");
  188. Serial.println(detectCount);
  189.  
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement