Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ENCODER_PIN_A 2
- #define ENCODER_PIN_B 3
- #define TRIGGER_PIN 18
- volatile long encoder0Pos = 0;
- long newposition;
- long oldposition = 0;
- unsigned long newtime;
- unsigned long oldtime = 0;
- double vel = 0;
- boolean timerRunning = false;
- boolean calcStarted = false;
- long startTime = 0;
- void setup(){
- Serial.begin (9600);
- Serial.println("start"); // a personal quirk
- pinMode(ENCODER_PIN_A, INPUT);
- digitalWrite(ENCODER_PIN_A, HIGH); // turn on pullup resistor
- pinMode(ENCODER_PIN_B, INPUT);
- digitalWrite(ENCODER_PIN_B, HIGH); // turn on pullup resistor
- pinMode(TRIGGER_PIN, INPUT);
- attachInterrupt(0, encodeCount, CHANGE);
- attachInterrupt(5, beginTiming, FALLING);
- }
- void loop(){
- noInterrupts();
- newtime = millis();
- newposition = encoder0Pos;
- interrupts();
- vel = (double(newposition-oldposition)) /(double(newtime-oldtime));
- oldposition = newposition;
- oldtime = newtime;
- Serial.print(vel);
- Serial.print(", ");
- Serial.print(millis()-startTime);
- if(timerRunning){
- if(vel == 0.00){
- Serial.print(", ");
- Serial.print("Stopped Here");
- }
- }
- Serial.println();
- }
- void beginTiming(){
- if(!timerRunning){
- //startTime = millis();
- }
- timerRunning = true;
- }
- void encodeCount(){
- if (digitalRead(ENCODER_PIN_A) == digitalRead(ENCODER_PIN_B)) {
- encoder0Pos++;
- } else {
- encoder0Pos--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement