Go Down

Topic: Help with button action  (Read 142 times) previous topic - next topic

Jordan_elder

Dec 05, 2018, 09:36 pm Last Edit: Dec 05, 2018, 09:38 pm by Jordan_elder
Hello - I am working on a project where once a button is pressed, a sound file will play and LED lights will come on. Ideally, I'd like to have the led lights turn off once the sound file is complete/played once. The next time the button is pressed, a different sound file (in sequence) would play and the same LED lights would turn on again for the duration of the sound file. This would repeat until the 11th press when the system would loop back to the beginning and restart.

I'm relatively new at all of this but have scoured the forums to put together a schematic and program that I thought would work. However, when I finally had it set up, nothing happened. No LED lights or sounds.

Below is the code I attempted to use. Any information, tips, or suggestions you may have would be much appreciated. I am sure that I made this a lot harder than it needed to be, but have been unable to figure out how to simplify.

Code: [Select]

#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Wire.h>
#include <I2Cdev.h>
#include <Button.h>


int inPin = 2;         // the number of the input pin
int outPin = 4;       // the number of the output pin
int ledPin = 5;

byte buttonPresses = 0;                // how many times the button has been pressed
byte lastPressCount = 0;               // to keep track of last press count

int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

int brightness = 0;    // how bright the LED is

int track = 20;
int prev = 20;

//Button button = Button(12,PULLDOWN);

boolean state = false; // the current state of the circuit

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 500;   // the debounce time, increase if the output flickers
long looptime = 0;     // the last time the loop was played
long gtime = 0;

SoftwareSerial mySerial(0, 1); // RX, TX

void setup()
{
  Wire.begin();              // join I2C bus
  //Serial.begin(38400);       // initialize serial communication
  //while (!Serial);           // wait for Leonardo enumeration, others continue immediately
  Serial.println("INIT");

  
  mySerial.begin(9600);
  mp3_set_serial(mySerial);  //set Serial for DFPlayer-mini mp3 module

  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);
  buttonPresses++;                  // increment buttonPresses count
  delay(250);                       // debounce switch
  

  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
      {
        (buttonPresses == 1);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(001);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 2);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(002);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 3);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(003);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 4);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(004);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 5);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(005);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 6);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(006);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 7);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(007);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 8);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(011);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 9);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(012);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        (buttonPresses == 10);
        digitalWrite(ledPin, HIGH);
        looptime = millis();
        mp3_play(010);
        for(brightness = 0; brightness < 200; brightness++) {
        analogWrite(outPin, brightness);
        delay(16);
      }
      }
      {
        if (buttonPresses == 11) buttonPresses = 0;         // rollover every 11th press
        if (lastPressCount != buttonPresses);              // only do output if the count has changed
      }
      Serial.print ("Button press count = ");          // out to serial
      Serial.println(buttonPresses, DEC);
      previous = reading;
}
}

el_supremo

#1
Dec 05, 2018, 09:43 pm Last Edit: Dec 05, 2018, 09:45 pm by el_supremo
Code: [Select]
        (buttonPresses == 1);
This, and similar statements, does nothing.
I think what you are trying to do actually needs a switch statement. Look it up. Give it a try.

Pete
And don't crosspost
Don't send me technical questions via Private Message.

Jordan_elder

Thank you very much. I will look it up and see what I can come up with. I apologize about the crossposting. I realized that my initial post may have been misplaced, so I posted in what I thought would be the correct forum. I will be sure not to do this in the future.

el_supremo

FYI: If you misplace a post, the "correct" way to fix that is to click on "Report to moderator" at the bottom of the message and ask a moderator to move it for you.

Pete
Don't send me technical questions via Private Message.

wvmarle

More issues:

Code: [Select]

void loop()
{
  reading = digitalRead(inPin);
  buttonPresses++;                  // increment buttonPresses count
  delay(250);                       // debounce switch
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {

buttonPresses++ is at the wrong place: this should only be incremented when you have a button press recorded (so after the if statement).

Then you're doing debouncing twice. That's not needed. Drop the delay(250) and your code will be much more responsive as well.

Remember to somewhere make sure previous is set back to HIGH (not sure if that's the case for you - do an autoformat and all the blocks line up nicely making it a lot more readable). You also don't seem to update the value of the variable time, so that part of the debounce won't work. That's not good a name, as there's a library called like that: you probably see the name being highlighted, that's a warning sign. Pick another (preferably more descriptive) name, buttonPressTime or so.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

PaulS

Code: [Select]
  delay(250);                       // debounce switch
If your switches are still bouncing a quarter of a second later, you REALLY need to invest another nickel in your switches.
The art of getting good answers lies in asking good questions.

Go Up