/* * This information must be maintained if you publish this. * wwww.plastibots.com * * Credit to PJRC.com * Encoder Library - Basic Example * http://www.pjrc.com/teensy/td_libs_Encoder.html * https://github.com/tinker-fun/Teensy-USB-Shortcut-Button/blob/master/shortcutButton.ino * * This example code is in the public domain. */ #define ENCODER_OPTIMIZE_INTERRUPTS #include #include "OneButton.h" #include #include //Teensy Pinouts board vs code https://forum.pjrc.com/threads/24928-Teensy2-0-different-Vcc-GND-Pins // Change these pin numbers to the pins connected to your encoder. // Best Performance: both pins have interrupt capability // Good Performance: only the first pin has interrupt capability // Low Performance: neither pin has interrupt capability Encoder dialKnob(6, 5); //D0 and D1 on knockoff board. //Init the OneButton instance on the push button OneButton button(7, true); // Teensy 2.0 has the LED on pin 11 // Teensy++ 2.0 has the LED on pin 6 // Teensy 3.x / Teensy LC have the LED on pin 13 const int ledPin = 11; //built in const int motorPin = 10; //C7 on board //const int btnPin = 7; //D2 on board long dialPos = -999; //char ctrlKey = KEY_LEFT_CTRL; int tickCount = 0; boolean singleClickState = false; boolean doubleClickState = false; boolean longClickState = false; void setup() { // initialize the buttons' inputs: pinMode(ledPin, OUTPUT); pinMode(motorPin, OUTPUT); //Button states to call functions button.attachClick(myClickFunction); button.attachDoubleClick(doubleclick); button.attachLongPressStart(longPressStart); button.attachLongPressStop(longPressStop); button.attachDuringLongPress(longPress); Serial.begin(9600); Serial.println("Initializing..."); vibe(300, 1); // initialize mouse control: //Mouse.begin(); //Keyboard.begin(); //delay(5000); } void loop() { button.tick(); delay(10); long newDialPos; newDialPos = dialKnob.read(); if (newDialPos != dialPos) { if (tickCount == 2) //each tick sends 2 readings. Want haptic to respnd with dial feel not numbers { vibe(50, 1); blink(); Serial.print(" Tick = "); Serial.println(tickCount); tickCount=0; } if(singleClickState) { Mouse.press(MOUSE_RIGHT); //clicks the mouse button and does not release. Follows by Mouse.release MOUSE_LEFT, MOUSE_MIDDLE //Mouse.move(xVal, yPos, wheel); Mouse.move((dialPos-newDialPos)*3, 0); // move right delay(50); Mouse.release(MOUSE_RIGHT); } //note each state will disable the other states - scrolling or zooming in/out (TinkerCAD) if(doubleClickState) { if (newDialPos > dialPos) { Mouse.move(0, 0, -1); } else { Mouse.move(0, 0, 1); } } Serial.print("Dial = "); Serial.print(newDialPos); Serial.println(); dialPos = newDialPos; tickCount ++; } } //END LOOP void myClickFunction() { Serial.println("Button 1 CLICK..."); singleClickState = !singleClickState; Serial.println(singleClickState); doubleClickState = false; //make sure it's not in another state longClickState = false; //make sure it's not in another state } void doubleclick() { Serial.println("Button 1 doubleclick..."); doubleClickState = !doubleClickState; Serial.println(doubleClickState); singleClickState = false; //make sure it's not in another state longClickState = false; //make sure it's not in another state } void longPress() { Serial.println("Button 1 longPress..."); ///Note this keeps repeating when holding longpress so will need a catch in there. } void longPressStart() { Serial.println("Button 1 longPress Start..."); longClickState = true; } // This function will be called once, when the button1 is released after beeing pressed for a long time. void longPressStop() { Serial.println("Button 1 longPress stop"); longClickState = false; } void vibe(int howLong, int howMany) { for (int i=0; i < howMany; i++){ digitalWrite(motorPin, HIGH); delay(howLong); digitalWrite(motorPin, LOW); delay(howLong); } } void blink() { digitalWrite(ledPin, HIGH); // set the LED on delay(50); // wait for a bit digitalWrite(ledPin, LOW); // set the LED off delay(50); // wait for a bit } /* //Mouse.click(MOUSE_RIGHT); //one time click and unlclick MOUSE_LEFT, MOUSE_MIDDLE Mouse.press(MOUSE_RIGHT); //clicks the mouse button and does not release. Follows by Mouse.release MOUSE_LEFT, MOUSE_MIDDLE //Mouse.move(xVal, yPos, wheel); //Mouse.move(40, 0); for (int i=0; i <= 20; i++){ Mouse.move(i, 0); // move right delay(50); } delay(1000); for (int i=0; i >= -20; i--){ Mouse.move(i, 0); // move left delay(50); } //Mouse.move(-40, 0); // move mouse left Mouse.release(MOUSE_RIGHT); delay(2000); //Mouse.move(0,0,1); //move the wheel. //delay(100); for (int i=0; i <= 3; i++){ Mouse.move(0, 0, i); // scroll delay(400); } for (int i=0; i >= -3; i--){ Mouse.move(0, 0, i); // scroll delay(400); } delay(2000); //Mouse.click(MOUSE_LEFT); //Keyboard.write('u'); //Keyboard.print("Hello!"); */ /* void watchButton() { if (millis() != t) { // check analog pin for the button value and save it to ButtonVal ButtonVal = digitalRead(btnPin); if(ButtonVal == current_state && counter >0) { counter--; } if(ButtonVal != current_state) { counter++; } // If ButtonVal has shown the same value for long enough let's switch it if (counter >= debounce_count) { counter = 0; current_state = ButtonVal; //Checks which button or button combo has been pressed if (ButtonVal > 0) { //BUTTON IS CONFIRMED CLICKED - DO WORK HERE vibe(20, 1); blink(); Serial.println("Button pressed!"); } } t = millis(); } } */