hi, i hope you can help me with this issue i have, i stuck.
first i use this module for PCF8575
my shematic is like the picture below, i only want to use P0 for the moment, all the other PINS are not connected. after i make this one pin to work i will figure out how to make all.
i have this code and is working fine from the arduino with out the PCF8575. when i ground pins 2 or 3 or 4 or 5 the code send midi note velocity 127 as long the pin is grounded it stay at 127, when pin release ground, it send midi note velocity 0
// LIBRARY
#include "MIDIUSB.h"
// BUTTONS
const int NButtons = 4; //*** total number of buttons on ardouino
const int buttonPin[NButtons] = {2, 3, 4, 5}; //*** define Digital Pins connected from Buttons to Arduino; (ie {10, 16, 14, 15, 6, 7, 8, 9, 2, 3, 4, 5}; 12 buttons)
int buttonCState[NButtons] = {}; // stores the button current value
int buttonPState[NButtons] = {}; // stores the button previous value
// debounce
unsigned long lastDebounceTime[NButtons] = {0}; // the last time the output pin was toggled
unsigned long debounceDelay = 80; //** the debounce time; increase if the output flickers
// MIDI Assignments
byte midiCh = 1; //* MIDI channel to be used
byte note = 36; //* Lowest note to be used; 36 = C2; 60 = Middle C
byte cc = 1; //* Lowest MIDI CC to be used
// SETUP
void setup() {
// Buttons
// Initialize buttons with pull up resistors
for (int i = 0; i < NButtons; i++) {
pinMode(buttonPin[i], INPUT_PULLUP);
}
}
////
// LOOP
void loop() {
buttons();
}
////
// BUTTONS
void buttons() {
for (int i = 0; i < NButtons; i++) {
buttonCState[i] = digitalRead(buttonPin[i]); // read pins from arduino
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
if (buttonPState[i] != buttonCState[i]) {
lastDebounceTime[i] = millis();
if (buttonCState[i] == LOW) {
// Sends the MIDI note ON
// use if using with ATmega32U4 (micro, pro micro, leonardo...)
noteOn(midiCh, note + i, 127); // channel, note, velocity
MidiUSB.flush();
}
else {
// Sends the MIDI note OFF accordingly to the chosen board
// use if using with ATmega32U4 (micro, pro micro, leonardo...)
noteOn(midiCh, note + i, 0); // channel, note, velocity
MidiUSB.flush();
}
buttonPState[i] = buttonCState[i];
}
}
}
}
////
// if using with ATmega32U4 (micro, pro micro, leonardo...)
// Arduino MIDI functions MIDIUSB Library
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);
}
i try to modify this code to work with PCF8575 because for my project i need more than 40 buttons.
my problem is that the code (second code here) is repeating it self. it gos 127 then 0 then 127 then 0 us long i keep the button press. when i release the button it stop. i cant figure out why this is happening.
i want when i pess the button to send 127, and us long the button is press to stay there. when i release the button to go back to 0. any idea?
i use only one pin P0 for the momment, if i make it work i will try to make all 16 pins to work the same way. thank u for your help
#include "PCF8575.h"
#include "MIDIUSB.h"
// Set i2c address
PCF8575 pcf8575(0x20);
byte midiCh = 1;
byte note = 36;
byte cc = 1;
int buttonCState = {}; //read current state of button
int buttonPState = {}; // read pressent state of button
// debounce
unsigned long lastDebounceTime = {0}; //debounce time
unsigned long debounceDelay = 80; // debounce delay
void setup()
{
Serial.begin(115200);
for (int i = 0; i < 3; i++) // use only first 3 pins
pcf8575.pinMode(i, INPUT);
pcf8575.begin();
}
void loop() {
for (int i = 0; i < 3; i++) { // only 3 pins
int buttonCState = pcf8575.digitalRead(P0); // P0 is first input pin on pcf8575
if ((millis() - lastDebounceTime) > debounceDelay) {
if (buttonPState != buttonCState) {
lastDebounceTime = millis();
if (buttonCState == HIGH) {
noteOn(midiCh, note + P0 , 127);
MidiUSB.flush();
}
else {
noteOff(midiCh, note + P0 , 0);
MidiUSB.flush();
}
buttonPState = buttonCState ;
}
}
}
}
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);
}