making display led and playing audio parallel when button is pressed

In this below programme: i am facing a problem while trigger button on the loop() function.
it should display "CHECK CONTINUITY" on LCD and play "2.wav" simultaneously which is not happening.

instead of that it only displays the message "CHECK CONTINUITY" on LED . when next time hit the button. it play the "2.wav" file than execute the "p==2" condition.

can anyone suggest to me. I am new to this field.


//YWROBOT
//Comible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <Servo.h>

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo

TMRpcm tmrpcm;

LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 20 chars and 4 line display

int L1 = 8;
int L2 = 6;
int L3 = 5;
int L4 = 3;
int L5 = 2; //5 LED pin

int buttonPin = 7; //the number of the pushbutton pin

int de=50; // delay time

int p=0; // variable for patten

int buttonState = 0; //variable for reading the pushbutton status

void setup()
{
lcd.init(); // initialize the lcd
lcd.init(); // Print a message to the LCD.

Serial.begin(9600);
pinMode(L1, OUTPUT);
pinMode(L2, OUTPUT);
pinMode(L3, OUTPUT);
pinMode(L4, OUTPUT);
pinMode(L5, OUTPUT);

pinMode(buttonPin, INPUT);

tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)){
Serial.println("SD fail");
return;
}

myservo.attach(10); // attaches the servo on pin 10 to the servo object

lcd.backlight();
lcd.setCursor(6,0);
lcd.print("HELLO");
lcd.setCursor(0,1);
lcd.print("TALKING DISCHARGE ROD");
lcd.setCursor(4,2);
lcd.print("WELCOMES YOU");
lcd.setCursor(0,3);
lcd.print("PLEASE FOLLOW INST..");
delay(5000);
lcd.clear();

tmrpcm.setVolume(6);
tmrpcm.play("1.wav");
lcd.setCursor(1,1);
lcd.print("USE ELECTRICAL PPE");
delay(de);
}
void loop()
{

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH)
{
p++;
delay(2000);
}
if(p == 1)
{
lcd.clear();
lcd.setCursor(2,1);
lcd.print("CHECK CONTINUTY");
tmrpcm.setVolume(5);
tmrpcm.play("2.wav");
delay(de);
}

if(p == 2)
{
digitalWrite(L1,1);
digitalWrite(L2,0);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,0); //1

tmrpcm.setVolume(5);
tmrpcm.play("3.wav");
delay(de);
lcd.clear();
lcd.setCursor(3,1);
lcd.print("CONNECT CLAMP");
lcd.setCursor(2,2);
lcd.print("WITH EARTH STRIP");
delay(de);
}

if(p == 3)
{
digitalWrite(L1,1);
digitalWrite(L2,1);
digitalWrite(L3,0);
digitalWrite(L4,0);
digitalWrite(L5,0); //2

tmrpcm.setVolume(5);
tmrpcm.play("4.wav");
delay(de);
lcd.clear();
lcd.setCursor(2,1);
lcd.print("CHECK NO VOLTAGE");
delay(de);
}

if(p == 4)
{
digitalWrite(L1,1);
digitalWrite(L2,1);
digitalWrite(L3,1);
digitalWrite(L4,0);
digitalWrite(L5,0); //3

tmrpcm.setVolume(5);
tmrpcm.play( "5.wav");
delay(de);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("WE ARE GOOD TO GO");
lcd.setCursor(1,1);
lcd.print("FOR SAFE DISCHARGE");
lcd.setCursor(6,2);
lcd.print("PLEASE");
lcd.setCursor(1,3);
lcd.print("OPERATE THE LIVER");
delay(de);
}

if(p == 5)
{
digitalWrite(L1,1);
digitalWrite(L2,1);
digitalWrite(L3,1);
digitalWrite(L4,1);
digitalWrite(L5,0); //4
delay(de);

}

if(p == 6)
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
digitalWrite(L1,1);
digitalWrite(L2,1);
digitalWrite(L3,1);
digitalWrite(L4,1);
digitalWrite(L5,1); //5
delay(de);

myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
}
}

Your problem is right here:

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH)
  {
    p++;
    delay(2000);
  }

It is going to be a problem pressing the button at exactly the right time because you are checking that the button IS pressed instead of when it BECOMES pressed. The delay(2000) only adds to the problem.

I would recommend looking at the following tutorial:

StateChangeDetection

Also, how is your button wired? The way you have it configured and coded implies you have a pull-down resistor.

TMRpcm uses Timer1 on a Arduino Uno.
Servo uses Timer1 on a Arduino Uno.
Do you have an Arduino Uno ?

Yes, I have Arduino Uno.

Attaching circuit details

Problem with below code, After one push-button it is not going ahead.

It is executing Case 1. when next hi button not executing Case 2.

Can you suggest where is the issue.

//YWROBOT
//Comible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#define SD_ChipSelectPin 4
#include <TMRpcm.h>
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo 

TMRpcm tmrpcm;

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display

int L1 = 2;
int L2 = 3;
int L3 = 5;
int L4 = 6;
int L5 = 8;  //5 LED pin

int buttonPin = 7; //the number of the pushbutton pin

int de=5000; // delay time

int p=0;  // variable for  patten

//int buttonState = 0;    //variable for reading the pushbutton status

unsigned long servoMillis = 0;
unsigned long servointerval = de;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button


void setup()
{
   lcd.init();                      // initialize the lcd 
   lcd.init();                         // Print a message to the LCD.
   
   Serial.begin(9600);
   pinMode(L1, OUTPUT);
   pinMode(L2, OUTPUT);
   pinMode(L3, OUTPUT);
   pinMode(L4, OUTPUT);
   pinMode(L5, OUTPUT);

   pinMode(buttonPin, INPUT_PULLUP);

   tmrpcm.speakerPin = 9;
   Serial.begin(9600);
   if (!SD.begin(SD_ChipSelectPin)){
       Serial.println("SD fail");
       return;
   }

   myservo.attach(10);  // attaches the servo on pin 10 to the servo object

   lcd.backlight();
   lcd.setCursor(6,0);
   lcd.print("HELLO");
   lcd.setCursor(0,1);
   lcd.print("TALKING DISCHARGE ROD");
   lcd.setCursor(4,2);
   lcd.print("WELCOMES YOU");
   lcd.setCursor(0,3);
   lcd.print("PLEASE FOLLOW INST..");
   delay(5000);
   lcd.clear();

   tmrpcm.setVolume(6);
   tmrpcm.play("1.wav");
   lcd.setCursor(1,1);
   lcd.print("USE ELECTRICAL PPE");
   delay(de);
}
void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 20;
   static unsigned long timer1 = 0;
   unsigned long interval1 = de;
   if (millis() - timer >= interval)
   {
       timer = millis();
       
       // read the pushbutton input pin:
       buttonState = digitalRead(buttonPin);

       // compare the buttonState to its previous state
       if (buttonState != lastButtonState)
       {
           // if the state has changed, increment the counter
           if (buttonState == LOW)
           {
               // if the current state is LOW then the button went from off to on:
               buttonPushCounter++;
               Serial.println("on");
               Serial.print("number of button pushes: ");
               Serial.println(buttonPushCounter);
           }
           else
           {
               // if the current state is HIGH then the button went from on to off:
               Serial.println("off");
           }
           // save the current state as the last state, for next time through the loop
           lastButtonState = buttonState;
       }
       // save the current state as the last state, for next time through the loop
       lastButtonState = buttonState;
   }
   switch (buttonPushCounter)
   {
       timer1 = 0;
       case 1:
           if (millis() - timer1 >= interval1)
           {
               timer1 = millis();
               lcd.clear();
               lcd.setCursor(2,1);
               lcd.print("CHECK CONTINUTY");
               tmrpcm.setVolume(5);
               tmrpcm.play("2.wav");
           }

           break;

       case 2:
           if (millis() - timer1 >= interval1)
           {
               timer1 = millis();

               digitalWrite(L1,1);
               digitalWrite(L2,0);
               digitalWrite(L3,0);
               digitalWrite(L4,0);
               digitalWrite(L5,0);  //1

               tmrpcm.setVolume(5);
               tmrpcm.play("3.wav");
           }
           if (millis() - timer1 >= interval1)
           {
               timer1 = millis();

               lcd.clear();
               lcd.setCursor(3,1);
               lcd.print("CONNECT CLAMP");
               lcd.setCursor(2,2);
               lcd.print("WITH EARTH STRIP");
           }

           break;

       case 3:
           if (millis() - timer1 >= interval1)
               {
                   timer1 = millis();

                   digitalWrite(L1,1);
                   digitalWrite(L2,1);
                   digitalWrite(L3,0);
                   digitalWrite(L4,0);
                   digitalWrite(L5,0);  //2

                   tmrpcm.setVolume(5);
                   tmrpcm.play("4.wav");
               }
           if (millis() - timer1 >= interval1)
               {
                   timer1 = millis();
                   lcd.clear();
                   lcd.setCursor(2,1);
                   lcd.print("CHECK NO VOLTAGE");
               }

           break;

       case 4:
           if (millis() - timer1 >= interval1)
               {
                   timer1 = millis();

                   digitalWrite(L1,1);
                   digitalWrite(L2,1);
                   digitalWrite(L3,1);
                   digitalWrite(L4,0);
                   digitalWrite(L5,0);  //3

                   tmrpcm.setVolume(5);
                   tmrpcm.play( "5.wav");
               }
           if (millis() - timer1 >= interval1)
               {
                   timer1 = millis();

                   lcd.clear();
                   lcd.setCursor(1,0);
                   lcd.print("WE ARE GOOD TO GO");
                   lcd.setCursor(1,1);
                   lcd.print("FOR SAFE DISCHARGE");
                   lcd.setCursor(6,2);
                   lcd.print("PLEASE");
                   lcd.setCursor(1,3);
                   lcd.print("OPERATE THE LIVER");
               }

           break;

       case 5:
           if (millis() - timer1 >= interval1)
               {
                   timer1 = millis();

                   digitalWrite(L1,1);
                   digitalWrite(L2,1);
                   digitalWrite(L3,1);
                   digitalWrite(L4,1);
                   digitalWrite(L5,0);  //4
               }

           break;

       case 6:
           for (pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
           {
               // in steps of 1 degree
               digitalWrite(L1,1);
               digitalWrite(L2,1);
               digitalWrite(L3,1);
               digitalWrite(L4,1);
               digitalWrite(L5,1);  //5
               delay(de);

               myservo.write(pos);              // tell servo to go to position in variable 'pos'
               delay(50);                       // waits 15ms for the servo to reach the position
           }

           break;

       default:
           break;
   }

}

