No responses to Teensy after breadboarding and coding

171 Views Asked by At

I'm new to Arduino and electronics, and I bought a Teensy 3.2 for making a MIDI controller recently. I connected three buttons and one slide pot into a breadboard with the Teensy; modify some code from online; and already changed the USB type as MIDI and port to Teensy. But there's no reaction to my product. May I ask for where are my mistakes? Thank you very much!!!

here's my code and board connection:

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

int buttonApin = 1;
int buttonBpin = 2;
int buttonCpin = 3;

int analogslide = A8;

int analogslideOld = 0;
int analogslideNew = 0;

#define analogslideCC 54

void setup() {
  MIDI.begin ();

  pinMode(buttonApin, INPUT_PULLUP);
  pinMode(buttonBpin, INPUT_PULLUP);
  pinMode(buttonCpin, INPUT_PULLUP);
  pinMode(analogslide, INPUT);

  Serial.begin(9600);
}

void loop() {

  static bool buttonAvalueOld = HIGH;
  static bool buttonBvalueOld = HIGH;
  static bool buttonCvalueOld = HIGH;

  bool buttonAvalueNew = digitalRead(buttonApin);
  bool buttonBvalueNew = digitalRead(buttonBpin);
  bool buttonCvalueNew = digitalRead(buttonCpin);

  if (buttonAvalueNew != buttonAvalueOld) {
    if (buttonAvalueNew == LOW) {
      MIDI.sendNoteOn(60, 127, 1);
    } else {
      MIDI.sendNoteOff(60, 0, 1);
    }
    buttonAvalueOld = buttonAvalueNew;
  }

  if (buttonBvalueNew != buttonBvalueOld) {
    if (buttonBvalueNew == LOW) {
      MIDI.sendNoteOn(64, 127, 1);
    } else {
      MIDI.sendNoteOff(64, 0, 1);
    }
    buttonBvalueOld = buttonBvalueNew;
  }

  if (buttonCvalueNew != buttonCvalueOld) {
    if (buttonCvalueNew == LOW) {
      MIDI.sendNoteOn(65, 127, 1);
    } else {
      MIDI.sendNoteOff(65, 0, 1);
    }
    buttonCvalueOld = buttonCvalueNew;
  }

  int slide = analogRead(A8);
  int analogslideNew = analogRead(A8);

  if (analogslideNew - analogslideOld >= 35 || analogslideOld - analogslideNew >= 35) {
    analogslideOld = analogslideNew;
    analogslideNew = (map(analogslideNew, 1023, 0, 0, 120));
    analogslideNew = (constrain(analogslideNew, 0, 120));
    MIDI.sendControlChange(analogslideCC, analogslideNew, 1);
  }
  delay(25);
}

My breadboard picture

1

There are 1 best solutions below

0
On

Hardware:

Pay attention to your breadboard power buses (top two stripes). There is a gap between the red and blue lines, meaning that you must join them with some wires. At its current circuit state, your red button and the potentiometer are floating, namely not connected to ground and/or VCC.

Also, pay close attention to what Joel mentioned regarding the voltages. Never connect more than 3.3V to your Teensy analog pins (the digital ones are 5V tolerant - please read Paul Stoffregen's explanation-) otherwise you could damage the MCU. I would use the 3.3V out for powering the buttons and potentiometer instead I would jump GND to GND and VCC to VCC from the bottom to the top rail (as a precaution in case you want to add more items)

Firmware:

Regarding this part of the main loop():

  static bool buttonAvalueOld = HIGH;
  static bool buttonBvalueOld = HIGH;
  static bool buttonCvalueOld = HIGH;

Why do you declare and initialize these variables inside the loop? You are attempting to keep the old value but you are losing it at every iteration. Put the outside and update their values inside of their respective conditionals

Similarly with these vars::

  bool buttonAvalueNew = digitalRead(buttonApin);
  bool buttonBvalueNew = digitalRead(buttonBpin);
  bool buttonCvalueNew = digitalRead(buttonCpin);

Declare them outside and update them inside the loop.

Finally, your code does not have any way of debouncing or chattering elimination. To overcome this, I would strongly suggest you use the Bounce library, which is extremely useful.