Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //STATIC INFO
- #define START_TRYK 13
- #define JOG_PLUS 9
- #define JOG_MINUS 12
- #define ENDESTOP_PLUS 11
- #define ENDESTOP_MINUS 10
- #define PIN_STEP 2
- #define PIN_DIR 5
- #define PIN_EN 8
- #define PIN_LED 7
- #define ACCELERATE_Q 3
- #define MAX_SPEED 7000
- #define BLINK_INTERVAL 100000
- #define INTERVAL 1000
- //GLOBAL VARIABLES
- bool running = 0;
- int frq = 0;
- int currentFrq = 0;
- int retning = 0;
- bool nyStart = 1;
- void setup()
- {
- pinMode(START_TRYK, INPUT);
- pinMode(JOG_PLUS, INPUT);
- pinMode(JOG_MINUS, INPUT);
- pinMode(ENDESTOP_PLUS, INPUT);
- pinMode(ENDESTOP_MINUS, INPUT);
- pinMode(PIN_STEP, OUTPUT);
- pinMode(PIN_DIR, OUTPUT);
- pinMode(PIN_EN, OUTPUT);
- pinMode(PIN_LED, OUTPUT);
- nyStart = 1;
- }
- void loop()
- {
- if(digitalRead(ENDESTOP_PLUS) == LOW) { retning = 2; }
- if(digitalRead(ENDESTOP_MINUS) == LOW) { retning = 1; }
- if(digitalRead(START_TRYK) == LOW && retning != 0) {running = 1;}
- //ALMINDELIG DRIFT
- while(running == 1 & digitalRead(START_TRYK) == HIGH) //Almindelig drift
- {
- while(retning == 1) //retning plus
- {
- currentFrq = (31+((MAX_SPEED/1000)*analogRead(A0)));
- if(digitalRead(ENDESTOP_PLUS) == LOW) { retning = 2; }
- if(digitalRead(START_TRYK) == LOW) { running = 0; retning = 0; break; }
- if(digitalRead(PIN_DIR) == HIGH && nyStart == 0)
- {
- while(frq > 31) //Decellerer
- {
- frq = (frq - ACCELERATE_Q);
- tone(PIN_STEP, frq);
- nyStart = 0;
- }
- }
- digitalWrite(PIN_DIR, LOW);
- if(nyStart == 1) { frq = 31; }
- while(frq < currentFrq) //accelerate
- {
- frq = (frq + ACCELERATE_Q);
- tone(PIN_STEP, frq);
- nyStart = 0;
- }
- tone(PIN_STEP, currentFrq);
- }
- while(retning == 2) //retning minus
- {
- currentFrq = (31+((MAX_SPEED/1000)*analogRead(A0)));
- if(digitalRead(ENDESTOP_MINUS) == LOW) { retning = 1; }
- if(digitalRead(START_TRYK) == LOW) { running = 0; retning = 0; break; }
- if(digitalRead(PIN_DIR) == LOW && nyStart == 0)
- {
- while(frq > 31) //Decellerer
- {
- frq = (frq - ACCELERATE_Q);
- tone(PIN_STEP, frq);
- nyStart = 0;
- }
- }
- digitalWrite(PIN_DIR, HIGH);
- if(nyStart == 1) { frq = 31; }
- while(frq < currentFrq) //accelerate
- {
- frq = (frq + ACCELERATE_Q);
- tone(PIN_STEP, frq);
- nyStart = 0;
- }
- tone(PIN_STEP, currentFrq);
- }
- if(digitalRead(START_TRYK) == LOW) { running = 0; retning = 0; }
- }
- //JOG DRIFT
- while(digitalRead(JOG_PLUS) == LOW)
- {
- digitalWrite(PIN_DIR, LOW);
- currentFrq = (31+((MAX_SPEED/1000)*analogRead(A0)));
- while(frq < currentFrq) //accelerate
- {
- frq = (frq + ACCELERATE_Q);
- tone(PIN_STEP, frq);
- }
- tone(PIN_STEP, currentFrq);
- }
- while(digitalRead(JOG_MINUS) == LOW)
- {
- digitalWrite(PIN_DIR, HIGH);
- currentFrq = (31+((MAX_SPEED/1000)*analogRead(A0)));
- while(frq < currentFrq) //accelerate
- {
- frq = (frq + ACCELERATE_Q);
- tone(PIN_STEP, frq);
- }
- tone(PIN_STEP, currentFrq);
- }
- //Reset til nul
- if(currentFrq >= 31)
- {
- while(currentFrq >= 31)
- {
- currentFrq = (currentFrq - ACCELERATE_Q);
- tone(PIN_STEP, currentFrq);
- }
- noTone(PIN_STEP);
- frq = 0;
- }
- nyStart = 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement