Hi Everyone,
I'm just getting into Arduino coding although I used to code in C#, I'm using Arduino Playground - HoldButton & Arduino library for WTV020-SD-16P audio module - Audio - Arduino Forum Library to execute a function(or directly) a sound file when I click the Rotary Encoder (KY-040) button (I need it to be a rotary encoder for aother functionaity.)
I can get debug code to appear when I short / long press but the code to call the sound file does not work, tried delays but nothing. If I call the wtv020sd16p.playVoice(2); in void setup() the file plays can anyone point me in the right direction.
Thanks
#include <Wtv020sd16p.h>
//Wtv020sd16p Pins
int resetPin = 5; // The pin number of the reset pin.
int clockPin = 6; // The pin number of the clock pin.
int dataPin = 7; // The pin number of the data pin.
int busyPin = 11; // The pin number of the busy pin.
//Wtv020sd16p Pins End
// KY-040 Click Button Pin
int inPin = 8; // the pin number for input (for me a push button)
// KY-040 Click Button Pin End
int ledPin = 13; // LED connected to digital pin 13
int current; // Current state of the button
// (LOW is pressed b/c i'm using the pullup resistors)
long millis_held; // How long the button was held (milliseconds)
long secs_held; // How long the button was held (seconds)
long prev_secs_held; // How long the button was held in the previous check
byte previous = HIGH;
unsigned long firstTime; // how long since the button was first pressed
Wtv020sd16p wtv020sd16p(resetPin,clockPin,dataPin,busyPin);
void setup()
{
Serial.begin(9600); // Use serial for debugging
wtv020sd16p.reset();
pinMode(ledPin, OUTPUT); // sets the digital pin as output
digitalWrite(inPin, HIGH); // Turn on 20k pullup resistors to simplify switch inpu
}
void loop() {
current = digitalRead(inPin);
// if the button state changes to pressed, remember the start time
if (current == LOW && previous == HIGH && (millis() - firstTime) > 200) {
firstTime = millis();
}
millis_held = (millis() - firstTime);
secs_held = millis_held / 1000;
// This if statement is a basic debouncing tool, the button must be pushed for at least
// 100 milliseconds in a row for it to be considered as a push.
if (millis_held > 50) {
if (current == LOW && secs_held > prev_secs_held) {
ledblink(1, 50, ledPin); // Each second the button is held blink the indicator led
}
// check if the button was released since we last checked
if (current == HIGH && previous == LOW) {
// HERE YOU WOULD ADD VARIOUS ACTIONS AND TIMES FOR YOUR OWN CODE
// ===============================================================================
// Button pressed for less than 1 second, one long LED blink
if (secs_held <= 0) {
ledblink(1,750,ledPin);
Serial.print("It Works!!! Seconds held: ");
Serial.print(secs_held);
Serial.print(" Milliseconds held: ");
Serial.println(millis_held);
SelectorPosition();
}
// If the button was held for 3-6 seconds blink LED 10 times
if (secs_held >= 1 && secs_held < 3) {
ledblink(10,200,ledPin);
Serial.print("It Works!!! Seconds held: ");
Serial.print(secs_held);
Serial.print(" Milliseconds held: ");
Serial.println(millis_held);
SelectorPosition();
}
// Button held for 1-3 seconds, print out some info
if (secs_held >= 3) {
Serial.print("It Works!!! Seconds held: ");
Serial.print(secs_held);
Serial.print(" Milliseconds held: ");
Serial.println(millis_held);
SelectorPosition();
}
// ===============================================================================
}
}
previous = current;
prev_secs_held = secs_held;
}
// Just a simple helper function to blink an led in various patterns
void ledblink(int times, int lengthms, int pinnum){
for (int x=0; x<times;x++) {
digitalWrite(pinnum, HIGH);
delay (lengthms);
digitalWrite(pinnum, LOW);
delay(lengthms);
}
}
//Call Wtv020sd16p Track 2
void SelectorPosition(){
Serial.println("Soundbite 2");
wtv020sd16p.playVoice(2);
}