I have been working on a project for the past month that involves a Teensy 3.2 and the audio adapter board. Basically I am developing a prototyping platform for my software to be used on different projects that I build that involve a teensy. The basic idea is that I have a teensy with an audio adapter board and an mcp23017 IO expander with 4 LEDs and 4 momentary push buttons attached, each with their own pin on the mcp. I am currently writing a program to simply do a hardware test to confirm that all parts of the hardware are functioning. Everything works great, all push buttons respond, and all LEDs respond. Where I am running into a problem is when I unplug the hardware. Everytime I do the LEDS fail to work anymore, until I reprogram the device with my code. The push buttons continue to function and it even progresses thru the program as if nothing were wrong, just missing the LEDs. Once I have reloaded my program everything works fine, until I unplug it. I have tried making sure that the program resets the pins to 0 everytime the program restarts with no luck. I have tried leaving it unplugged so it can fully power cycle just in case there is a static field built up. Any ideas? BTW, the project also has an LCD and 4 analog read knobs attached, just in case that info reveals something I am overlooking. It might also be helpful to know that the LEDs are the only outputs on the mcp23017?
Here is my code I am currently using. I know the program is very simple and not very efficient, but its only purpose is to confirm working hardware. Thanks ahead of time for any suggestions.
#include <Adafruit_LiquidCrystal.h>
#include <Wire.h>
#include "Adafruit_MCP23017.h"
Adafruit_LiquidCrystal lcd(0);
Adafruit_MCP23017 mcp;
int time = 250;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.setBacklight(HIGH);
mcp.begin(1);
mcp.digitalWrite(4, LOW);
mcp.digitalWrite(5, LOW);
mcp.digitalWrite(6, LOW);
mcp.digitalWrite(7, LOW);
mcp.pinMode(4, OUTPUT);
mcp.pinMode(5, OUTPUT);
mcp.pinMode(6, OUTPUT);
mcp.pinMode(7, OUTPUT);
mcp.pinMode(0, INPUT);
mcp.pinMode(1, INPUT);
mcp.pinMode(2, INPUT);
mcp.pinMode(3, INPUT);
mcp.pinMode(8, INPUT);
mcp.pinMode(9, INPUT);
mcp.pinMode(10, INPUT);
}
void loop() {
///////////////////////////////////////LED Test/////////////////
lcd.clear();
lcd.print("Hardware Test");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("LED Test");
delay(time);
mcp.digitalWrite(4, HIGH);
delay(time);
mcp.digitalWrite(4, LOW);
mcp.digitalWrite(5, HIGH);
delay(time);
mcp.digitalWrite(5, LOW);
mcp.digitalWrite(6, HIGH);
delay(time);
mcp.digitalWrite(6, LOW);
mcp.digitalWrite(7, HIGH);
delay(time);
mcp.digitalWrite(7, LOW);
delay(time);
for (int i = 0; i < 4; i++) {
mcp.digitalWrite(7, HIGH);
mcp.digitalWrite(6, HIGH);
mcp.digitalWrite(5, HIGH);
mcp.digitalWrite(4, HIGH);
delay(250);
mcp.digitalWrite(7, LOW);
mcp.digitalWrite(6, LOW);
mcp.digitalWrite(5, LOW);
mcp.digitalWrite(4, LOW);
delay(250);
}
///////////////////////////////////////////Button Test//////////////
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Button Test");
delay (500);
lcd.setCursor(0, 1);
lcd.print("Press a Button");
while (1) {
mcp.digitalWrite(4, mcp.digitalRead(0));
mcp.digitalWrite(5, mcp.digitalRead(1));
mcp.digitalWrite(6, mcp.digitalRead(2));
mcp.digitalWrite(7, mcp.digitalRead(3));
if (mcp.digitalRead(0) == HIGH && mcp.digitalRead(1) == HIGH
&& mcp.digitalRead(2) == HIGH && mcp.digitalRead(3) == HIGH) {
break;
}
}
mcp.digitalWrite(4, LOW);
mcp.digitalWrite(5, LOW);
mcp.digitalWrite(6, LOW);
mcp.digitalWrite(7, LOW);
///////////////////////////////////////Analog Test////////////////////
lcd.clear();
lcd.home();
lcd.print("Analog Test");
lcd.setCursor(0, 1);
lcd.print("Turn Knobs");
delay(1000);
lcd.clear();
lcd.home();
lcd.print("A2 A3 A6 A7");
lcd.setCursor(0, 1);
while (1) {
lcd.setCursor(0,1);
lcd.print(analogRead(2));
lcd.setCursor(4,1);
lcd.print(analogRead(3));
lcd.setCursor(8,1);
lcd.print(analogRead(6));
lcd.setCursor(12, 1);
lcd.print(analogRead(7));
if(mcp.digitalRead(10) == HIGH){
break;
}
}
}
Here is my code I am currently using. I know the program is very simple and not very efficient, but its only purpose is to confirm working hardware. Thanks ahead of time for any suggestions.
#include <Adafruit_LiquidCrystal.h>
#include <Wire.h>
#include "Adafruit_MCP23017.h"
Adafruit_LiquidCrystal lcd(0);
Adafruit_MCP23017 mcp;
int time = 250;
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.setBacklight(HIGH);
mcp.begin(1);
mcp.digitalWrite(4, LOW);
mcp.digitalWrite(5, LOW);
mcp.digitalWrite(6, LOW);
mcp.digitalWrite(7, LOW);
mcp.pinMode(4, OUTPUT);
mcp.pinMode(5, OUTPUT);
mcp.pinMode(6, OUTPUT);
mcp.pinMode(7, OUTPUT);
mcp.pinMode(0, INPUT);
mcp.pinMode(1, INPUT);
mcp.pinMode(2, INPUT);
mcp.pinMode(3, INPUT);
mcp.pinMode(8, INPUT);
mcp.pinMode(9, INPUT);
mcp.pinMode(10, INPUT);
}
void loop() {
///////////////////////////////////////LED Test/////////////////
lcd.clear();
lcd.print("Hardware Test");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("LED Test");
delay(time);
mcp.digitalWrite(4, HIGH);
delay(time);
mcp.digitalWrite(4, LOW);
mcp.digitalWrite(5, HIGH);
delay(time);
mcp.digitalWrite(5, LOW);
mcp.digitalWrite(6, HIGH);
delay(time);
mcp.digitalWrite(6, LOW);
mcp.digitalWrite(7, HIGH);
delay(time);
mcp.digitalWrite(7, LOW);
delay(time);
for (int i = 0; i < 4; i++) {
mcp.digitalWrite(7, HIGH);
mcp.digitalWrite(6, HIGH);
mcp.digitalWrite(5, HIGH);
mcp.digitalWrite(4, HIGH);
delay(250);
mcp.digitalWrite(7, LOW);
mcp.digitalWrite(6, LOW);
mcp.digitalWrite(5, LOW);
mcp.digitalWrite(4, LOW);
delay(250);
}
///////////////////////////////////////////Button Test//////////////
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Button Test");
delay (500);
lcd.setCursor(0, 1);
lcd.print("Press a Button");
while (1) {
mcp.digitalWrite(4, mcp.digitalRead(0));
mcp.digitalWrite(5, mcp.digitalRead(1));
mcp.digitalWrite(6, mcp.digitalRead(2));
mcp.digitalWrite(7, mcp.digitalRead(3));
if (mcp.digitalRead(0) == HIGH && mcp.digitalRead(1) == HIGH
&& mcp.digitalRead(2) == HIGH && mcp.digitalRead(3) == HIGH) {
break;
}
}
mcp.digitalWrite(4, LOW);
mcp.digitalWrite(5, LOW);
mcp.digitalWrite(6, LOW);
mcp.digitalWrite(7, LOW);
///////////////////////////////////////Analog Test////////////////////
lcd.clear();
lcd.home();
lcd.print("Analog Test");
lcd.setCursor(0, 1);
lcd.print("Turn Knobs");
delay(1000);
lcd.clear();
lcd.home();
lcd.print("A2 A3 A6 A7");
lcd.setCursor(0, 1);
while (1) {
lcd.setCursor(0,1);
lcd.print(analogRead(2));
lcd.setCursor(4,1);
lcd.print(analogRead(3));
lcd.setCursor(8,1);
lcd.print(analogRead(6));
lcd.setCursor(12, 1);
lcd.print(analogRead(7));
if(mcp.digitalRead(10) == HIGH){
break;
}
}
}