Hi to everyone!
I'm building a guitar pedal, it would send and receive midi message. It does it.
The problem is that I would like to put a simple pulsing led just to know the power status (and aesthetic reasons) and when I put some strings of code the midi system going crazy.
I think this is a simple programming issue..
This led is on a PWM pin (3).
This is the code (LEDCONTROLLO is just a simple led for debugging, the pulse led is LED1):
#include <MIDI.h>
#include "Controller.h"
// Define labels for the AMP's pins
#define RELAY1 4 // LEAD
#define RELAY2 5 // CLEAN
#define RELAY3 6 // REVERB
#define RELAY4 7 // BOOST
//Define LED's pin
const int LED1 = 3;
const int LEDCONTROLLO = 13;
//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();
// Define labels for the POT & FOOTSWITCHES's pin
byte NUMBER_BUTTONS = 1;
byte NUMBER_POTS = 1;
//Pot (Pin Number, Command, CC Control, Channel Number)
Pot PO1(A0, 0, 24, 13);
Pot *POTS[] {&PO1};
//***DEFINE DIRECTLY CONNECTED BUTTONS*******************************
//Button (Pin Number, Command, Note Number, Channel, Debounce Time)
//** Command parameter 0=NOTE 1=CC 2=Toggle CC **
Button BU1(8, 2, 25, 13, 5 );
Button BU2(9, 2, 26, 13, 5 );
Button BU3(10, 2, 27, 13, 5 );
Button BU4(11, 2, 28, 13, 5 );
Button BU5(12, 2, 29, 13, 5 );
Button *BUTTONS[] {&BU1, &BU2, &BU3, &BU4, &BU5};
void setup() {
pinMode(RELAY1, OUTPUT); //PIN in uscita LEAD
pinMode(RELAY2, OUTPUT); //PIN in uscita CLEAN
pinMode(RELAY3, OUTPUT); //PIN in uscita REVERB
pinMode(RELAY4, OUTPUT); //PIN in uscita BOOST
pinMode (LED1, OUTPUT); //PIN in uscita LED
pinMode (LEDCONTROLLO, OUTPUT); //PIN in uscita LED CONTROLLO
MIDI.begin(13);
//MIDI.begin(MIDI_CHANNEL_OMNI); // Initialize the Midi Library.
// OMNI sets it to listen to all channels.. MIDI.begin(2) would set it
// to respond to notes on channel 2 only.
MIDI.setHandleControlChange(MyCCFunction); // This command tells the MIDI Library
// the function you want to call when a Continuous Controller command
// is received. In this case it's "MyCCFunction".
//Pulsing led function call
LampeggioLed();
}
void loop() { // Main loop
if (NUMBER_BUTTONS != 0) updateButtons();
if (NUMBER_POTS != 0) updatePots();
MIDI.read(); // Continuously check if Midi data has been received.
}
//*****************Functions******************
//Pulsing led function
void LampeggioLed() {
float in, out;
for (in = 0; in < 6.283; in = in + 0.001)
{
out = sin(in) * 127.5 + 127.5;
analogWrite(LED1,out);
delay(1);
}
}
// MyCCFunction is the function that will be called by the Midi Library
// when a Continuous Controller message is received.
// It will be passed bytes for Channel, Controller Number, and Value
// It checks if the controller number is within the 22 to 27 range
// If it is, light up the corresponding LED with the PWM brightness equal to the Value byte
void MyCCFunction(byte channel, byte number, byte value) {
switch (number) {
case 20:
if (value > 0)
{
digitalWrite(RELAY1,HIGH);
}
else
{
digitalWrite(RELAY1,LOW);
}
break;
case 21:
if (value > 0)
{
digitalWrite(RELAY2,HIGH);
}
else
{
digitalWrite(RELAY2,LOW);
}
break;
case 22:
if (value > 0)
{
digitalWrite(RELAY3,HIGH);
}
else
{
digitalWrite(RELAY3,LOW);
}
break;
case 23:
if (value > 0)
{
digitalWrite(RELAY4,HIGH);
}
else
{
digitalWrite(RELAY4,LOW);
}
break;
}
}
//**********************BUTTONS*************************
void updateButtons() {
// Cycle through Button array
for (int i = 0; i < NUMBER_BUTTONS; i = i + 1) {
byte message = BUTTONS[i]->getValue();
// Button is pressed
if (message == 0) {
switch (BUTTONS[i]->Bcommand) {
case 0: //Note
MIDI.sendNoteOn(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
break;
case 1: //CC
MIDI.sendControlChange(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
break;
case 2: //Toggle
if (BUTTONS[i]->Btoggle == 0) {
MIDI.sendControlChange(BUTTONS[i]->Bvalue, 127, BUTTONS[i]->Bchannel);
BUTTONS[i]->Btoggle = 1;
}
else if (BUTTONS[i]->Btoggle == 1) {
MIDI.sendControlChange(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
BUTTONS[i]->Btoggle = 0;
}
break;
}
}
// Button is not pressed
if (message == 1) {
switch (BUTTONS[i]->Bcommand) {
case 0:
MIDI.sendNoteOff(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
break;
case 1:
MIDI.sendControlChange(BUTTONS[i]->Bvalue, 0, BUTTONS[i]->Bchannel);
break;
}
}
}
}
//***********************POTS*****************************
void updatePots() {
for (int i = 0; i < NUMBER_POTS; i = i + 1) {
byte potmessage = POTS[i]->getValue();
if (potmessage != 255) MIDI.sendControlChange(POTS[i]->Pcontrol, potmessage, POTS[i]->Pchannel);
}
}
If I put the LampeggioLed() function in the loop Arduino doesn't send and receive correct messages, if I put this function in the setup() it just stay switched on.
I don't know where to put this to have a working midi system:
float in, out;
for (in = 0; in < 6.283; in = in + 0.001)
{
out = sin(in) * 127.5 + 127.5;
analogWrite(LED1,out);
delay(1);
}
Sorry guys for my english....
Thank you in advance!