Set up:
Arduino UNO R3
Lcd display con iC2
Este encoder
Lo que quiero hacer;
Un menu que se desplace de acuerdo a los pasos del encoder, que una vez que des clic en algun item del menu, se abra un sub-menu o la posibilidad de modificar una variable.
En lo que estoy:
El encoder funciona, me instale un sketch que permite contar la posiciones desplazadas, si es giro izquierdo o derecho todo bien.
No Puedo leer el clik!
Este es el sketch que lee el encoder;
const byte pinA = 2; // encoder pin A to Arduino pin 2 which is also interrupt pin 0 which we will use
const byte pinB = 3; // encoder pin B to Arduino pin 3 which is also interrupt pin 1 but we won't use it
byte state = 0; // will store two bits for pins A & B on the encoder which we will get from the pins above
int level = 0; // a value bumped up or down by the encoder
/* For demo purposes we will create an array of these binary digits */
String bits[] = {"00","01","10","11"};
/* A truth table of possible moves
1 for clockwise
-1 for counter clockwwise
0 for error - keybounce */
int bump[] = {0,0,-1,1};
void setup(){
pinMode(pinA,INPUT); // reads Pin A of the encoder
pinMode(pinB,INPUT); // reads Pin B of the encoder
/* Writing to an Input pin turns on an internal pull up resistor */
digitalWrite(pinA,HIGH);
digitalWrite(pinB,HIGH);
/* Set up to call our knob function any time pinA rises */
attachInterrupt(0,knobTurned,RISING); // calls our 'knobTurned()' function when pinA goes from LOW to HIGH
level = 50; // a value to start with
/* Set up for using the on-screen monitor */
Serial.begin(115200); // make sure your monitor baud rate matches this
Serial.println("Encoder Ready");
Serial.print("level = ");
Serial.println(level); // to remind us where we're starting
}
void loop(){
/* main programming will go here later
for now we'll just do nothing until we get an interrupt */
}
void knobTurned(){
/* AH HA! the knob was turned */
state = 0; // reset this value each time
state = state + digitalRead(pinA); // add the state of Pin A
state <<= 1; // shift the bit over one spot
state = state + digitalRead(pinB); // add the state of Pin B
/* now we have a two bit binary number that holds the state of both pins
00 - something is wrong we must have got here with a key bounce
01 - sames as above - first bit should never be 0
10 - knob was turned backwards
11 - knob was turned forwards
*/
/* We can pull a value out of our truth table and add it to the current level */
level = level + bump[state];
/* Let's see what happened */
Serial.print(bits[state] + " "); // show us the two bits
Serial.print(bump[state],DEC); // show us the direction of the turn
Serial.print(" ");
Serial.println(level); // show us the new value
}
La hoja de datos establece que para las terminales D y E.. existe un contacto NO.
YA LO COMPROBE CON EL MULTIMETRO...
Asi que conecto una de las terminales a 5V y la otra al pin 4 para leer su estado.
Resultado;
Comienza a contar de 0 hacia delante ta pronto como se termina el set up... ya intente poner una pulldown a tierra, pero asi no me lee nada.
Este es el código que utilizo;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Bounce2.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
byte state = 0; // will store two bits for pins A & B on the encoder which we will get from the pins above
int level = 0; // a value bumped up or down by the encoder
int bump[] = {0,0,-1,1};
String bits[] = {"00","01","10","11"};
const byte pinA = 2; // encoder pin A to Arduino pin 2 which is also interrupt pin 0 which we will use
const byte pinB = 3; // encoder pin B to Arduino pin 3 which is also interrupt pin 1 but we won't use it
const byte pinC = 4; //lee click
int conteo = 0;
int stado = 0;
//Bounce debouncer = Bounce(); // bouncer object
void setup(){
//debouncer.attach(pinC);
//debouncer.interval(5); // interval in ms
lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight
lcd.backlight();
pinMode(pinA,INPUT); // reads Pin A of the encoder
pinMode(pinB,INPUT); // reads Pin B of the encoder
pinMode(pinC,INPUT); // reads Pin B of the encoder
/* Writing to an Input pin turns on an internal pull up resistor */
digitalWrite(pinA,HIGH);
digitalWrite(pinB,HIGH);
attachInterrupt(0,knobTurned,RISING); // calls our 'knobTurned()' function when pinA goes from LOW to HIGH
level = 50; // a value to start with
Serial.begin(115200); // make sure your monitor baud rate matches this
Serial.println("Encoder Ready");
Serial.print("level = ");
Serial.println(level); // to remind us where we're starting
}
void loop(){
//debouncer.update();
stado= digitalRead(pinC);
if(stado == HIGH){
conteo++;
lcd.setCursor(0,0);
lcd.print("El conteo es");
lcd.setCursor(0,1);
lcd.print(conteo); }
}
void knobTurned(){
/* AH HA! the knob was turned */
state = 0; // reset this value each time
state = state + digitalRead(pinA); // add the state of Pin A
state <<= 1; // shift the bit over one spot
state = state + digitalRead(pinB); // add the state of Pin B
level = level + bump[state];
/* Let's see what happened */
Serial.print(bits[state] + " "); // show us the two bits
Serial.print(bump[state],DEC); // show us the direction of the turn
Serial.print(" ");
Serial.println(level); // show us the new value
}
De momento removí el bounce, por que pensé que le habia configurado mal.
Carge un sketch de los ejemplos del arduino, donde se enciende un led cada vez que se da
clik al boton... y funciona... cada vez que doy clik al encoder se enciende el led del pin 13...
Igual algo hice mal en el codigo... han sido dias de dormir poco.
Muchas gracias de antemano!!! 8)