Hi, I'm having a small issue with my rotary encoder. It seems to only ever count up (generally in multiples of two), I have a feeling it may be my logic, but I cant spot the issue. If someone can have a look, It would be much appreciated.
#include <Wire.h>
//Initialise Pins
//Encoder Pins
static int encoder0PinA = 4;
static int encoder0PinB = 5;
//Hold Button
static int holdButton = 2;
//Other Variables
//Initialise Encoder Variables
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;
int rotateFlag = 1; // Flag set if rotation has changed
//Initialise Touchscreen Variables
int x,y,ny,nx = 0; //X and Y variable holders for touch value
int halfwidth = 500; //Midpoint on Touchscreen X
int halfheight = 500; //Midpoint on Touchscreen Y
int touchFlag = 0; //Flag set if touch enabled has been sent
int prevSend = 0; // holder for previous values of y abnd x
//Initialise Hold Button Variables
int holdFlag = 0; //Set if hold button toggled
//Initialise Blinkm Variables
int redVal = 0, greenVal = 0, blueVal = 0;
int scriptFlag = 0; //Flag set if BlinkM script active
unsigned long seconds = millis(); // Used to time start of blinkm script after inactivity on touchscreen
void setup(){
//Init Hold Button
pinMode(holdButton,INPUT);
digitalWrite(holdButton,HIGH); //Pullup enabled
attachInterrupt(0,holdToggle,FALLING); //Hold Button interrupt pin 0 is pin 2
//Initialise Encoder Pins
pinMode (encoder0PinA,INPUT);
digitalWrite(encoder0PinA,HIGH); //Pullup enabled
pinMode (encoder0PinB,INPUT);
digitalWrite(encoder0PinB,HIGH); //Pullup enabled
//Init MIDI port
Serial.begin(31250);
//Init BlinkM
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send("p");
Wire.send(0x00);
Wire.send(0);
Wire.send(0);
Wire.endTransmission(); // leave I2C bus
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send("f");
Wire.send(20);
Wire.endTransmission(); // leave I2C bus
}
//Get X value from touchscreen
int readX() {
int xr=0;
pinMode(14, INPUT); // A0
pinMode(15, OUTPUT); // A1
pinMode(16, INPUT); // A2
pinMode(17, OUTPUT); // A3
digitalWrite(15, LOW); // set A1 to GND
digitalWrite(17, HIGH); // set A3 as 5V
delay(5); // short delay is required to give the analog pins time to adjust to their new roles
xr=analogRead(0) - 50; // return xr;
}
//Get Y value from touchscreen
int readY(){
int yr=0;
pinMode(14, OUTPUT); // A0
pinMode(15, INPUT); // A1
pinMode(16, OUTPUT); // A2
pinMode(17, INPUT); // A3
digitalWrite(14, LOW); // set A0 to GND
digitalWrite(16, HIGH); // set A2 as 5V
delay(5); // short delay is required to give the analog pins time to adjust to their new roles
yr=analogRead(1) -50; //
return yr;
}
//Toggles Hold on interrupt
void holdToggle(){
//Set to !holdflag
if (holdFlag ==1){
holdFlag = 0;
midiCC(0xB0,92,0x00); //Touchpad up command
touchFlag=0;
}
else
holdFlag = 1;
}
//Get Rotary Encoder Status
int getRot() {
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (digitalRead(encoder0PinB) == LOW) {
encoder0Pos--;
rotateFlag = 1;
}
else {
encoder0Pos++;
rotateFlag = 1;
}
//Normalise values to 7 bit midi
if ( encoder0Pos < 0){
encoder0Pos = encoder0Pos + 127;
}
else if ( encoder0Pos > 127){
encoder0Pos = encoder0Pos -127;
}
}
else{
rotateFlag = 0;
}
encoder0PinALast = n;
return rotateFlag; //1 of rotated, 0 if not
}
void loop()
{
if(holdFlag == 0){
prevSend = x+y; //quick easy fast calculation prep
y = readY();
x = readX();
if(x <1000 && y < 1000){
if(prevSend != x+y){ //check if the buffers changed
ny = map(y,0,1016,0,127); // maps the value range of the axis to midi 0->127
nx = map(x,0,1016,0,127); // maps the value range of the axis to midi 0->127
if(touchFlag == 0){ //check if touchpad is enabled.
midiCC(0xB0,92,127); //Touchpad down command
touchFlag = 1; //set touch flag
}
midiCC(0xB0, 12, nx); // sends midi control change for touch pad X axis
midiCC(0xB0, 13, ny); // sends midi control change for touch pad y axis
//Colour code
seconds = millis(); //Record current time
int y1 =0, x1 = 0;
if (x < halfwidth){
if (y <= halfheight){
//Purple CC 00 CC
redVal = 0xCC;
greenVal = 0x00;
blueVal = 0xCC;
}
else {
//Red FF 00 00
redVal = 0xFF;
greenVal = 0x00;
blueVal = 0x00;
}
}
else{
if (x >= halfwidth){
if (y <= halfheight){
//Yell FF FF 00
redVal = 0xFF;
greenVal = 0xFF;
blueVal = 0x00;
}
else {
//Blue 00 00 FF
redVal = 0x00;
greenVal = 0x00;
blueVal = 0xFF;
}
}
}
if (scriptFlag == 0){
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send('c'); // ‘c’ == fade to color
Wire.send(redVal); // value for red channel
Wire.send(greenVal); // value for blue channel
Wire.send(blueVal); // value for green channel
Wire.endTransmission(); // leave I2C bus
}
else{
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send('o'); // stop Script
Wire.endTransmission(); // leave I2C bus
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send('c'); // ‘c’ == fade to color
Wire.send(redVal); // value for red channel
Wire.send(greenVal); // value for blue channel
Wire.send(blueVal); // value for green channel
Wire.endTransmission(); // leave I2C bus
scriptFlag = 0;
}
}
}
else{
if(touchFlag){ //If touchpad still enabled
midiCC(0xB0,92,0x00); //Touchpad up command
touchFlag=0;
}
}
}
if((seconds + 1000)<millis() && scriptFlag == 0 && holdFlag == 0){
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send("p");
Wire.send(0x00);
Wire.send(0);
Wire.send(0);
Wire.endTransmission(); // leave I2C bus
Wire.begin(); // set up I2C on 4 and 5 analog
Wire.beginTransmission(0x09);// join I2C, talk to BlinkM 0x09
Wire.send("f");
Wire.send(20);
Wire.endTransmission(); // leave I2C bus
seconds = millis();
scriptFlag = 1;
}
if (getRot()){ //If rotary enc changed
midiCC(0xC0,0,encoder0Pos); //send command
}
}
//Send MIDI command
void midiCC(char command, char value1, char value2){
Serial.print(command, BYTE);
Serial.print(value1, BYTE);
Serial.print(value2, BYTE);
}