Advertisement
Guest User

Simulacrum w/ Treadmill Forwards

a guest
Aug 28th, 2012
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 KB | None | 0 0
  1. //A0 is Right Rotation.
  2. //A1 is Left Rotation.
  3. //A2 is Right Pressure.
  4. //A3 is Left Pressure.
  5. // 2 is Encoder Pin 1.
  6. //~3 is Encoder Pin 2.
  7.  
  8. int cur = 0;
  9. int dur = 0;
  10.  
  11. boolean LeftFoot = false;
  12. boolean RightFoot = false;
  13.  
  14. unsigned long walked = 0;
  15. unsigned long accum = 0;
  16. //unsigned long walked1 = 0;
  17.  
  18. int encoderPin1 = 2;
  19. int encoderPin2 = 3;
  20.  
  21. volatile int lastEncoded = 0;
  22. volatile long encoderValue = 0;
  23.  
  24. long lastencoderValue = 0;
  25.  
  26. int lastMSB = 0;
  27. int lastLSB = 0;
  28. void setup() {
  29.   pinMode(encoderPin1, INPUT);
  30.   pinMode(encoderPin2, INPUT);
  31.  
  32.   digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  33.   digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
  34.  
  35.   //call updateEncoder() when any high/low changed seen
  36.   //on interrupt 0 (pin 2), or interrupt 1 (pin 3)
  37.   attachInterrupt(0, updateEncoder, CHANGE);
  38.   attachInterrupt(1, updateEncoder, CHANGE);
  39.  
  40.   cur = analogRead(A0);
  41.   dur = analogRead(A1);
  42.   Mouse.begin();
  43.   Keyboard.begin();
  44. }
  45.  
  46. void loop() {
  47.   if((millis()>walked+100)&&(walked!=0)){
  48.     Keyboard.releaseAll();
  49.     walked = 0;
  50.   }
  51.   //if((millis()>walked1+50)&&(walked1!=0)){
  52.   //  Keyboard.release('s');
  53.   //  walked1 = 0;
  54.   //}
  55.   if((!LeftFoot)&&(analogRead(A3)>=900)){
  56.     LeftFoot = true;
  57.   }
  58.   if((!RightFoot)&&(analogRead(A2)>=900)){
  59.     RightFoot = true;
  60.   }
  61.   if((LeftFoot)&&(analogRead(A3)<900)){
  62.     LeftFoot = false;
  63.   }
  64.   if((RightFoot)&&(analogRead(A2)<900)){
  65.     RightFoot = false;
  66.   }
  67.   if(!((analogRead(A2)>500)&&(analogRead(A3)>900))){
  68.     if (abs(cur-analogRead(A0))>2){
  69.       if((RightFoot)&&(!LeftFoot)) {
  70.         Mouse.move((cur-analogRead(A0))*-2.8444,0);
  71.       }
  72.       cur = analogRead(A0);
  73.     }
  74.     else if (abs(dur-analogRead(A1))>2){
  75.       if((LeftFoot)&&(!RightFoot)){
  76.         Mouse.move((dur-analogRead(A1))*-2.8444,0);
  77.       }
  78.       dur = analogRead(A1);
  79.     }
  80.   }
  81.  
  82.   accum++;
  83.   if(accum==100){
  84.     if ((encoderValue>3)&&(encoderValue<30)){
  85.       walked = millis();
  86.       Keyboard.release(KEY_LEFT_SHIFT);
  87.       Keyboard.press(KEY_LEFT_ALT);
  88.       Keyboard.press('w');
  89.     }else if((encoderValue>=30)&&(encoderValue<60)){
  90.       walked = millis();
  91.       Keyboard.release(KEY_LEFT_SHIFT);
  92.       Keyboard.release(KEY_LEFT_ALT);
  93.       Keyboard.press('w');
  94.     }else if(encoderValue>=60){
  95.       walked = millis();
  96.       Keyboard.release(KEY_LEFT_ALT);
  97.       Keyboard.press(KEY_LEFT_SHIFT);
  98.       Keyboard.press('w');
  99.     }
  100.     encoderValue = 0;
  101.     accum = 0;
  102.   }
  103.       //Serial.println(encoderValue);
  104.   delay(1);
  105. }
  106.  
  107. void updateEncoder(){
  108.   int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  109.   int LSB = digitalRead(encoderPin2); //LSB = least significant bit
  110.  
  111.   int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  112.   int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
  113.  
  114.   if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011){
  115.     encoderValue++;
  116.   }
  117.   if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000){
  118.    encoderValue--;
  119.    // walked1 = millis();
  120.    // Keyboard.press('s');
  121.   }
  122.   lastEncoded = encoded; //store this value for next time
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement