Rotary encoder used as infinite volume knob

I copied this code from Mr.Ralph S Bacon with these URL:
youtube: https://www.youtube.com/watch?v=J9cDEef0IbQ

code: https://dl.dropboxusercontent.com/u/20622434/RotaryEncoderInterrruptsLED.ino

The good thing of this code is using interrupt on pin3. The bad things is that I modified PinLED from pin 11 to pin 13 and it did not change the brightness of the PinLED.

The hardware connection has a note: Vcc –> 3.3V, CLK –> pin3, DT –> pin4, SW –> pin8. The range value will be [0, 100]


// Used for generating interrupts using CLK signal
const int PinA = 3;

// Used for reading DT signal
const int PinB = 4;

// Used for the push button switch
const int PinSW = 8;

// Simple PWM LED pin
#define PinLED 13

// Keep track of last rotary value
int lastCount = 50;

// Updated by the ISR (Interrupt Service Routine)
volatile int virtualPosition = 50;

// ------------------------------------------------------------------
// INTERRUPT INTERRUPT INTERRUPT INTERRUPT INTERRUPT 
// ------------------------------------------------------------------
void isr () {
 static unsigned long lastInterruptTime = 0;
 unsigned long interruptTime = millis();

// If interrupts come faster than 5ms, assume it's a bounce and ignore
 if (interruptTime - lastInterruptTime > 5) {
 if (digitalRead(PinB) == LOW)
 {
 virtualPosition-- ; // Could be -5 or -10
 }
 else {
 virtualPosition++ ; // Could be +5 or +10
 }

// Restrict value from 0 to +100
 virtualPosition = min(100, max(0, virtualPosition));

// Keep track of when we were here last (no more than every 5ms)
 lastInterruptTime = interruptTime;
 }
}

// ------------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP 
// ------------------------------------------------------------------
void setup() {
 // Just whilst we debug, view output on serial monitor
 Serial.begin(9600);

// Rotary pulses are INPUTs
 pinMode(PinA, INPUT);
 pinMode(PinB, INPUT);
 pinMode(PinLED, OUTPUT);
 analogWrite(PinLED, virtualPosition);

// Switch is floating so use the in-built PULLUP so we don't need a resistor
 pinMode(PinSW, INPUT_PULLUP);

// Attach the routine to service the interrupts
 attachInterrupt(digitalPinToInterrupt(PinA), isr, LOW);

// Ready to go!
 Serial.println("Start");
}

// ------------------------------------------------------------------
// MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP MAIN LOOP
// ------------------------------------------------------------------
void loop() {

// Is someone pressing the rotary switch?
 if ((!digitalRead(PinSW))) {
 virtualPosition = 50;
 while (!digitalRead(PinSW))
 delay(10);
 Serial.println("Reset");
 }

// If the current rotary switch position has changed then update everything
 if (virtualPosition != lastCount) {
 
 // Our LED gets brighter or dimmer
 analogWrite(PinLED, virtualPosition);

// Write out to serial monitor the value and direction
 Serial.print(virtualPosition > lastCount ? "Up :" : "Down:");
 Serial.println(virtualPosition);

// Keep track of this new value
 lastCount = virtualPosition ;
 }
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s