#include "pitches.h"
#include "LowPower.h"
#include <CapacitiveSensor.h>
const int touchPins[] = {12, 8, 7, 6, 5, 4, 3, 2};
const int buttonPins[] = {12, 8, 7, 6, 5, 4, 3, 2};
const int buttonTones[] = {
NOTE_G4, NOTE_F4, NOTE_E4, NOTE_D4,
NOTE_C4, NOTE_B4, NOTE_A4, NOTE_G3
};
const int speakerPin = 9;
const int ON_OFFbutton = A0;
const int numTones = sizeof(buttonPins) / sizeof(buttonPins[0]);
bool buttonPressed = false;
bool ledFlag = true;
bool A0_Flag = false;
int touchCalibration = 6; // valibration value between 1 & 15
static bool newNotePlayed = false;
int lastNote = 0;
int currentNote = 0;
// ******** functions out of loop *************
/*
void captouchsence() { // function not compiling
bool touchDetected = false;
for (int i = 0; i < numTones; i++) {
int capacitance = sensors[i].capacitance();
if (capacitance > threshold) { // Adjust the threshold value
touchDetected = true;
tone(speakerPin, buttonTones[i]);
}
}
if (!touchDetected)
{
tone(speakerPin, NOTE_A5);
}
}
*/
void woodwindbuttons ()
{
int firstOpenHole = -1;
for (int i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
buttonPressed = true;
firstOpenHole = i;
currentNote = buttonTones[firstOpenHole];
break; // Exit the loop once the first open hole is found
}
}
if (buttonPressed) {
tone(speakerPin, currentNote);
}
else {
currentNote = NOTE_A5;
tone(speakerPin, currentNote);
}
}
void buttonsonly ()
{
buttonPressed = false;
for (int i = 0; i < numTones; i++) {
if (digitalRead(buttonPins[i]) == LOW) {
buttonPressed = true;
currentNote = buttonTones[i];
tone(speakerPin, currentNote);
if (!buttonPressed) {
buttonPressed = false;
currentNote = NOTE_A5;
tone(speakerPin, currentNote);
}
}
}
}
// Toggle LED
void LEDtoggle() {
if (ledFlag == true) {
ledFlag = false;
digitalWrite(LED_BUILTIN, LOW);
} else {
ledFlag = true;
digitalWrite(LED_BUILTIN, HIGH);
}
}
// start of program
void setup() {
Serial.begin(9600);
digitalWrite(LED_BUILTIN, HIGH);
for (int i = 0; i < numTones; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
pinMode(ON_OFFbutton, INPUT_PULLUP);
}
/*
for (int i = 0; i < numTones; i++) {
sensors[i].init(touchPins[i], touchCalibration); // Adjust the second parameter as needed
}
const int numTones = sizeof(touchPins) / sizeof(touchPins[0]);
CapacitiveSensor sensors[numTones];
*/
pinMode(speakerPin, OUTPUT);
}
// MAIN Program area
void loop() {
// ************* ON OFF RESET ***************
if (ON_OFFbutton == LOW)
{
delay(250); //debounce
if (ON_OFFbutton == LOW)
{
int i = 0;
A0_Flag = true;
for ( i=0; ON_OFFbutton == LOW; i++ )
{
delay(100);
}
if (i <= 10) // 1 second
{
Serial.println("Reset");// reset arduino
}
else // if over 1 second sleep
{
Serial.println("sleep Mode");
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); // deep sleep mode for arduino
}
}
}
// Update current and last notes
if (lastNote != currentNote) {
Serial.println(lastNote", "currentNote);
lastNote = currentNote;
LEDtoggle ();
}
// This function Snippet of Code works as a woodwind instrument
//woodwindbuttons ();
// ***** this snippit of code is for individual buttons ****
buttonsonly ();
// ***** this snippit of code is for using captouch sensors ****
// captouchsence();
}
/*
********** end of **********
*/