Hi everyone!
After a few days I managed to compile the full code for my project... Everything in functioning correcly and I learnd a few new things that will be super useful for next projects!
If anyone is curious here's the code (5 buttons, one rotary switch, 5 pots and one rotary encoder to send midi messages to my audio interface, not every midi message is mapped, I will tweak the code when needed)
Thanks
#include "MIDI.h"
#include "Arduino.h"
#include "avdweb_Switch.h"
#include <Encoder.h>
//Rotary Encoder
Encoder knob1(A1, A0);
long position1 = -999;
int encodervalue = 0;
//int premutevalue = 0;
//Pot
int analogpot1 = A7;
int analogpot2 = A6;
int analogpot3 = A5;
int analogpot4 = A4;
int analogpot5 = A3;
//Switches
Switch buttonGND1 = Switch(4);
Switch buttonGND2 = Switch(6);
Switch buttonGND3 = Switch(8);
Switch buttonGND4 = Switch(10);
Switch buttonGNDK = Switch(A2);
Switch buttonGNDRotary1 = Switch(3);
Switch buttonGNDRotary3 = Switch(2);
int analogpot1val = 0;
int analogpot1lastval = 0;
int analogpot2val = 0;
int analogpot2lastval = 0;
int analogpot3val = 0;
int analogpot3lastval = 0;
int analogpot4val = 0;
int analogpot4lastval = 0;
int analogpot5val = 0;
int analogpot5lastval = 0;
int stateLED1 = 0;
int stateLED2 = 0;
int stateLED3 = 0;
int stateLED4 = 0;
int stateRed = 0;
const int Red = 13; //red LED is on pin 4
const int LED1 = 5;
const int LED2 = 7;
const int LED3 = 9;
const int LED4 = 11;
MIDI_CREATE_DEFAULT_INSTANCE();
void checkEntered1(int button);
//void noteOn(byte channel, byte pitch, byte velocity) {
// midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
// MidiUSB.sendMIDI(noteOn);
//}
//void noteOff(byte channel, byte pitch, byte velocity) {
// midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
// MidiUSB.sendMIDI(noteOff);
//}
//void controlChange(byte channel, byte control, byte value) {
// midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
// MidiUSB.sendMIDI(event);
//}
int code[] = {1, 1, 3, 2, 4}; //the desired code is entered in this array,
//separated by commas
int entered[7]; //create a new empty array for the code entered by
//the user (has 4 elements)
int check_passcode = false;
void mute() {
MIDI.sendControlChange(7, encodervalue, 1);
delay(1);
MIDI.sendControlChange(7, encodervalue, 1);
delay(1);
}
void passcode() {
digitalWrite(Red, HIGH);
buttonGND1.poll();
buttonGND2.poll();
buttonGND3.poll();
buttonGND4.poll();
if (buttonGND1.pushed()) { //if button1 is pressed
checkEntered1(1); //call checkEntered and pass it a 1
//delay(250);//wait, needed for correct functioning, otherwise buttons are deemed to be pressed more than once
}
else if (buttonGND2.pushed()) { //if button2 is pressed
checkEntered1(2); //call checkEntered1 and pass it a 2
}
else if (buttonGND3.pushed()) { //if button3 is pressed
checkEntered1(3); //call checkEntered1 and pass it a 3
}
else if (buttonGND4.pushed()) { //if button2 is pressed (copio questo se voglio aggiungere bottoni)
checkEntered1(4); //call checkEntered1 and pass it a 4
}
}
void checkEntered1(int button) { //check the first element of the entered[] array
if (entered[0] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered2(button); //move on to checkEntered2, passing it "button"
}
else if (entered[0] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[0] = button; //set the first element as the button that has been pressed
//Serial.print("1: "); Serial.println(entered[0]); //for debugging
}
}
void checkEntered2(int button) { //check the second element of the entered[] array
if (entered[1] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered3(button); //move on to checkEntered3, passing it "button"
}
else if (entered[1] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[1] = button; //set the second element as the button that has been pressed
//Serial.print("2: "); Serial.println(entered[1]); //for debugging
}
}
void checkEntered3(int button) { //check the third element of the entered[] array
if (entered[2] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered4(button); //move on to checkEntered4, passing it "button"
}
else if (entered[2] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[2] = button; //set the third element as the button that has been pressed
//Serial.print("3: "); Serial.println(entered[2]); //for debugging
}
}
void checkEntered4(int button) { //check the third element of the entered[] array
if (entered[3] != 0) { //if it is not a zero, i.e. it has already been inputted
checkEntered5(button); //move on to checkEntered4, passing it "button"
}
else if (entered[3] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[3] = button; //set the third element as the button that has been pressed
//Serial.print("4: "); Serial.println(entered[3]); //for debugging
}
}
void checkEntered5(int button) { //check the fourth element of the entered[] array
if (entered[4] == 0) { //if it is zero, i.e. if it hasn't been defined with a button yet
entered[4] = button; //set the final element as the button that has been pressed
//Serial.print("5: "); Serial.println(entered[4]); //for debugging
delay(100); //allow time for processing
compareCode(); //call the compareCode function
}
}
void compareCode() { //checks if the code entered is correct by comparing the code[] array with the entered[] array
for (int i = 0; i < 5; i++) { //these three lines are for debugging
//Serial.println(entered[i]);
}
if ((entered[0] == code[0]) && (entered[1] == code[1]) && (entered[2] == code[2]) && (entered[3] == code[3]) && (entered[4] == code[4]) && (entered[5] == code[5])) { //if all the elements of each array are equal
digitalWrite(Red, LOW); // turn the red LED off
//digitalWrite(Green, HIGH); //turn the green LED on
//delay(1000); //wait for a bit
//digitalWrite(Green, LOW); //turn the green LED off
//Serial.println("Green");
check_passcode = true;
for (int i = 0; i < 6; i++) { //this next loop is for debugging
entered[i] = 0;
}
}
else { //if you (or the intruder) get the code wrong
digitalWrite(Red, LOW);
delay(100);
digitalWrite(Red, HIGH);
delay(100);
digitalWrite(Red, LOW);
delay(100);
digitalWrite(Red, HIGH);
delay(100);
digitalWrite(Red, LOW);
delay(100);
digitalWrite(Red, HIGH);
delay(100);
digitalWrite(Red, LOW);
delay(100);
digitalWrite(Red, HIGH);
delay(100);
digitalWrite(Red, LOW);
//Serial.println("Red OFF");
for (int i = 0; i < 7; i++) { //this next loop is for debugging
entered[i] = 0;
}
}
}
void setup() { //run once at sketch startup
//Serial.begin(9600); //begin Serial
Serial.begin(31250);
// pinMode(button1, INPUT_PULLUP); //button 1 is an input
// pinMode(button2, INPUT_PULLUP); //button 2 is an input
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(Red, OUTPUT);
pinMode (analogpot1, INPUT);
pinMode (analogpot2, INPUT);
pinMode (analogpot3, INPUT);
pinMode (analogpot4, INPUT);
pinMode (analogpot5, INPUT);
for (int i = 0; i < 5; i++) { //work through numbers 0-3
Serial.println(code[i]); //print each digit of the code
Serial.println(entered[i]); //print each element of the entered[]array (this was for me to check that it started at 0
}
while (check_passcode == false) passcode(), mute();
//MIDI.sendControlChange(7, 0, 1);
}
void loop() {
//Pots //Pots
analogpot1val = analogRead(analogpot1);
if (abs(analogpot1val - analogpot1lastval) > 6) // questo è il valore soglia, bisogna sperimentare
{
MIDI.sendControlChange(1, analogpot1val / 8, 16); //Midi via 5din
// controlChange(9,102,analogpot1val/8); //Midi via USB
// MidiUSB.flush(); //Midi via USB
//Serial.println("Pot1"), Serial.println(analogpot1val / 8);
analogpot1lastval = analogpot1val;
}
analogpot2val = analogRead(analogpot2);
if (abs(analogpot2val - analogpot2lastval) > 6) // questo è il valore soglia, bisogna sperimentare
{
MIDI.sendControlChange(2, analogpot2val / 8, 16); //Midi via 5din
// controlChange(9,102,analogpot1val/8); //Midi via USB
// MidiUSB.flush(); //Midi via USB
//Serial.println("Pot2"), Serial.println(analogpot2val / 8);
analogpot2lastval = analogpot2val;
}
analogpot3val = analogRead(analogpot3);
if (abs(analogpot3val - analogpot3lastval) > 6) // questo è il valore soglia, bisogna sperimentare
{
MIDI.sendControlChange(3, analogpot3val / 8, 16); //Midi via 5din
// controlChange(9,102,analogpot1val/8); //Midi via USB
// MidiUSB.flush(); //Midi via USB
//Serial.println("Pot3"), Serial.println(analogpot3val / 8);
analogpot3lastval = analogpot3val;
}
analogpot4val = analogRead(analogpot4);
if (abs(analogpot4val - analogpot4lastval) > 6) // questo è il valore soglia, bisogna sperimentare
{
MIDI.sendControlChange(4, analogpot4val / 8, 16); //Midi via 5din
// controlChange(9,102,analogpot1val/8); //Midi via USB
// MidiUSB.flush(); //Midi via USB
//Serial.println("Pot4"), Serial.println(analogpot4val / 8);
analogpot4lastval = analogpot4val;
}
analogpot5val = analogRead(analogpot5);
if (abs(analogpot5val - analogpot5lastval) > 6) // questo è il valore soglia, bisogna sperimentare
{
//MIDI.sendControlChange(7, analogpot5val / 8, 1); //Midi via 5din
// controlChange(9,102,analogpot1val/8); //Midi via USB
// MidiUSB.flush(); //Midi via USB
//Serial.println("Pot5"), Serial.println(analogpot5val / 8);
analogpot5lastval = analogpot5val;
}
// Rotary Encoder //Rotary Encoder
long new1;
new1 = knob1.read();
if (new1 != position1) {
if (new1 > position1) {
//Serial.print("Knob1 = UP");
// if (buttonGNDMID.on()) {
// MIDI.sendControlChange(1, 3, 2);
// }
// else {
// MIDI.sendControlChange(21, 3, 2);
// }
//Serial.println("Knob"), Serial.println(encodervalue + 1);
{ if (encodervalue + 1 < 126) {
encodervalue = encodervalue + 1;
MIDI.sendControlChange(7, encodervalue + 1, 1);
}
else encodervalue = 127;
MIDI.sendControlChange(7, encodervalue, 1);
}
}
else {
//Serial.print("Knob1 = DW");
// if (buttonGNDMID.on()) {
// MIDI.sendControlChange(1, 125, 2);
// }
// else {
// MIDI.sendControlChange(21, 125, 2);
//Serial.println("Knob"), Serial.println(encodervalue - 1);
//encodervalue = encodervalue - 1;
{ if (encodervalue - 1 > 1) {
encodervalue = encodervalue - 1;
MIDI.sendControlChange(7, encodervalue - 1, 1);
}
else encodervalue = 0;
MIDI.sendControlChange(7, encodervalue, 1);
}
}
}
position1 = new1;
//}
//buttons //Buttons
buttonGND1.poll(); //Reference 1
if (buttonGND1.pushed()) {
if (stateLED1 == 255) {
stateLED1 = 0;
MIDI.sendControlChange(109, 0, 16);
MIDI.sendControlChange(108, 0, 16);
MIDI.sendControlChange(107, 0, 16);
MIDI.sendControlChange(106, 0, 16);
}
else {
stateLED1 = 255;
stateLED2 = 0;
stateLED3 = 0;
stateLED4 = 0;
MIDI.sendControlChange(109, 127, 16);
MIDI.sendControlChange(108, 0, 16);
MIDI.sendControlChange(107, 0, 16);
MIDI.sendControlChange(106, 0, 16);
}
digitalWrite(LED1, stateLED1);
digitalWrite(LED2, stateLED2);
digitalWrite(LED3, stateLED3);
digitalWrite(LED4, stateLED4);
}
buttonGND2.poll(); //Reference 2
if (buttonGND2.pushed()) {
if (stateLED2 == 255) {
stateLED2 = 0;
MIDI.sendControlChange(109, 0, 16);
MIDI.sendControlChange(108, 0, 16);
MIDI.sendControlChange(107, 0, 16);
MIDI.sendControlChange(106, 0, 16);
}
else {
stateLED1 = 0;
stateLED2 = 255;
stateLED3 = 0;
stateLED4 = 0;
MIDI.sendControlChange(109, 0, 16);
MIDI.sendControlChange(108, 127, 16);
MIDI.sendControlChange(107, 0, 16);
MIDI.sendControlChange(106, 0, 16);
}
digitalWrite(LED1, stateLED1);
digitalWrite(LED2, stateLED2);
digitalWrite(LED3, stateLED3);
digitalWrite(LED4, stateLED4);
}
buttonGND3.poll(); //Reference 3
if (buttonGND3.pushed()) {
if (stateLED3 == 255) {
stateLED3 = 0;
MIDI.sendControlChange(109, 0, 16);
MIDI.sendControlChange(108, 0, 16);
MIDI.sendControlChange(107, 0, 16);
MIDI.sendControlChange(106, 0, 16);
}
else {
stateLED1 = 0;
stateLED2 = 0;
stateLED3 = 255;
stateLED4 = 0;
MIDI.sendControlChange(109, 0, 16);
MIDI.sendControlChange(108, 0, 16);
MIDI.sendControlChange(107, 127, 16);
MIDI.sendControlChange(106, 0, 16);
}
digitalWrite(LED1, stateLED1);
digitalWrite(LED2, stateLED2);
digitalWrite(LED3, stateLED3);
digitalWrite(LED4, stateLED4);
}
buttonGND4.poll(); //Reference 3
if (buttonGND4.pushed()) {
if (stateLED4 == 255) {
stateLED4 = 0;
MIDI.sendControlChange(109, 0, 16);
MIDI.sendControlChange(108, 0, 16);
MIDI.sendControlChange(107, 0, 16);
MIDI.sendControlChange(106, 0, 16);
}
else {
stateLED1 = 0;
stateLED2 = 0;
stateLED3 = 0;
stateLED4 = 255;
MIDI.sendControlChange(109, 0, 16);
MIDI.sendControlChange(108, 0, 16);
MIDI.sendControlChange(107, 0, 16);
MIDI.sendControlChange(106, 127, 16);
}
digitalWrite(LED1, stateLED1);
digitalWrite(LED2, stateLED2);
digitalWrite(LED3, stateLED3);
digitalWrite(LED4, stateLED4);
}
buttonGNDK.poll(); //103
if (buttonGNDK.pushed()) {
if (stateRed == 255) {
stateRed = 0;
// controlChange(15, 105, 0); // channel, control, value
// MidiUSB.flush();
MIDI.sendNoteOff(44, 0, 1); //cc, value, channel
}
else {
stateRed = 255;
// controlChange(15, 105, 127); // channel, control, value
// MidiUSB.flush();
MIDI.sendNoteOff(44, 127, 1); //cc, value, channel
}
digitalWrite(Red, stateRed);
}
buttonGNDRotary1.poll();
if (buttonGNDRotary1.pushed()) {
//Serial.println ("Pos1");
MIDI.sendControlChange(51, 127, 3);
}
if (buttonGNDRotary1.released()) {
//Serial.println ("Not Pos1");
MIDI.sendControlChange(51, 0, 3);
}
buttonGNDRotary3.poll();
if (buttonGNDRotary3.pushed()) {
//Serial.println ("Pos3");
MIDI.sendControlChange(52, 127, 3);
}
if (buttonGNDRotary3.released()) {
//Serial.println ("Not Pos3");
MIDI.sendControlChange(52, 0, 3);
}
}