Thank you again Grumpy_Mike, I've been in deep again, but my novice status is thwarting me.
However, one of my 8 knobs is sending continuous controller information when turned, so I can see something is going right. Previously I had all the pins defined together as 'button pins' and was then able to split them into 'note' and 'CC' by the order I'd listed them, but I'm clearly not doing things right when trying to define controllerNumber(s) to the analogue pins. Also, despite working with your much appreciated code, I'm not cracking the midi send end of things.
Are you able to have a look at my sketch please? I know I need to do some more thorough learning, but I'm thinking this is maybe not so far away to educated eyes. I've tried many things, but this is the best version I've managed (it uploads, my note buttons work and one knob is sending controller data). I've put comments in where I've been doing damage. Thank you, any help very welcome.
/*
Video for this project here:https://youtu.be/QPgYpVHFSQY
Thank you for checking out my video and my code!
For more videos and information check out
https://www.youtube.com/merwinmusic
http://www.lukemerwin.com/
*/
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
#include <SoftwareSerial.h>
#endif
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "Adafruit_BLEMIDI.h"
#include "BluefruitConfig.h"
#define FACTORYRESET_ENABLE 0
#define MINIMUM_FIRMWARE_VERSION "0.7.0"
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
Adafruit_BLEMIDI midi(ble);
bool isConnected = false;
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
// callback
void connected(void)
{
isConnected = true;
Serial.println(F(" CONNECTED!"));
delay(1000);
}
void disconnected(void)
{
Serial.println("disconnected");
isConnected = false;
}
void BleMidiRX(uint16_t timestamp, uint8_t status, uint8_t byte1, uint8_t byte2)
{
Serial.print("[MIDI ");
Serial.print(timestamp);
Serial.print(" ] ");
Serial.print(status, HEX); Serial.print(" ");
Serial.print(byte1 , HEX); Serial.print(" ");
Serial.print(byte2 , HEX); Serial.print(" ");
Serial.println();
}
//this is the original 'midi guitar' code, changed to 7 from 15 buttons and note numbers changed//
const int ButtonPins[7] = {2, 3, 5, 10, 11, 12, 13};
int LastButtonState[7] = {0};
int currentMillis = 0;
int LastButtonChangeTime[7] = {currentMillis};
int NoteNumber[7] = {36, 37, 38, 39, 40, 41, 42};
//this is my new attempt at code attempting to define the analogue pins with CC numbers//
const int pin[8] = {6, 9, 18, 19, 20, 21, 22, 23};
//lastPotValue[8] = {0};
int potValue = {0};
int lastPotValue = potValue;
int lastPotChangeTime[8] = {potValue};
int controllerNumber[8] = {1, 2, 8, 7, 6, 5, 4, 3};
void setup(void)
{
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit MIDI Example"));
Serial.println(F("---------------------------------------"));
/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ) {
error(F("Couldn't factory reset"));
}
}
//ble.sendCommandCheckOK(F("AT+uartflow=off"));
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
/* Set BLE callbacks */
ble.setConnectCallback(connected);
ble.setDisconnectCallback(disconnected);
// Set MIDI RX callback
midi.setRxCallback(BleMidiRX);
Serial.println(F("Enable MIDI: "));
if ( ! midi.begin(true) )
{
error(F("Could not enable MIDI"));
}
ble.verbose(false);
Serial.print(F("Waiting for a connection..."));
for(int i=0; i < 15; i++)
pinMode(ButtonPins[i],INPUT_PULLUP);
}
void loop() {
//-------------------------------------------------------------------------------------//
// interval for each scanning ~ 500ms (non blocking)
ble.update(250);
// bail if not connected
if (! isConnected)
return;
//-----------------------------------------------------------------------------------//
//--------original midi guitar code, i<7 inserted instead of i<15---------------//
{
unsigned long currentMillis = millis();
for (int i=0; i<7; i++) {
boolean state = !digitalRead(ButtonPins[i]); // 'true'==pressed, 'false'==released
// Check for state change and do debounce
if (state != LastButtonState[i] && currentMillis-LastButtonChangeTime[i] > 50) {
LastButtonState[i] = state;
LastButtonChangeTime[i] = currentMillis;
if (state)
midi.send(0x90, NoteNumber[i], 0x00); // Pressed
else
midi.send(0x80, NoteNumber[i], 0x00); // Released
}
}
}
//-----new attempt at code, continuos CC12 being sent, Knob to pin 23 (A5) sending CC12 from 0-127 (so some hope)-------//
unsigned long potValue = analogRead(pin);
for (int i=8; i<15; i++) {
if( abs(lastPotValue - potValue) > 4) midi.send(0xB0, controllerNumber, potValue >> 3);
else
midi.send(0xB0, controllerNumber, potValue >> 127);
lastPotValue = potValue;
}
}