Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //A0 is Right Rotation.
- //A1 is Left Rotation.
- //A2 is Right Pressure.
- //A3 is Left Pressure.
- // 2 is Encoder Pin 1.
- //~3 is Encoder Pin 2.
- int cur = 0;
- int dur = 0;
- boolean LeftFoot = false;
- boolean RightFoot = false;
- unsigned long walked = 0;
- unsigned long accum = 0;
- //unsigned long walked1 = 0;
- int encoderPin1 = 2;
- int encoderPin2 = 3;
- volatile int lastEncoded = 0;
- volatile long encoderValue = 0;
- long lastencoderValue = 0;
- int lastMSB = 0;
- int lastLSB = 0;
- void setup() {
- pinMode(encoderPin1, INPUT);
- pinMode(encoderPin2, INPUT);
- digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
- digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
- //call updateEncoder() when any high/low changed seen
- //on interrupt 0 (pin 2), or interrupt 1 (pin 3)
- attachInterrupt(0, updateEncoder, CHANGE);
- attachInterrupt(1, updateEncoder, CHANGE);
- cur = analogRead(A0);
- dur = analogRead(A1);
- Mouse.begin();
- Keyboard.begin();
- }
- void loop() {
- if((millis()>walked+100)&&(walked!=0)){
- Keyboard.releaseAll();
- walked = 0;
- }
- //if((millis()>walked1+50)&&(walked1!=0)){
- // Keyboard.release('s');
- // walked1 = 0;
- //}
- if((!LeftFoot)&&(analogRead(A3)>=900)){
- LeftFoot = true;
- }
- if((!RightFoot)&&(analogRead(A2)>=900)){
- RightFoot = true;
- }
- if((LeftFoot)&&(analogRead(A3)<900)){
- LeftFoot = false;
- }
- if((RightFoot)&&(analogRead(A2)<900)){
- RightFoot = false;
- }
- if(!((analogRead(A2)>500)&&(analogRead(A3)>900))){
- if (abs(cur-analogRead(A0))>2){
- if((RightFoot)&&(!LeftFoot)) {
- Mouse.move((cur-analogRead(A0))*-2.8444,0);
- }
- cur = analogRead(A0);
- }
- else if (abs(dur-analogRead(A1))>2){
- if((LeftFoot)&&(!RightFoot)){
- Mouse.move((dur-analogRead(A1))*-2.8444,0);
- }
- dur = analogRead(A1);
- }
- }
- accum++;
- if(accum==100){
- if ((encoderValue>3)&&(encoderValue<30)){
- walked = millis();
- Keyboard.release(KEY_LEFT_SHIFT);
- Keyboard.press(KEY_LEFT_ALT);
- Keyboard.press('w');
- }else if((encoderValue>=30)&&(encoderValue<60)){
- walked = millis();
- Keyboard.release(KEY_LEFT_SHIFT);
- Keyboard.release(KEY_LEFT_ALT);
- Keyboard.press('w');
- }else if(encoderValue>=60){
- walked = millis();
- Keyboard.release(KEY_LEFT_ALT);
- Keyboard.press(KEY_LEFT_SHIFT);
- Keyboard.press('w');
- }
- encoderValue = 0;
- accum = 0;
- }
- //Serial.println(encoderValue);
- delay(1);
- }
- void updateEncoder(){
- int MSB = digitalRead(encoderPin1); //MSB = most significant bit
- int LSB = digitalRead(encoderPin2); //LSB = least significant bit
- int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
- int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value
- if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011){
- encoderValue++;
- }
- if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000){
- encoderValue--;
- // walked1 = millis();
- // Keyboard.press('s');
- }
- lastEncoded = encoded; //store this value for next time
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement