Double momentary switch to call functions

Hi everyone!

I'm new with Arduino and programming however I made a simple midi controller for Ableton and so far the sketch is running without any issues. In order to expand the controller I've got a double momentary toggle switch "on-(off)-on" (spring loaded). The idea of having this switch is to select between midi channels. Once is toggle to one side it calls a function, once is toggle to the other side it calls another function.

I've been watching a lot of examples but more I see, more I get confused. Someone can give me a clue so i can put this up and running? Here's my code so far:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_CAP1188.h>

Adafruit_CAP1188 cap1 = Adafruit_CAP1188();

byte midi1 = 176; //define midi channel 1
byte midi2 = 177; //define midi channel 2

//Pot and faders pins
int pot1Pin = A0;
int pot2Pin = A1;

int fad1Pin = A2;
int fad2Pin = A3;

int mappedPot1Val = 0;
int mappedPot2Val = 0;

int mappedFad1Val = 0;
int mappedFad2Val = 0;

int prevPot1Val = 0;
int prevPot2Val = 0;

int prevFad1Val = 0;
int prevFad2Val = 0;

int THRESHOLD = 2;

//LED pins
int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;

void setup() {

Serial.begin(9600);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);

cap1.begin();

cap1.writeRegister(0x1F , 0x4F); // touch sensitivity

}

void loop(){

potsChn1();
touchSenschn1();

}

///////////////////////////////////////////////////////////

void potsChn1(){

int pot1Val = analogRead(pot1Pin);
mappedPot1Val = map(pot1Val, 0, 1023, 0, 127);

if(abs(mappedPot1Val - prevPot1Val) >= THRESHOLD){
midiOUT(0xB0, 90, mappedPot1Val);

prevPot1Val = mappedPot1Val;
}

int pot2Val = analogRead(pot2Pin);
mappedPot2Val = map(pot2Val, 0, 1023, 0, 127);

if(abs(mappedPot2Val - prevPot2Val) >= THRESHOLD){
midiOUT(0xB0, 102, mappedPot2Val);

prevPot2Val = mappedPot2Val;
}

///////////////////////////////////////////////////////////

void potsChn2(){

int pot1Val = analogRead(pot1Pin);
mappedPot1Val = map(pot1Val, 0, 1023, 0, 127);

if(abs(mappedPot1Val - prevPot1Val) >= THRESHOLD){
midiOUT(0xB1, 90, mappedPot1Val);

prevPot1Val = mappedPot1Val;
}

int pot2Val = analogRead(pot2Pin);
mappedPot2Val = map(pot2Val, 0, 1023, 0, 127);

if(abs(mappedPot2Val - prevPot2Val) >= THRESHOLD){
midiOUT(0xB1, 102, mappedPot2Val);

prevPot2Val = mappedPot2Val;
}

}

///////////////////////////////////////////////////////////

void touchSenschn1() {

uint8_t touched1 = cap1.touched();

if (cap1.touched() & (1 << 0)) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
MIDImessage(midi1, 3, 127);

}

if (cap1.touched() & (1 << 1)) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
MIDImessage(midi1, 9, 127);

}

if (cap1.touched() & (1 << 2)) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
MIDImessage(midi1, 14, 127);

}
if (cap1.touched() & (1 << 3)) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
MIDImessage(midi1, 15, 127);

}

///////////////////////////////////////////////////////////

void touchSenschn2() {

uint8_t touched1 = cap1.touched();

if (cap1.touched() & (1 << 0)) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
MIDImessage(midi2, 3, 127);

}

if (cap1.touched() & (1 << 1)) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
MIDImessage(midi2, 9, 127);

}

if (cap1.touched() & (1 << 2)) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
MIDImessage(midi2, 14, 127);

}
if (cap1.touched() & (1 << 3)) {
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
MIDImessage(midi2, 15, 127);

}

///////////////////////////////////////////////////////////

}
void MIDImessage(byte command, byte data1, byte data2) {
Serial.write(command);
Serial.write(data1);
Serial.write(data2);
}

void midiOUT(char command, char value1, char value2) {
Serial.write(command);
Serial.write(value1);
Serial.write(value2);

}

The first thing I notice is that in your touchSenschn1() function, the very first line is
** uint8_t touched1 = cap1.touched();** I presume this is reading the controls and assigning their positions into the variable touched1.

However, for the rest of your function you then totally ignore this variable and instead call the function cap1.touched() again.

So I'd suggest that, for example
if (cap1.touched() & (1 << 0)) {
should read
if (touched1 & (1 << 0)) {

Unfortunately I don't have access to the Adafruit_CAP1188 library so I don't think I can help much with this.

Can I suggest that you write a much simpler program first to enable you to get to grips with reading the switch and reacting to it. You do not need any of the midi stuff in there to do this and it will help you to see the wood for the trees.

The program that you posted does not compile. It looks like a closing brace is missing at the end of the potsChn1() and touchSenschn1() functions.

Yes, a closing brace was missing. Thank you!

Done! :slight_smile:

cordeiropedro:
Done! :slight_smile:

Done what ?

The momentary switch is calling the functions.

I would be interested to see the program and in knowing how you have the switch wired.

Sure! Because I need two LEDs to indicate in what channel I'm working, I used the LED status to run the function.

const int buttonPin = 52;
const int buttonPin1 = 53;
const int ledPin = 25;
const int ledPin1 = 26;

int pot1Pin = A0;
int mappedPot1Val = 0;
int prevPot1Val = 0;
int THRESHOLD = 2;

int buttonState = 0;
int buttonState1 = 0;
int ledState = 0;
int led1State = 0;

void setup() {

Serial.begin(9600);

pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);

pinMode(buttonPin, INPUT);
pinMode(buttonPin1, INPUT);
digitalWrite(ledPin, HIGH);
}

void loop(){

buttonState = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin1);
ledState = digitalRead(ledPin);
led1State = digitalRead(ledPin1);

if (buttonState == HIGH) {

digitalWrite(ledPin, HIGH);
digitalWrite(ledPin1, LOW);
}

if (buttonState1 == HIGH) {

digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin, LOW);
}

if (ledState == HIGH) {

potsChn1();
}

if (led1State == HIGH) {

potsChn2();
}

}

/////////////////////////////////////////

void potsChn1(){

int pot1Val = analogRead(pot1Pin);
mappedPot1Val = map(pot1Val, 0, 1023, 0, 127);

if(abs(mappedPot1Val - prevPot1Val) >= THRESHOLD){
midiOUT(0xB0, 90, mappedPot1Val);

prevPot1Val = mappedPot1Val;
}
}
void potsChn2(){

int pot1Val = analogRead(pot1Pin);
mappedPot1Val = map(pot1Val, 0, 1023, 0, 127);

if(abs(mappedPot1Val - prevPot1Val) >= THRESHOLD){
midiOUT(0xB1, 90, mappedPot1Val);

prevPot1Val = mappedPot1Val;
}
}
void MIDImessage(byte command, byte data1, byte data2) {
Serial.write(command);
Serial.write(data1);
Serial.write(data2);
}

void midiOUT(char command, char value1, char value2) {
Serial.write(command);
Serial.write(value1);
Serial.write(value2);

}

The switch is wired to pins 52 and 53 with two 110k resistors to the ground.