Skip to content

Commit b23cbb9

Browse files
committedOct 5, 2024
v1.0.0
1 parent bec244c commit b23cbb9

File tree

5 files changed

+112
-126
lines changed

5 files changed

+112
-126
lines changed
 

‎include/sensors.h

+16-15
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,42 @@ Date: May 2024
1313

1414
// Include Libraries
1515
#include <Arduino.h>
16-
#include <Wire.h>
17-
#include <SPI.h>
1816
#include <Adafruit_CAP1188.h>
19-
#include "utilities.h"
2017

2118
// Define Macros
2219
#define NUM_OF_MODULES 2
2320
#define SENSORS_PER_MODULE 8
2421
#define NUM_OF_SENSORS NUM_OF_MODULES*SENSORS_PER_MODULE
25-
#define CYCLE_REGISTER 0x24
26-
#define CYCLE_VALUE 0b0001000
27-
#define SENSITIVITY_REGISTER 0x1F
28-
#define SENSITIVITY_VALUE 0b1101111
22+
#define MAIN_REGISTER 0x00
23+
#define SENSORS_REGISTER 0x03
2924
#define STANDBY_REGISTER 0x40
3025
#define STANDBY_VALUE 0b11111111
31-
#define STANDBY_SENSITIVITY_REGISTER 0x42
32-
#define STANDBY_SENSITIVITY_VALUE 0b110
26+
#define REPEAT_RATE_REGISTER 0x28
27+
#define REPEAT_RATE_VALUE 0b00000000
3328
#define CALIBRATION_REGISTER 0x26
3429
#define CALIBRATION_VALUE 0b11111111
3530
#define RECALIBRATION_REGISTER 0x2F
3631
#define RECALIBRATION_VALUE 0b10011111
37-
#define TRIGGER_THRESHOLD 100
32+
#define CYCLE_REGISTER 0x24
33+
#define CYCLE_VALUE 0b0001100
34+
#define STANDBY_CYCLE_REGISTER 0x41
35+
#define STANDBY_CYCLE_VALUE 0b0001100
36+
#define SENSITIVITY_REGISTER 0x1F
37+
#define SENSITIVITY_VALUE 0b1101111
38+
#define STANDBY_SENSITIVITY_REGISTER 0x42
39+
#define STANDBY_SENSITIVITY_VALUE 0b110
3840

3941
// Declare Variables
4042
extern Adafruit_CAP1188 modules[NUM_OF_MODULES];
4143
extern const byte cs_pins[NUM_OF_MODULES];
4244
extern const byte rst_pins[NUM_OF_MODULES];
43-
extern byte sensor_state[NUM_OF_SENSORS];
44-
extern bool touch[NUM_OF_SENSORS];
45-
extern bool release[NUM_OF_SENSORS];
45+
extern const byte irq_pins[NUM_OF_MODULES];
46+
extern bool sensor_state[NUM_OF_SENSORS];
4647
extern byte active_sensors;
47-
extern unsigned long trigger_timer[NUM_OF_SENSORS];
48+
extern volatile bool interrupt;
4849

4950
// Declare Functions
5051
void setupModules();
51-
void detectTouch();
52+
void handleInterrupt();
5253

5354
#endif

‎include/utilities.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,18 @@ Date: May 2024
2121
#define RED_PIN 3
2222
#define GREEN_PIN 5
2323
#define COMMON_ANODE 1
24-
#define FLASH_DURATION 50
2524
#define INTENSITY 25
25+
#define DURATION 50
2626

2727
// Declare Variables
28-
extern unsigned long flash_timer;
28+
extern unsigned long timer;
2929

3030
// Declare Functions
3131
void setupSerial();
32-
byte getColorValue(byte percentage);
33-
void changeColor(byte red,byte green);
3432
void setupLED();
35-
void flashLED();
33+
void controlLED();
34+
void changeColor(byte red,byte green);
35+
byte getAnalogValue(byte color);
3636
void registerNotes();
3737

3838
#endif

‎src/main.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ void setup()
2222

2323
void loop()
2424
{
25-
detectTouch();
26-
registerNotes();
27-
sendData();
28-
flashLED();
25+
if(interrupt)
26+
{
27+
registerNotes();
28+
sendData();
29+
interrupt = false;
30+
}
31+
controlLED();
2932
}

‎src/sensors.cpp

+19-51
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ Date: May 2024
1414
Adafruit_CAP1188 modules[NUM_OF_MODULES];
1515
const byte cs_pins[NUM_OF_MODULES] = {9,7};
1616
const byte rst_pins[NUM_OF_MODULES] = {8,6};
17-
byte sensor_state[NUM_OF_SENSORS] = {0};
18-
bool touch[NUM_OF_SENSORS] = {0};
19-
bool release[NUM_OF_SENSORS] = {0};
17+
extern const byte irq_pins[NUM_OF_MODULES] = {1,0};
18+
bool sensor_state[NUM_OF_SENSORS] = {0};
2019
byte active_sensors = 0;
21-
unsigned long trigger_timer[NUM_OF_SENSORS] = {0};
20+
volatile bool interrupt = false;
2221

2322
// Define Functions
2423
void setupModules()
@@ -28,63 +27,32 @@ void setupModules()
2827
modules[m] = Adafruit_CAP1188(cs_pins[m],rst_pins[m]);
2928
if(modules[m].begin())
3029
{
31-
modules[m].writeRegister(CYCLE_REGISTER,CYCLE_VALUE);
32-
modules[m].writeRegister(SENSITIVITY_REGISTER,SENSITIVITY_VALUE);
30+
pinMode(irq_pins[m],INPUT_PULLUP);
31+
attachInterrupt(digitalPinToInterrupt(irq_pins[m]),handleInterrupt,FALLING);
3332
modules[m].writeRegister(STANDBY_REGISTER,STANDBY_VALUE);
34-
modules[m].writeRegister(STANDBY_SENSITIVITY_REGISTER,STANDBY_SENSITIVITY_VALUE);
33+
modules[m].writeRegister(REPEAT_RATE_REGISTER,REPEAT_RATE_VALUE);
3534
modules[m].writeRegister(CALIBRATION_REGISTER,CALIBRATION_VALUE);
3635
modules[m].writeRegister(RECALIBRATION_REGISTER,RECALIBRATION_VALUE);
37-
if(DEBUG)
38-
{
39-
Serial.print("CAP1188 Module #");
40-
Serial.println(m + 1);
41-
Serial.println("- Detection: Successful\n");
42-
}
36+
modules[m].writeRegister(CYCLE_REGISTER,CYCLE_VALUE);
37+
modules[m].writeRegister(STANDBY_CYCLE_REGISTER,STANDBY_CYCLE_VALUE);
38+
modules[m].writeRegister(SENSITIVITY_REGISTER,SENSITIVITY_VALUE);
39+
modules[m].writeRegister(STANDBY_SENSITIVITY_REGISTER,STANDBY_SENSITIVITY_VALUE);
40+
Serial.print("CAP1188 Module #");
41+
Serial.println(m + 1);
42+
Serial.println("- Detection: Successful\n");
4343
}
4444
else
4545
{
46-
if(DEBUG)
47-
{
48-
Serial.print("CAP1188 Module #");
49-
Serial.println(m + 1);
50-
Serial.println("- Detection: Failed\n");
51-
Serial.println("CHECK CONNECTIONS AND REBOOT SYSTEM TO PROCEED");
52-
}
46+
Serial.print("CAP1188 Module #");
47+
Serial.println(m + 1);
48+
Serial.println("- Detection: Failed\n");
49+
Serial.println("CHECK CONNECTIONS AND REBOOT SYSTEM TO PROCEED");
5350
while(true);
5451
}
5552
}
5653
}
5754

58-
void detectTouch()
55+
void handleInterrupt()
5956
{
60-
for(byte m = 0; m < NUM_OF_MODULES; m++)
61-
{
62-
byte active_bits = modules[m].touched();
63-
for(byte s = 0; s < SENSORS_PER_MODULE; s++)
64-
{
65-
byte index = m*SENSORS_PER_MODULE + s;
66-
if((active_bits & (1 << s)))
67-
{
68-
if(sensor_state[index] == 0)
69-
{
70-
if((millis() - trigger_timer[index] >= TRIGGER_THRESHOLD))
71-
{
72-
sensor_state[index] = 1;
73-
touch[index] = 1;
74-
active_sensors++;
75-
trigger_timer[index] = millis();
76-
}
77-
}
78-
}
79-
else
80-
{
81-
if(sensor_state[index] == 1)
82-
{
83-
sensor_state[index] = 0;
84-
release[index] = 1;
85-
active_sensors--;
86-
}
87-
}
88-
}
89-
}
57+
interrupt = true;
9058
}

‎src/utilities.cpp

+65-51
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Date: May 2024
1111
#include "utilities.h"
1212

1313
// Define Variables
14-
unsigned long flash_timer = 0;
14+
unsigned long timer = 0;
1515

1616
// Define Functions
1717
void setupSerial()
@@ -26,40 +26,40 @@ void setupSerial()
2626
}
2727
}
2828

29-
byte getColorValue(byte ratio)
29+
void setupLED()
3030
{
31-
byte value;
32-
value = map(ratio,0,100,0,255);
33-
if(COMMON_ANODE)
34-
{
35-
value = 255 - value;
36-
}
37-
return value;
31+
pinMode(RED_PIN,OUTPUT);
32+
pinMode(GREEN_PIN,OUTPUT);
33+
changeColor(INTENSITY,0);
3834
}
3935

40-
void changeColor(byte red_ratio,byte green_ratio)
36+
void changeColor(byte red,byte green)
4137
{
42-
analogWrite(RED_PIN,getColorValue(red_ratio));
43-
analogWrite(GREEN_PIN,getColorValue(green_ratio));
38+
analogWrite(RED_PIN,getAnalogValue(red));
39+
analogWrite(GREEN_PIN,getAnalogValue(green));
4440
}
4541

46-
void setupLED()
42+
byte getAnalogValue(byte color)
4743
{
48-
changeColor(INTENSITY,0);
49-
pinMode(RED_PIN,OUTPUT);
50-
pinMode(GREEN_PIN,OUTPUT);
44+
byte value;
45+
value = map(color,0,100,0,255);
46+
if(COMMON_ANODE)
47+
{
48+
value = 255 - value;
49+
}
50+
return value;
5151
}
5252

53-
void flashLED()
53+
void controlLED()
5454
{
5555
if(active_sensors > 0)
5656
{
57-
changeColor(0,INTENSITY);
58-
flash_timer = millis();
57+
active_sensors == 1 ? changeColor(INTENSITY,INTENSITY) : changeColor(0,INTENSITY);
58+
timer = millis();
5959
}
6060
else
6161
{
62-
if(millis() - flash_timer >= FLASH_DURATION)
62+
if(millis() - timer >= DURATION)
6363
{
6464
changeColor(INTENSITY,0);
6565
}
@@ -68,35 +68,49 @@ void flashLED()
6868

6969
void registerNotes()
7070
{
71-
for(byte index = 0; index < NUM_OF_SENSORS; index++)
72-
{
73-
if(touch[index] == 1)
74-
{
75-
sendNoteOn(notes[mapping[index]]);
76-
touch[index] = 0;
77-
if(DEBUG)
78-
{
79-
Serial.print("Sensor: ");
80-
Serial.print(index + 1);
81-
Serial.print(" | Sample: ");
82-
Serial.print(mapping[index] + 1);
83-
Serial.print(" | MIDI Note On: ");
84-
Serial.println(notes[mapping[index]]);
85-
}
86-
}
87-
if(release[index] == 1)
88-
{
89-
sendNoteOff(notes[mapping[index]]);
90-
release[index] = 0;
91-
if(DEBUG)
92-
{
93-
Serial.print("Sensor: ");
94-
Serial.print(index + 1);
95-
Serial.print(" | Sample: ");
96-
Serial.print(mapping[index] + 1);
97-
Serial.print(" | MIDI Note Off: ");
98-
Serial.println(notes[mapping[index]]);
99-
}
100-
}
101-
}
71+
for(byte m = 0; m < NUM_OF_MODULES; m++)
72+
{
73+
modules[m].writeRegister(MAIN_REGISTER,modules[m].readRegister(MAIN_REGISTER) & ~1);
74+
byte active_bits = modules[m].readRegister(SENSORS_REGISTER);
75+
for(byte s = 0; s < SENSORS_PER_MODULE; s++)
76+
{
77+
byte index = m*SENSORS_PER_MODULE + s;
78+
if((active_bits & (1 << s)))
79+
{
80+
if(sensor_state[index] == 0)
81+
{
82+
sendNoteOn(notes[mapping[index]]);
83+
sensor_state[index] = 1;
84+
active_sensors++;
85+
if(DEBUG)
86+
{
87+
Serial.print("Sensor: ");
88+
Serial.print(index + 1);
89+
Serial.print(" | Sample: ");
90+
Serial.print(mapping[index] + 1);
91+
Serial.print(" | MIDI Note On: ");
92+
Serial.println(notes[mapping[index]]);
93+
}
94+
}
95+
}
96+
else
97+
{
98+
if(sensor_state[index] == 1)
99+
{
100+
sendNoteOff(notes[mapping[index]]);
101+
sensor_state[index] = 0;
102+
active_sensors--;
103+
if(DEBUG)
104+
{
105+
Serial.print("Sensor: ");
106+
Serial.print(index + 1);
107+
Serial.print(" | Sample: ");
108+
Serial.print(mapping[index] + 1);
109+
Serial.print(" | MIDI Note Off: ");
110+
Serial.println(notes[mapping[index]]);
111+
}
112+
}
113+
}
114+
}
115+
}
102116
}

0 commit comments

Comments
 (0)
Please sign in to comment.