/* Midi message on one button, LCD and Bounce effect */
#include <LiquidCrystal_I2C.h>
#include <CD74HC4067.h>
#include "pitches.h"
/*
using ace_button::AceButton;
using ace_button::ButtonConfig;
using ace_button::LadderButtonConfig;
/////////////// Test resistors ladder
// The ADC pin used by the resistor ladder.
static const uint8_t BUTTON_PIN = A1;
static const uint8_t NUM_BUTTONS = 5;
static AceButton b0(nullptr, 0);
static AceButton b1(nullptr, 1);
static AceButton b2(nullptr, 2);
static AceButton b3(nullptr, 3);
static AceButton b3(nullptr, 4);
// button 4 cannot be used because it represents "no button pressed"
static AceButton* const BUTTONS[NUM_BUTTONS] = {
&b0, &b1, &b2, &b3, &b4
};
static const uint8_t NUM_LEVELS = NUM_BUTTONS + 1;
static const uint16_t LEVELS[NUM_LEVELS] = {
0, // R0= 220 Ohms / 0% (0.022%) / 0
204, // R1= 2K5 / 20% / 204
414, // R2= 6K8 / 40,48% / 414
614, // R3= 15K / 60% / 614
815, // R4 = 39K / 79,59% / 815
1023, // 100%, open circuit
};
*/
#define SPEAKER_PIN 12
// Instantiate an analog multiplexer 74HC4067 "A"
CD74HC4067 my_mux(8, 9, 10, 11); // create a new CD74HC4067 object with its four control pins SO to S3
const int signal_pin = A0; // select a pin to connect SIG to share with the 16 channels of the CD74HC4067
const uint8_t buttonPins[] = { 6, 5, 4, 3, 2};
const int buttonTones[] = {
NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
NOTE_G4
};
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
/*
int button1 = 2; //<-- je declare un btn type arcade sur la broche digit 2
int button2 = 3;
int button3 = 4;
int button4 = 5;
int button5 = 6;
int lastState; //déclare un "etat" ancien
int currentState;//déclare un "etat" actuel
*/
/*
const int nommidi[] = {
0x20, 0x21, 0x22, 0x23, 0x24
};
const int nomscomplets = sizeof(nommidi) / sizeof(nommidi[0]);
*/
/*
int noteC1 = 0x20; //jouer la note C1 (0x24 en hexadecimal)
int noteC2 = 0x21;
int noteC3 = 0x22;
int noteC4 = 0x23;
int noteC5 = 0x24;
*/
LiquidCrystal_I2C lcd(0x27, 16, 2);
//////////////////////////////////////////////////////////////////////////////////////
// SETUP
//////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
/*
pinMode(button1, INPUT);// je met mon btn en mode INPUT
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
*/
for (uint8_t i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(signal_pin, INPUT); // set the initial mode of the common pin SIG.
// This can be changed in loop() for each channel.
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(F("Poulet basquaise"));
lcd.setCursor(0, 1);
lcd.print("> oooo");
delay(250);
lcd.setCursor(0, 0);
lcd.print("Poulet basquaise");
lcd.setCursor(0, 1);
lcd.print("> oooo");
delay(250);
lcd.setCursor(0, 0);
lcd.print("Poulet basquaise");
lcd.setCursor(0, 1);
lcd.print("> oooo");
delay(250);
lcd.setCursor(0, 0);
lcd.print("Poulet basquaise");
lcd.setCursor(0, 1);
lcd.print("> oooo <");
delay(250);
lcd.setCursor(0, 0);
lcd.print("Poulet basquaise");
lcd.setCursor(0, 1);
lcd.print("> oooo");
delay(250);
lcd.setCursor(0, 0);
lcd.print("Poulet basquaise");
lcd.setCursor(0, 1);
lcd.print("> oooo");
delay(250);
lcd.setCursor(0, 0);
lcd.print("Poulet basquaise");
lcd.setCursor(0, 1);
lcd.print("> oooo");
delay(250);
lcd.setCursor(0, 0);
lcd.print("Poulet basquaise");
lcd.setCursor(0, 1);
lcd.print("> oooo");
delay(250);
lcd.clear();
}
//////////////////////////////////////////////////////////////////////////////////////
// LOOP
//////////////////////////////////////////////////////////////////////////////////////
void loop()
{
/*
/////////////// Lecture des buttons
currentState = digitalRead(button1); //écouteur d'événement sur broche 2
if(currentState == LOW) //si mon Etat Actuel est "ENFONCE"
{
if(lastState == HIGH)//si mon Ancien Etat était "PAS ENFONCE"
{
sendNote(0x90,noteC1,110);//Correspond à MIDI on canal 1, note C1, velocité de 110
}
lastState = LOW;//mon nouveau Ancien Etat est "ENFONCE"
}
else //par contre
{
if (currentState == HIGH) //si mon Etat Actuel est "PAS ENFONCE"
{
if (lastState == LOW)//si mon Ancien Etat était "ENFONCE"
{
sendNote(0x80,noteC1,0);//Correspond à MIDI off canal 1, note C1 , velocité de 0
//lcd.setCursor(0, 1);
//lcd.print("Note off");
//delay(250);
//lcd.clear();
}
lastState = HIGH;//mon nouveau Ancien Etat est "PAS ENFONCE"
}
}
*/
////// PITCH Notes read and play
int pitch = 0;
for (uint8_t i = 0; i < numTones; i++)
{
if (digitalRead(buttonPins[i]) == LOW)
{
pitch = buttonTones[i];
}
}
if (pitch) {
tone(SPEAKER_PIN, pitch);
} else {
noTone(SPEAKER_PIN);
}
////// MUX POTS loop through channels 0 - i
for (int i = 0; i < 9; i++) {
my_mux.channel(i);
volatile int val = analogRead(signal_pin); // Read analog value
Serial.println("Channel "+String(i)+": "+String(val)); // Print value
lcd.setCursor(0, 0);
lcd.print("Channel "+String(i)+": "+String(val));
//delay(500);
lcd.flush();
}
//delay(2000);
/*
///////Fonction sendNote
void sendNote(int cmd, int note, int velocity)
{
Serial.write(cmd );//Ecrit cmd sur le port serie
Serial.write(note );//Ecrit note sur le port serie
Serial.write(velocity );//Ecrit velocity sur le port serie
lcd.setCursor(0, 0);
lcd.print("Note "+String(note)+" vel "+String(velocity));
// lcd.setCursor(7, 0);
// lcd.print(note);
// lcd.setCursor(10, 0);
// lcd.print(velocity);
}
*/
}