Hello
It's my first message and I need some help.
I use Arduino Mega 2560 (clone), 8*8
led display, and rotary encoder KY040.
Project is to display a digit on 8*8
led display and increment or decrement it with the rotary encoder.
The code (below) is very simple, but when I turn the rotary digits change not regulary. Sometimes not change, sometime skip a digit.
I turn slowly or quickly, never got a good incrementation.
I change rotary with another ref, same result.
An idea ?
Mayby I have to tru another biblioteca ?
Many thanks for your help.
JM
#include <LedControl.h>
#include <RotaryEncoder.h>
int CS = 8;
int DIN = 9;
int CLK = 10;
//Déclaration des pins du codeur rotatif
RotaryEncoder encoder(2, 3); // (DT, CLK)
static int pos = 0;
static int compteur = 0;
int newPos;
// digits 0 to 9
byte chiffre[][8] ={
{B00000000, B00111000, B01000100, B01001100, B01010100, B01100100, B01000100, B00111000},
{B00000000, B00010000, B00110000, B00010000, B00010000, B00010000, B00010000, B00111000},
{B00000000, B00111000, B01000100, B00000100, B00001000, B00010000, B00100000, B01111100},
{B00000000, B01111100, B00001000, B00010000, B00001000, B00000100, B01000100, B00111000},
{B00000000, B00001000, B00011000, B00101000, B01001000, B01111100, B00001000, B00001000},
{B00000000, B01111100, B01000000, B01111000, B00000100, B00000100, B01000100, B00111000},
{B00000000, B00011000, B00100000, B01000000, B01111000, B01000100, B01000100, B00111000},
{B00000000, B01111100, B00000100, B00001000, B00010000, B00100000, B01000000, B01000000},
{B00000000, B00111000, B01000100, B01000100, B00111000, B01000100, B01000100, B00111000},
{B00000000, B00111000, B01000100, B01000100, B00111100, B00000100, B00001000, B00110000}
};
LedControl lc=LedControl(DIN,CLK,CS,0);
void setup(){
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(0,0); // Set the brightness
lc.clearDisplay(0); // and clear the display
}
void loop(){
encoder.tick();
newPos = encoder.getPosition();
if (pos != newPos) {
if (newPos>pos){
if (compteur<9){ // maxi 9
compteur ++;
}
}else{
if (compteur>0){ // mini 0
compteur --;
}
}
pos = newPos;
}
// display the digit
printByte(chiffre[compteur]);
}
// displaying on 8*8 led
void printByte(byte character []){
int i = 0;
for(i=0;i<8;i++) {
lc.setRow(0,i,character[i]);
}
}