Brooke's World The life and ramblings of Brooke.

October 3, 2013

Using the tymkrs “Turn Me” with an Arduino

Filed under: Uncategorized — Tags: — Brooke @ 11:24 pm

Here is a quick write up on how to use the tymkrs “Turn Me” rotary encoder.  This supports the “push down” feature of the tymkrs kit.

RotaryEncoderDemo_bb     output_window

DSC_0249_for_web

Fritzing Rotary Encoder Part: Rotary Encoder with Knob bth.fzpz

Fritzing Project: RotaryEncoderDemo.fzz

Arduino (1.5) project: RotaryEncoderDemo.ino


 /* Read Quadrature Encoder
  * Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.
  * http://playground.arduino.cc/Main/RotaryEncoders
  * Sketch by max wolf / www.meso.net
  * v. 0.1 - very basic functions - mw 20061220
  * Sketch updated by Brooke Hedrick / www.millamilla.com
  * v. 0.2 - Added the "S" pin for encoders with push-down support - bth 09292013
  *        - Put a limit of 255 and 0 in place.  It rolls over automatically at the limits - bth 09292013
  *
  */

 int val;
 int encoder0PinA = 3;
 int encoder0PinB = 4;
 int encoder0PinS = 5;
 int encoder0PosUp = 0;
 int encoder0PosDown = 0;
 int encoder0PinALast = LOW;
 int n = LOW;
 int encoder0UpDown = LOW;

 void setup() {
   pinMode(encoder0PinA,INPUT);
   pinMode(encoder0PinB,INPUT);
   pinMode(encoder0PinS,INPUT);
   Serial.begin (9600);
 }

 void loop() {
   encoder0UpDown = digitalRead(encoder0PinS);
   n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     //Serial.print("up down:");
     //Serial.println(encoder0UpDown);
     if (digitalRead(encoder0PinB) == LOW) {
       if (encoder0UpDown == LOW) {
         encoder0PosUp--;
         if (encoder0PosUp < 0) {
             encoder0PosUp = 255;
         }
       } else {
         encoder0PosDown--;
         if (encoder0PosDown < 0) {
             encoder0PosDown = 255;
         }
       }
     } else {
       if (encoder0UpDown == LOW) {
         encoder0PosUp++;
         if (encoder0PosUp > 255) {
           encoder0PosUp = 0;
         }
       } else {
         encoder0PosDown++;
         if (encoder0PosDown > 255) {
           encoder0PosDown = 0;
         }
       }
     }
     Serial.print(encoder0PosUp);
     Serial.print(" ");
     Serial.println(encoder0PosDown);
   }
   encoder0PinALast = n;
 }

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress