A sequence of two inputs to produce an output

Hello

I am working on a school project and need some help.

First let me explain what i am trying to achieve. I have two diy floor mats that act like buttons. I intended to place one mat on one side of the door and the other on the other side. if the inside mat is stepped on, a four second for loop runs to check if the outside mat was stepped on. If the 4 seconds are up and the outside mat was not stepped on then the inside and outside mat should go back to 0. If the outside mat was stepped on in those 4 seconds then a alarm should go off and only stop once a off switch is pressed. I an using a Mp3 module ( called DFPlayer mini) that is connected to a breadboard speaker to help me produce the sound of an alarm

I have attached my code below.

I called the inside mat flagIn and the outside mat falgOut. The off switch is labeled byPass.

I am able to verify it and up load it to my Arduino uno without a problem . However it does not work. Once i upload the code it causes the speaker to immediately begins to play the alarm and the off switch does not turn it off.

[code]
#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

int  flagIn = 2;
int  flagOut = 3;
int  byPass = 7;

int flagInVal = 0;
int flagOutVal = 0;
int countVal = 0;
boolean isPlaying = false;


void setup() {
  
  pinMode(flagIn,INPUT);
  digitalWrite(flagIn,HIGH);
  pinMode(flagOut,INPUT);
  digitalWrite(flagOut,HIGH);
  pinMode(byPass,INPUT);
  digitalWrite(byPass,HIGH);

mySerial.begin (9600);
  delay(1000);
  playFirst();
  isPlaying = true;
}

void loop() {
  
if(digitalRead(flagIn)== HIGH)
{
flagIn = 1;
{
  for(int count = 0; count < 4000; count++)
  {
  if(digitalRead(flagOut) == HIGH)
{
 flagOut = 1; 
 {
  if(isPlaying)
  {
   play();
   isPlaying = false;
}
 }
}
}
}
}

if(digitalRead(byPass) == HIGH)
{
  if(isPlaying)
  {
    pause();
    isPlaying = true;  
  }
}
} 

void playFirst()
{
  execute_CMD(0x3F, 0, 0);
  delay(500);
  setVolume(15);
  delay(500);
  execute_CMD(0x11,0,1); 
  delay(500);
}
void play()
{
  execute_CMD(0x0D,0,1); 
  delay(500);
} 
void pause()
{
  execute_CMD(0x0E,0,0);
  delay(500);
}
void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

[/code]

I would greatly appreciate any advice to help fix my code or if you believe that something like this has been solved before in the forum then commenting a link to that post would mean a lot.

Thanks once again

What do expect this line of code to do?

flagIn = 1;

Isn’t “flagIn” a digital input? You’ve also done this with flagOut.

Also, how does a for loop that counts to 4000 become 4 seconds?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Thanks for the reply. I have attached the code the way you suggested let me know if you can see it . I have also included pictures of the arduino and board. A sketch aswell

#include "SoftwareSerial.h"
SoftwareSerial mySerial(10, 11);
# define Start_Byte 0x7E
# define Version_Byte 0xFF
# define Command_Length 0x06
# define End_Byte 0xEF
# define Acknowledge 0x00 //Returns info with command 0x41 [0x01: info, 0x00: no info]

int  flagIn = 2;
int  flagOut = 3;
int  byPass = 7;

int flagInVal = 0;
int flagOutVal = 0;
int countVal = 0;
boolean isPlaying = false;


void setup() {
  
  pinMode(flagIn,INPUT);
  digitalWrite(flagIn,HIGH);
  pinMode(flagOut,INPUT);
  digitalWrite(flagOut,HIGH);
  pinMode(byPass,INPUT);
  digitalWrite(byPass,HIGH);

mySerial.begin (9600);
  delay(1000);
  playFirst();
  isPlaying = true;
}

void loop() {
  // put your main code here, to run repeatedly:
if(digitalRead(flagIn)== HIGH)

{
  for(int count = 0; count < 4000; count++)
  {
  if(digitalRead(flagOut) == HIGH)
{
 
 {
  if(isPlaying)
  {
   play();
   isPlaying = false;
}
 }
}
}
}


if(digitalRead(byPass) == HIGH)
{
  if(isPlaying)
  {
    pause();
    isPlaying = true;  
  }
}
} 

void playFirst()
{
  execute_CMD(0x3F, 0, 0);
  delay(500);
  setVolume(15);
  delay(500);
  execute_CMD(0x11,0,1); 
  delay(500);
}
void play()
{
  execute_CMD(0x0D,0,1); 
  delay(500);
} 
void pause()
{
  execute_CMD(0x0E,0,0);
  delay(500);
}
void setVolume(int volume)
{
  execute_CMD(0x06, 0, volume); // Set the volume (0x00~0x30)
  delay(2000);
}

void execute_CMD(byte CMD, byte Par1, byte Par2)
// Excecute the command and parameters
{
// Calculate the checksum (2 bytes)
word checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2);
// Build the command line
byte Command_line[10] = { Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, highByte(checksum), lowByte(checksum), End_Byte};
//Send the command line to the module
for (byte k=0; k<10; k++)
{
mySerial.write( Command_line[k]);
}
}

[/code]

Thanks once again :slight_smile:

arduino_pic_2[1].jpg