Hi guys,
I am building an audio volume controller which i have working but need a little help with alternatives for the delay function.
I need to turn 2 transistors on based on the direction of a rotary encoder, if turned clockwise it will turn one transistor on for a tenth of a second and if its turned anticlockwise it will turn the other transistor on for a tenth of a second too.
The only problem is that while the delay is happening it doesn't read the encoder so it sort of pauses the reading for a little while.
I have tried to use the blink without delay example but cant figure out how to just keep the pin high for 100 milliseconds, it seams the blink without delay sketch uses some kind of switching technique to blink the led, is that right?
I think the solution lies with the mills() function but how would I implement that? (code below)
Thanks
//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;
int volUp = 13;
int volDown = 12;
volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
void setup() {
Serial.begin (9600);
pinMode(encoderPin1, INPUT);
pinMode(encoderPin2, INPUT);
pinMode(volUp, OUTPUT);
pinMode(volDown, OUTPUT);
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);
}
void loop(){
//Do stuff here
if (encoderValue > 1)
{
digitalWrite(volUp, HIGH); // turns on transister 1 to mkae volume go up
delay(100); // waits for a tenth of a second which simulates a quick button press
digitalWrite(volUp, LOW); // turns off the transister
encoderValue = 0; // resets position value to zero
Serial.print("volume up");
}
else if (encoderValue < -1)
{
digitalWrite(volDown, HIGH); // turns on transister 1 to mkae volume go up
delay(100); // waits for a tenth of a second which simulates a quick button press
digitalWrite(volDown, LOW); // turns off the transister
encoderValue = 0; // resets position value to zero
Serial.print("volume down");
}
Serial.println(encoderValue);
delay(100); //just here to slow down the output, and show it will work even during a delay
}
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 --;
lastEncoded = encoded; //store this value for next time
}