odi:
to stop with the buttons the shouters before the 5 sec expires(for example if the shouters are in the middle i want to stop them before the five sec)
you should look into a State Machine approach.
It is a little above your current coding, but learning this is essential.
Also, find a button press library and make it easier on yourself.
here is what I use for buttons and includes the ability to use single, double, triple and long presses.
#include "SimplePress.h"
const byte buttonPin0 = 2;
const byte buttonPin1 = 3;
const byte buttonPin2 = 4;
SimplePress button0(buttonPin0, 500); // half second capture for multi-presses; uses default 200ms debounce
// a long press is defined as greater than the capture time (500ms in this example)
// or...
SimplePress button1(buttonPin1, 500, 150); // or specify custom debounce time in ms
SimplePress button2(buttonPin2, 500);
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
button0.begin();
button1.begin();
button2.begin();
}
void loop()
{
if(int pressCount = button0.pressed()) // check to see if there is a button press event
{
switch(pressCount) // the event can be any positive integer (the number of button presses) or -1 in the case of a long press
{
case -1:
Serial.println(F("Button 0 long Press"));
longFlashPin13();
break;
case 1:
Serial.println(F("Button 0 one Press"));
flashPin13(1);
break;
case 2:
Serial.println(F("Button 0 two Presses"));
flashPin13(2);
break;
}
}
if(int press2count = button1.pressed())
{
switch(press2count)
{
case -1:
Serial.println(F("Button 1 long Press"));
break;
case 1:
Serial.println(F("Button 1 one Press"));
break;
}
}
if (button2.pressed()) // Simple press detection
{
Serial.println(F("Button 2 Pressed!"));
}
}
void flashPin13(byte numTimes)
{
for(byte i = 0; i < numTimes; i++)
{
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
if (i < numTimes - 1) delay(250);
}
}
void longFlashPin13(void)
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}
header file:
#ifndef SIMPLEPRESS_H
#define SIMPLEPRESS_H
#include <Arduino.h>
class SimplePress{
public:
SimplePress(int _pin, uint32_t _pressInterval, uint32_t _debouncePeriod = 200);
bool begin();
int8_t pressed();
private:
byte pressCount;
byte lastState;
byte pin;
uint32_t lastMillis;
uint32_t debouncePeriod;
uint32_t pressInterval;
};
SimplePress::SimplePress(int _pin, uint32_t _pressInterval, uint32_t _debouncePeriod)
{
pin = _pin;
debouncePeriod = _debouncePeriod;
pressInterval = _pressInterval;
}
bool SimplePress::begin()
{
pinMode(pin, INPUT_PULLUP);
lastState = HIGH;
return true;
}
int8_t SimplePress::pressed()
{
byte nowState = digitalRead(pin);
if(nowState != lastState)
{
if(millis() - lastMillis < debouncePeriod) return 0;
if(nowState == LOW)
{
lastMillis = millis();
pressCount++;
}
else
{
if (millis() - lastMillis > pressInterval) // a long press
{
lastState = nowState;
pressCount = 0;
return -1;
}
}
}
if(pressCount != 0)
{
if(millis() - lastMillis > pressInterval and nowState == HIGH)
{
int presses = pressCount;
pressCount = 0;
return presses;
}
}
lastState = nowState;
return 0;
}
#endif