Wtv020sd16p + KY-040 Click

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);

}

  // 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) {

In MY universe, 100 != 50.

      if (secs_held <= 0) {

How can you hold the switch down for less than 0 seconds?

We know that the code does something. We do NOT know what it does.

We know that you expect the code to do something. We do NOT know what you expect.

We can probably safely assume that the two somethings are not the same thing. If they were, you probably wouldn't have posted here. But, what your problem is is a complete mystery. The solution will need to remain a mystery, too.

PaulS:

  // 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) {



In MY universe, 100 != 50.



if (secs_held <= 0) {



How can you hold the switch down for less than 0 seconds?

We know that the code does something. We do NOT know what it does.

We know that you expect the code to do something. We do NOT know what you expect.

We can probably safely assume that the two somethings are not the same thing. If they were, you probably wouldn't have posted here. But, what your problem is is a complete mystery. The solution will need to remain a mystery, too.

millis_held > 50 so not sure what your point is the author of the code is saying button should be pressed for more than 50 milliseconds??

Button press is measured in milliseconds so again nothing untowards there..

The issue is as described:

// 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(); <<<<>>>>>

}

//Call Wtv020sd16p Track 2
void SelectorPosition(){

Serial.println("Soundbite 2"); <<<<>>>>>
WTV020SD.triggerVoice(2,true);<<<<>>>>>

}

millis_held > 50 so not sure what your point is the author of the code is saying button should be pressed for more than 50 milliseconds??

The code says one thing. The comment above it says something else.

            SelectorPosition(); <<<<<EVERYTHING ABOVE FIRES THIS DOES NOT>>>>>>

If that does not "fire", how can:

     Serial.println("Soundbite 2"); <<<<<THIS APPEARS IN SERIAL MONITOR>>>>>>

be true?

       WTV020SD.triggerVoice(2,true);<<<<<THIS DOES NOT FIRE>>>>>>

Code doesn't "FIRE". Functions get called. They do, or do not, do something.

Why don't you add some Serial.print() statements to the library, to see what is happening?

I know it's quite peculiar that parts of the function appear to work and not others (where the Wtv0020sd16p library is called). I'll add some Serial statements in the library to see that is happening.

Thanks