Any help or ideas are greatly appreciated!
I am working on a project that has me reading 25 rotary encoders with buttons, controlling 32 LEDs, reading 16 "stand alone" buttons and reading 2 potentiometers. That makes a total of 127 inputs. I thought about using multiple Teensy's but that would be a lot of Teensy's so think I can make it work with 8 MCP23017's.
Bread boarding a prototype to test how the MCP23017's would work out I have run into a strange problem that has to do with addressing the Multiplexers.
When I set the MCP to address 27 (both on the bread board, 3.3v to pins 15,16,17 and in the code) everything seems to work OK. When I set the address to any other address (example: 20, GND pins 15,16,17) , it doesn't.
I only have 2 encoders attached at this time so wonder if this may be the problem ( I seem to remember you might need to set the unused data pins to Gnd or 3.3v, can't remember which, so they aren't floating)??? But it still doesn't make sense that it works on address 27 and not on address 20.
I have tested this on both and LC and a 3.6 with the same results. I have also swapped out the MCP, just in case that was the problem.
Pictures for the bread board wiring and a picture of the prototype panel.
Here is the code. I know it could be written better, but it works, at least on address 27. I also know I probably should be using interrupts, but I can't figure out how to code them.
#include <Wire.h>
#include "Adafruit_MCP23017.h"
const int v1Clk=0;
const int v1Dta=1;
const int v2Clk=2;
const int v2Dta=3;
const int v3Clk=4;
const int v3Dta=5;
const int v4Clk=6;
const int v4Dta=7;
const int v5Clk=8;
const int v5Dta=9;
const int v6Clk=10;
const int v6Dta=11;
const int v7Clk=12;
const int v7Dta=13;
const int v8Clk=14;
const int v8Dta=15;
int v1Amt = 0;
int v2Amt = 0;
int v3Amt = 0;
int v4Amt = 0;
int v5Amt = 0;
int v6Amt = 0;
int v7Amt = 0;
int v8Amt = 0;
int resolution = 2;
Adafruit_MCP23017 mcpVol;
void setup() {
mcpVol.begin(27); // use default address 0
mcpVol.pinMode(v1Clk, INPUT);
mcpVol.pinMode(v1Dta, INPUT);
mcpVol.pinMode(v2Clk, INPUT);
mcpVol.pinMode(v2Dta, INPUT);
mcpVol.pinMode(v3Clk, INPUT);
mcpVol.pinMode(v3Dta, INPUT);
mcpVol.pinMode(v4Clk, INPUT);
mcpVol.pinMode(v4Dta, INPUT);
mcpVol.pinMode(v5Clk, INPUT);
mcpVol.pinMode(v5Dta, INPUT);
mcpVol.pinMode(v6Clk, INPUT);
mcpVol.pinMode(v6Dta, INPUT);
mcpVol.pinMode(v7Clk, INPUT);
mcpVol.pinMode(v7Dta, INPUT);
mcpVol.pinMode(v8Clk, INPUT);
mcpVol.pinMode(v8Dta, INPUT);
Serial.begin(9600);
}
void loop() {
ckVol1();
ckVol2();
ckVol3();
ckVol4();
ckVol5();
ckVol6();
ckVol7();
ckVol8();
}
void ckVol1(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v1Clk);
int newB=mcpVol.digitalRead(v1Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v1Amt=v1Amt+resolution;
if (v1Amt>127)v1Amt=127;
Serial.print("Vol 1: ");
Serial.println(v1Amt);
}else if(change==1){
v1Amt=v1Amt-resolution;
if (v1Amt<0) v1Amt=0;
Serial.print("Vol 1: ");
Serial.println(v1Amt);
}
}
void ckVol2(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v2Clk);
int newB=mcpVol.digitalRead(v2Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v2Amt=v2Amt+resolution;
if (v2Amt>127)v2Amt=127;
Serial.print("Vol 2: ");
Serial.println(v2Amt);
}else if(change==1){
v2Amt=v2Amt-resolution;
if (v2Amt<0) v2Amt=0;
Serial.print("Vol 2: ");
Serial.println(v2Amt);
}
}
void ckVol3(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v3Clk);
int newB=mcpVol.digitalRead(v3Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v3Amt=v3Amt+resolution;
if (v3Amt>127)v3Amt=127;
Serial.print("Vol 3: ");
Serial.println(v3Amt);
}else if(change==1){
v3Amt=v3Amt-resolution;
if (v3Amt<0) v3Amt=0;
Serial.print("Vol 3: ");
Serial.println(v3Amt);
}
}
void ckVol4(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v4Clk);
int newB=mcpVol.digitalRead(v4Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v4Amt=v4Amt+resolution;
if (v4Amt>127)v4Amt=127;
Serial.print("Vol 4: ");
Serial.println(v4Amt);
}else if(change==1){
v4Amt=v4Amt-resolution;
if (v4Amt<0) v4Amt=0;
Serial.print("Vol 4: ");
Serial.println(v4Amt);
}
}
void ckVol5(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v5Clk);
int newB=mcpVol.digitalRead(v5Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v5Amt=v5Amt+resolution;
if (v5Amt>127)v5Amt=127;
Serial.print("Vol 5: ");
Serial.println(v5Amt);
}else if(change==1){
v5Amt=v5Amt-resolution;
if (v5Amt<0) v5Amt=0;
Serial.print("Vol 5: ");
Serial.println(v5Amt);
}
}
void ckVol6(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v6Clk);
int newB=mcpVol.digitalRead(v6Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v6Amt=v6Amt+resolution;
if (v6Amt>127)v6Amt=127;
Serial.print("Vol 6: ");
Serial.println(v6Amt);
}else if(change==1){
v6Amt=v6Amt-resolution;
if (v6Amt<0) v6Amt=0;
Serial.print("Vol 6: ");
Serial.println(v6Amt);
}
}
void ckVol7(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v7Clk);
int newB=mcpVol.digitalRead(v7Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v7Amt=v7Amt+resolution;
if (v7Amt>127)v7Amt=127;
Serial.print("Vol 7: ");
Serial.println(v7Amt);
}else if(change==1){
v7Amt=v7Amt-resolution;
if (v7Amt<0) v7Amt=0;
Serial.print("Vol 7: ");
Serial.println(v7Amt);
}
}
void ckVol8(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v8Clk);
int newB=mcpVol.digitalRead(v8Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v8Amt=v8Amt+resolution;
if (v8Amt>127)v8Amt=127;
Serial.print("Vol 8: ");
Serial.println(v8Amt);
}else if(change==1){
v8Amt=v8Amt-resolution;
if (v8Amt<0) v8Amt=0;
Serial.print("Vol 8: ");
Serial.println(v8Amt);
}
}
I am working on a project that has me reading 25 rotary encoders with buttons, controlling 32 LEDs, reading 16 "stand alone" buttons and reading 2 potentiometers. That makes a total of 127 inputs. I thought about using multiple Teensy's but that would be a lot of Teensy's so think I can make it work with 8 MCP23017's.
Bread boarding a prototype to test how the MCP23017's would work out I have run into a strange problem that has to do with addressing the Multiplexers.
When I set the MCP to address 27 (both on the bread board, 3.3v to pins 15,16,17 and in the code) everything seems to work OK. When I set the address to any other address (example: 20, GND pins 15,16,17) , it doesn't.
I only have 2 encoders attached at this time so wonder if this may be the problem ( I seem to remember you might need to set the unused data pins to Gnd or 3.3v, can't remember which, so they aren't floating)??? But it still doesn't make sense that it works on address 27 and not on address 20.
I have tested this on both and LC and a 3.6 with the same results. I have also swapped out the MCP, just in case that was the problem.
Pictures for the bread board wiring and a picture of the prototype panel.
Here is the code. I know it could be written better, but it works, at least on address 27. I also know I probably should be using interrupts, but I can't figure out how to code them.
#include <Wire.h>
#include "Adafruit_MCP23017.h"
const int v1Clk=0;
const int v1Dta=1;
const int v2Clk=2;
const int v2Dta=3;
const int v3Clk=4;
const int v3Dta=5;
const int v4Clk=6;
const int v4Dta=7;
const int v5Clk=8;
const int v5Dta=9;
const int v6Clk=10;
const int v6Dta=11;
const int v7Clk=12;
const int v7Dta=13;
const int v8Clk=14;
const int v8Dta=15;
int v1Amt = 0;
int v2Amt = 0;
int v3Amt = 0;
int v4Amt = 0;
int v5Amt = 0;
int v6Amt = 0;
int v7Amt = 0;
int v8Amt = 0;
int resolution = 2;
Adafruit_MCP23017 mcpVol;
void setup() {
mcpVol.begin(27); // use default address 0
mcpVol.pinMode(v1Clk, INPUT);
mcpVol.pinMode(v1Dta, INPUT);
mcpVol.pinMode(v2Clk, INPUT);
mcpVol.pinMode(v2Dta, INPUT);
mcpVol.pinMode(v3Clk, INPUT);
mcpVol.pinMode(v3Dta, INPUT);
mcpVol.pinMode(v4Clk, INPUT);
mcpVol.pinMode(v4Dta, INPUT);
mcpVol.pinMode(v5Clk, INPUT);
mcpVol.pinMode(v5Dta, INPUT);
mcpVol.pinMode(v6Clk, INPUT);
mcpVol.pinMode(v6Dta, INPUT);
mcpVol.pinMode(v7Clk, INPUT);
mcpVol.pinMode(v7Dta, INPUT);
mcpVol.pinMode(v8Clk, INPUT);
mcpVol.pinMode(v8Dta, INPUT);
Serial.begin(9600);
}
void loop() {
ckVol1();
ckVol2();
ckVol3();
ckVol4();
ckVol5();
ckVol6();
ckVol7();
ckVol8();
}
void ckVol1(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v1Clk);
int newB=mcpVol.digitalRead(v1Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v1Amt=v1Amt+resolution;
if (v1Amt>127)v1Amt=127;
Serial.print("Vol 1: ");
Serial.println(v1Amt);
}else if(change==1){
v1Amt=v1Amt-resolution;
if (v1Amt<0) v1Amt=0;
Serial.print("Vol 1: ");
Serial.println(v1Amt);
}
}
void ckVol2(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v2Clk);
int newB=mcpVol.digitalRead(v2Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v2Amt=v2Amt+resolution;
if (v2Amt>127)v2Amt=127;
Serial.print("Vol 2: ");
Serial.println(v2Amt);
}else if(change==1){
v2Amt=v2Amt-resolution;
if (v2Amt<0) v2Amt=0;
Serial.print("Vol 2: ");
Serial.println(v2Amt);
}
}
void ckVol3(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v3Clk);
int newB=mcpVol.digitalRead(v3Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v3Amt=v3Amt+resolution;
if (v3Amt>127)v3Amt=127;
Serial.print("Vol 3: ");
Serial.println(v3Amt);
}else if(change==1){
v3Amt=v3Amt-resolution;
if (v3Amt<0) v3Amt=0;
Serial.print("Vol 3: ");
Serial.println(v3Amt);
}
}
void ckVol4(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v4Clk);
int newB=mcpVol.digitalRead(v4Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v4Amt=v4Amt+resolution;
if (v4Amt>127)v4Amt=127;
Serial.print("Vol 4: ");
Serial.println(v4Amt);
}else if(change==1){
v4Amt=v4Amt-resolution;
if (v4Amt<0) v4Amt=0;
Serial.print("Vol 4: ");
Serial.println(v4Amt);
}
}
void ckVol5(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v5Clk);
int newB=mcpVol.digitalRead(v5Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v5Amt=v5Amt+resolution;
if (v5Amt>127)v5Amt=127;
Serial.print("Vol 5: ");
Serial.println(v5Amt);
}else if(change==1){
v5Amt=v5Amt-resolution;
if (v5Amt<0) v5Amt=0;
Serial.print("Vol 5: ");
Serial.println(v5Amt);
}
}
void ckVol6(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v6Clk);
int newB=mcpVol.digitalRead(v6Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v6Amt=v6Amt+resolution;
if (v6Amt>127)v6Amt=127;
Serial.print("Vol 6: ");
Serial.println(v6Amt);
}else if(change==1){
v6Amt=v6Amt-resolution;
if (v6Amt<0) v6Amt=0;
Serial.print("Vol 6: ");
Serial.println(v6Amt);
}
}
void ckVol7(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v7Clk);
int newB=mcpVol.digitalRead(v7Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v7Amt=v7Amt+resolution;
if (v7Amt>127)v7Amt=127;
Serial.print("Vol 7: ");
Serial.println(v7Amt);
}else if(change==1){
v7Amt=v7Amt-resolution;
if (v7Amt<0) v7Amt=0;
Serial.print("Vol 7: ");
Serial.println(v7Amt);
}
}
void ckVol8(){
static int oldA = HIGH;
static int oldB = HIGH;
int result=0;
int newA=mcpVol.digitalRead(v8Clk);
int newB=mcpVol.digitalRead(v8Dta);
if (newA!=oldA || newB != oldB){
//something has changed
if(oldA == HIGH && newA==LOW){
result = (oldB + 2 -1);
}
}
oldA=newA;
oldB=newB;
int change = result;
if(change==2){
v8Amt=v8Amt+resolution;
if (v8Amt>127)v8Amt=127;
Serial.print("Vol 8: ");
Serial.println(v8Amt);
}else if(change==1){
v8Amt=v8Amt-resolution;
if (v8Amt<0) v8Amt=0;
Serial.print("Vol 8: ");
Serial.println(v8Amt);
}
}