Now onto my problem...
I'm using the rotary encoder with a Uno. I originally tried to use the code posted at https://learn.adafruit.com/trinket-usb- ... b?view=all, and port it over to the Uno, but that wasn't working out so I scrapped that idea and started from scratch and switched over to using the Encoder library (found at http://www.pjrc.com/teensy/td_libs_Encoder.html).
I have the encoder portion working, but I'm not able to get the push button to work. Here's how my sketch is wired: And here is my code:
Code: Select all
#include <Encoder.h>
const int ButtonPin = 1;
const int Encoder_A = 2;
const int Encoder_B = 3;
int buttonState = 0; // variable for reading the pushbutton status
// Use pins 2 & 3 for Uno
// For other boards, see http://www.pjrc.com/teensy/td_libs_Encoder.html
Encoder myEnc(Encoder_A, Encoder_B);
void setup()
{
Serial.begin(115200);
Serial.println("Encoder Test:");
pinMode(ButtonPin, INPUT);
}
long oldPosition = -999;
void loop()
{
// Handle the encoder
long newPosition = myEnc.read();
if (newPosition != oldPosition)
{
oldPosition = newPosition;
//Serial.println(newPosition);
}
// Handle the push button
// read the state of the pushbutton value:
buttonState = digitalRead(ButtonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if(buttonState == HIGH)
{
// turn LED on:
//digitalWrite(ledPin, HIGH);
Serial.println("Button is pressed");
}
else
{
// turn LED off:
//digitalWrite(ledPin, LOW);
Serial.println("Button is off");
}
}
The Encoder library requires the pins to be 2 & 3, so I moved them.
The volume knob tutorial mentions that pin 1 on the Trinket shares the LED and it acts as a pull down resistor, so I added a resistor between the push button data out pin on the encoder, and pin 1 on my Uno.
When I run my sketch, both the button on and button off states are constantly being fired. I'm stuck and could use some help.
Thanks,
Jeff