UNTZtrument: Keep LEDs lit during toggle?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
redletters
 
Posts: 8
Joined: Wed Oct 22, 2014 6:14 pm

UNTZtrument: Keep LEDs lit during toggle?

Post by redletters »

Hey y'all, still very new to the Arduino and C/C++ stuff -- could someone help me figure out how to keep the LEDs lit while I toggle a MIDI note on, and to turn them off once the note is toggled off?

During the UNTZtrument test, I was able to press a button that lit the LED and then press it again to turn it off. That code looks something like this:

Code: Select all

if (MODE == LATCHING) {
    // If a button was just pressed or released...
    if (trellis.readSwitches()) {
      // go through every button
      for (uint8_t i=0; i<numKeys; i++) {
        // if it was pressed...
	if (trellis.justPressed(i)) {
	  Serial.print("v"); Serial.println(i);
	  // Alternate the LED
	  if (trellis.isLED(i))
	    trellis.clrLED(i);
	  else
	    trellis.setLED(i);
        } 
      }
      // tell the trellis to set the LEDs we requested
      trellis.writeDisplay();
    }
  }
}
[/size]
Could I add this to the UNTZtrument_Hello_World code in order to make this work?

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: UNTZtrument: Keep LEDs lit during toggle?

Post by pburgess »

That looks about right. A replacement loop() function for the Hello World example might look like this:

Code: Select all

void loop() {
  unsigned long t = millis();
  if((t - prevReadTime) >= 20L) { // 20ms = min Trellis poll time
    if(untztrument.readSwitches()) { // Button state change?
      for(uint8_t i=0; i<N_BUTTONS; i++) { // For each button...
        // Get column/row for button, convert to MIDI note number
        uint8_t x, y, note;
        untztrument.i2xy(i, &x, &y);
        note = LOWNOTE + y * WIDTH + x;
        if(untztrument.justPressed(i)) { // Fresh button press?
          if(untztrument.isLED(i)) { // LED already on? Turn off
            usbMIDI.sendNoteOff(note, 0, CHANNEL);
            untztrument.clrLED(i);
          } else { // Turn LED on, send NoteOn message
            usbMIDI.sendNoteOn(note, 127, CHANNEL);
            untztrument.setLED(i);
          }
        }
      }
      untztrument.writeDisplay();
    }
    prevReadTime = t;
    digitalWrite(LED, ++heart & 32); // Blink = alive
  }
  while(usbMIDI.read()); // Discard incoming MIDI messages
}

User avatar
redletters
 
Posts: 8
Joined: Wed Oct 22, 2014 6:14 pm

Re: UNTZtrument: Keep LEDs lit during toggle?

Post by redletters »

Yes! That's exactly what my friend helped me come up with today, and it has the LEDs turning on and off with each press... but now there is only one MIDI note being mapped (the top left button). Any ideas on why that might be?

User avatar
redletters
 
Posts: 8
Joined: Wed Oct 22, 2014 6:14 pm

Re: UNTZtrument: Keep LEDs lit during toggle?

Post by redletters »

Okay, this is what we came up with and now it's working for our purposes:

Code: Select all

void columns(int row, int button_index){
  for(int y=0; y<8; y++){
    uint8_t column_i = untztrument.xy2i(row, y);
    untztrument.clrLED(column_i);
  }
  untztrument.setLED(button_index);
}

void loop() {
  unsigned long t = millis();
  if((t - prevReadTime) >= 20L) { // 20ms = min Trellis poll time
    if(untztrument.readSwitches()) { // Button state change?
      for(int x=0; x<8; x++) {
        for(int y=0; y<8; y++) {
          uint8_t i = untztrument.xy2i(x,y);
          uint8_t note = LOWNOTE + y * WIDTH + x;
        if(untztrument.justPressed(i)) {
          if(untztrument.isLED(i)) { // LED already on? Turn off
            usbMIDI.sendNoteOff(note, 0, CHANNEL);
            untztrument.clrLED(i);
          } else { // Turn LED on, send NoteOn message
            usbMIDI.sendNoteOn(note, 127, CHANNEL);
            untztrument.setLED(i);
            columns(x, i);
          }
        }
       
       untztrument.writeDisplay();

      }
      }
    }
    prevReadTime = t;
    digitalWrite(LED, ++heart & 32); // Blink = alive
  }
  while(usbMIDI.read()); // Discard incoming MIDI messages
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”