Hello.
I have 2 Ky-40 encoders and 1 Adafruits neotrellis button matrix on an Arduino uno.
The bug is with encoders. I know its a code bug because both behave exactly in the same way.
If I turn them to the right the give the correct value, but from time to time they give the left turn value.
If I turn the left they always give the correct value.
I think is a timing thing.
Here is a clue, if I put a delay at the end of the Loop, the greater the delay, the more often the wrong
value. If i put a delay of 20 ms or greater, they always give the wrong value.
I try different Baud rates with no luck.
Pleaseeee!!!
#include "Adafruit_NeoTrellis.h"
#define CLK 2
#define DT 3
#define SW 5
#define CLK2 7
#define DT2 10
#define SW2 11
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;
int currentStateCLK2;
int lastStateCLK2;
String currentDir2 ="";
unsigned long lastButtonPress2 = 0;
Adafruit_NeoTrellis trellis;
TrellisCallback blink(keyEvent evt){
if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_RISING) {
Serial.println(evt.bit.NUM);
} else if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_FALLING) {
// Serial.write(evt.bit.NUM);
}
return 0;
}
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char messageFromPC[numChars] = {0};
int a = 0;
int b = 0;
int c = 0;
int d = 0;
boolean newData = false;
void setup() {
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT);
pinMode(CLK2,INPUT);
pinMode(DT2,INPUT);
pinMode(SW2, INPUT);
Serial.begin(9600);
lastStateCLK = digitalRead(CLK);
lastStateCLK2 = digitalRead(CLK2);
!trellis.begin();
for(int i=0; i<NEO_TRELLIS_NUM_KEYS; i++){
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
trellis.registerCallback(i, blink);
}
}
///////////////////////////////////////////////////////////
void loop1(void) {
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
if (digitalRead(DT) != currentStateCLK) {
currentDir ="1000";
} else {
currentDir ="1001";
}
Serial.println(currentDir);
}
lastStateCLK = currentStateCLK;
int btnState = digitalRead(SW);
if (btnState == LOW) {
if (millis() - lastButtonPress > 50) {
Serial.println("1002");
}
lastButtonPress = millis();
}
}
//////////////////////////////////////////////////////////////////
void loop2(void) {
currentStateCLK2 = digitalRead(CLK2);
if (currentStateCLK2 != lastStateCLK2 && currentStateCLK2 == 1){
if (digitalRead(DT2) != currentStateCLK2) {
currentDir2 ="1003";
} else {
currentDir2 ="1004";
}
Serial.println(currentDir2);
}
lastStateCLK2 = currentStateCLK2;
int btnState2 = digitalRead(SW2);
if (btnState2 == LOW) {
if (millis() - lastButtonPress2 > 50) {
Serial.println("1005");
}
lastButtonPress2 = millis();
}
}
/////////////////////////////////////////////////////////////////////
void loop(void) {
trellis.read();
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
newData = false;
}
loop1();
loop2();
}
/////////////////////////////////////////////////////////////////////
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
///////////////////////////////////////////////////////////////////
void parseData() {
char * strtokIndx;
strtokIndx = strtok(tempChars," ");
strcpy(messageFromPC, strtokIndx);
strtokIndx = strtok(NULL, " ");
a = atoi(strtokIndx);
strtokIndx = strtok(NULL, " ");
b = atoi(strtokIndx);
strtokIndx = strtok(NULL, " ");
c = atoi(strtokIndx);
strtokIndx = strtok(NULL, " ");
d = atoi(strtokIndx);
trellis.pixels.setPixelColor(a,b,c,d);
trellis.pixels.show();
}