I see a few issues:

  1. Not sure why you set timer1 to 0 here but I think it never gets executed.
   switch (buttonPushCounter)
   {
       timer1 = 0;
  1. You never set buttonPushCounter back to 0 so once it gets to 7 none of the cases will be executed.

  2. timer1 is a local static that is initialized to 0. When you press the button once it is likely almost 5 seconds have already passed and the conditional in case 1 gets executed and you set timer1 to millis(). However, when you press the button a second time it will be 5 seconds before the conditional in case 2 gets executed. Perhaps it is this delay that confuses you. Put a print in each case before the conditional and make sure case 2 is being executed.

The below code is not working for Arduino mega board. when we hit the first buton does not execute the case. motor attached to not behaving 0 to 90, can any one help me on this.

//YWROBOT
//Comible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#include <Servo.h>
#define SD_ChipSelectPin  53

//Pin Definitions
#define SERVO9G_PIN_SIG 8
Servo servo9g;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo 

TMRpcm tmrpcm;

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display




int L1 = 2;
int L2 = 3;
int L3 = 4;
int L4 = 5;
int L5 = 6;  //5 LED pin

int buttonPin = 7; //the number of the pushbutton pin

int de=5000; // delay time

int p=0;  // variable for  patten

int buttonState = 0;    //variable for reading the pushbutton status
//Global variables and defines
const int servo9gRestPosition   = 0;  //Starting position
const int servo9gTargetPosition =90;  //Position when event is detected

//
unsigned long servoMillis = 0;
unsigned long servointerval = de;

void setup()
{
    lcd.init();                      // initialize the lcd 
    lcd.init();                         // Print a message to the LCD.
    
    Serial.begin(9600);
    pinMode(L1, OUTPUT);
    pinMode(L2, OUTPUT);
    pinMode(L3, OUTPUT);
    pinMode(L4, OUTPUT);
    pinMode(L5, OUTPUT);

    pinMode(buttonPin, INPUT);

    tmrpcm.speakerPin = 9;
    Serial.begin(9600);
    if (!SD.begin(SD_ChipSelectPin)) {
        Serial.println("SD fail");
        return;
    }

    servo9g.attach(8);  // attaches the servo on pin 8 to the servo object
servo9g.attach(SERVO9G_PIN_SIG);
servo9g.write(servo9gRestPosition);
delay(100);
servo9g.detach();

    
    tmrpcm.setVolume(5);
    tmrpcm.play("0.wav");
    lcd.backlight();
    lcd.setCursor(6,0);
    lcd.print("HELLO");
    lcd.setCursor(0,1);
    lcd.print("TALKING DISCHARGEROD");
    lcd.setCursor(4,2);
    lcd.print("WELCOMES YOU");
    lcd.setCursor(0,3);
    lcd.print("PLEASE FOLLOW INST..");
    delay(8000);
    lcd.clear();

    tmrpcm.setVolume(5);
    tmrpcm.play("1.wav");
    lcd.setCursor(1,1);
    lcd.print("WEAR ELECTRICAL PPE");
    delay(de);
}
void loop()
{

    buttonState = digitalRead(buttonPin);
    
    if (buttonState == HIGH)
    {
        p++;
        delay(2000);
    }
    if(p == 1)
    {
        if(millis() - servoMillis > servointerval) {
            servoMillis = millis();
            digitalWrite(L1,1);
            digitalWrite(L2,0);
            digitalWrite(L3,0);
            digitalWrite(L4,0);
            digitalWrite(L5,0);  //1
            lcd.clear();
            lcd.setCursor(2,0);
            lcd.print("CHECK NO VOLTAGE");
            lcd.setCursor(7,1);
            lcd.print("USING");
             lcd.setCursor(2,2);
            lcd.print("VOLTAGE DETECTOR");
            tmrpcm.setVolume(5);
            tmrpcm.play("2.wav");
        }
    }

    if(p == 2)
    { 
        if(millis() - servoMillis > servointerval) {
            servoMillis = millis();
            digitalWrite(L1,1);
            digitalWrite(L2,1);
            digitalWrite(L3,0);
            digitalWrite(L4,0);
            digitalWrite(L5,0);  //2
            
            tmrpcm.setVolume(5);
            tmrpcm.play("3.wav");
            lcd.clear();
            lcd.setCursor(3,1);
            lcd.print("CONNECT CLAMP");
            lcd.setCursor(2,2);
            lcd.print("WITH EARTH STRIP");
        }
    }

    if(p == 3)
    {
        if(millis() - servoMillis > servointerval) {
            servoMillis = millis();
            digitalWrite(L1,1);
            digitalWrite(L2,1);
            digitalWrite(L3,1);
            digitalWrite(L4,0);
            digitalWrite(L5,0);  //3


            tmrpcm.setVolume(5);
            tmrpcm.play("4.wav");
        
            lcd.clear();
            lcd.setCursor(2,0);
            lcd.print("CHECK CONTINUITY");
            lcd.setCursor(6,1);
            lcd.print("BETWEEN");
            lcd.setCursor(2,2);
            lcd.print("CLAMP AND GROUND");
        }
    }

    if(p == 4)
    {
        digitalWrite(L1,1);
        digitalWrite(L2,1);
        digitalWrite(L3,1);
        digitalWrite(L4,1);
        digitalWrite(L5,0);  //4

        tmrpcm.setVolume(5);
        tmrpcm.play( "5.wav");

        lcd.clear();
        lcd.setCursor(1,1);
        lcd.print("WE ARE GOOD TO GO");
        lcd.setCursor(1,2);
        lcd.print("FOR SAFE DISCHARGE");

        if(millis() - servoMillis > servointerval) {
            servoMillis = millis();

            servo9g.attach(SERVO9G_PIN_SIG);
            servo9g.write(servo9gTargetPosition);
            delay(500);
          
            
        }
    }

    if(p == 5)
    {

        digitalWrite(L1,1);
        digitalWrite(L2,1);
        digitalWrite(L3,1);
        digitalWrite(L4,1);
        digitalWrite(L5,1);
        if(millis() - servoMillis > servointerval) {
            servoMillis = millis();

            for (pos = 90; pos >= 0; pos += 1) { // goes from 0 degrees to 180 degrees
                // in steps of 1 degree
                delay(de);
                servo9g.write(pos);              // tell servo to go to position in variable 'pos'
                //delay(50);                       // waits 15ms for the servo to reach the position
            }
        }
    }
}

[code]

Why are you checking for button state HIGH and then delaying for 2 seconds?

    if (buttonState == HIGH)
    {
        p++;
        delay(2000);
    }

You could save the last state of the button and then just check for a LOW to HIGH transition.

What happens when p increments to 6?

This is a problem. Starts at 90 and will go all the way to 32767.

            for (pos = 90; pos >= 0; pos += 1) { // goes from 0 degrees to 180 degrees

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.