Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Nathan Croxton
- // Further upgrades
- // - write data in format for csv dump
- // - save data to eprom every x presses
- #include <AccelStepper.h>
- #include <MultiStepper.h>
- #include <LiquidCrystal.h>
- #define testCycles 1000
- #define noDelay true
- #define testFreq 2 //Hz
- #define motorRes 200
- #define screwTravel 1
- #define screwLead 4
- const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 6;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- int travel = screwTravel*(motorRes/screwLead);
- #define nemaStep 13
- #define nemaDir 1
- #define nemaSleep 7
- #define keyPressInt 2
- // Setup Stepper Motor
- AccelStepper stepper(AccelStepper::DRIVER, nemaStep, nemaDir);
- // Init varibale
- unsigned int timePerTest;
- unsigned int detectCount =0;
- unsigned int pressCount = 0;
- unsigned long time;
- unsigned long lastDetectTime = 0;
- int testCyclesSerial;
- int testTravelSerial;
- char junk = ' ';
- // Interupt function
- void detectPress() {
- //if (time-lastDetectTime > 100){
- // increment count
- detectCount ++;
- lastDetectTime = time;
- //}
- }
- void input() {
- // Serial Input function obtains partameters for rotation
- Serial.print("Key Tester. Version 0.1.\nSONDER.\n\n");
- Serial.println("\n\nEnter number of tests, Press ENTER");
- while (Serial.available() == 0) ; // Wait here until input buffer has a character
- {
- testCyclesSerial = Serial.parseInt(); // new command in 1.0 forward
- Serial.print("Test Cycles = "); Serial.println(testCyclesSerial, DEC);
- while (Serial.available() > 0) // .parseFloat() can leave non-numeric characters
- { junk = Serial.read() ; } // clear the keyboard buffer
- }
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("T.Cycles:");
- lcd.print(testCyclesSerial);
- Serial.println("Enter travel for each test (mm), Press ENTER");
- while (Serial.available() == 0) ;
- {
- testTravelSerial = Serial.parseInt();
- Serial.print("Test Travel= "); Serial.println(testTravelSerial, DEC);
- while (Serial.available() > 0)
- { junk = Serial.read() ; }
- Serial.print("\n\Test will begin program in 3 seconds\n");
- }
- lcd.setCursor(0,1);
- lcd.print("T.Travel:");
- lcd.print(testTravelSerial);
- // Short delay following user input to prepare for photography
- delay(2000);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Beginning test shortly");
- delay(2000);
- }
- // Move stepper to press key
- void downStroke(){
- // Change this value to modify travel of system
- stepper.moveTo(travel);
- digitalWrite(nemaSleep, LOW);
- stepper.runToPosition();
- digitalWrite(nemaSleep, HIGH);
- }
- // Return stepper to starting postion
- void upStroke(){
- stepper.moveTo(500);
- digitalWrite(nemaSleep, LOW);
- stepper.runToPosition();
- digitalWrite(nemaSleep, HIGH);
- }
- void setup() {
- // put your setup code here, to run once:
- stepper.setMaxSpeed(1000);
- stepper.setAcceleration(500);
- // Begin Serial
- Serial.begin(9600);
- // Setup interupt to trigger at keypress. May need to be debounced depending on outcome of testing.
- attachInterrupt(digitalPinToInterrupt(keyPressInt), detectPress, FALLING);
- // Used to sleep stepper motor when not in use
- pinMode(nemaSleep, OUTPUT);
- digitalWrite(nemaSleep, HIGH);
- lcd.begin(16, 2);
- // Print a message to the LCD.
- lcd.print("Key Tester");
- lcd.setCursor(0,1);
- lcd.print("Use 9600 Serial");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- input();
- while (pressCount < testCyclesSerial) {
- // Drive the stepper
- downStroke();
- upStroke();
- pressCount++;
- // Use short delay to achieve the desired frequency
- if (noDelay == false){
- delay((1/testFreq) *1000);
- }
- //timePerTest = testCycles - pressCount;
- // Print results of press
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("P. Count: ");
- lcd.print(pressCount);
- lcd.setCursor(0,1);
- lcd.print("D. Count: ");
- lcd.print(detectCount);
- Serial.print("Press ");
- Serial.print(pressCount);
- Serial.print(" Complete - ");
- Serial.print(detectCount);
- Serial.println(" presses detected.");
- }
- // Print info following test
- Serial.println("Test is complete");
- Serial.print("pressCount: ");
- Serial.println(pressCount);
- Serial.print("detectCount: ");
- Serial.println(detectCount);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement