Code explaination

Hi guys,i need to create a program which allow my ip camera to send out a noise from the speaker, when it detect a motion more than 30sec. (contentious HIGH for more than 30 sec) This program is working ,but dont understand the program at all. can someone pls explain it to me in the simplest way.

int ledPin = 13;//Declare the led pin number
int PushButton = 2;//Declare the PushButton number
int SpeakerPin = 11;//Declare the speaker pin number
int freq = 16400;//The frequency output when HIGH
unsigned long alarmStart;
unsigned long timerStart;
int lastVal;
int alarmState;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT); 
  pinMode(PushButton, INPUT);
  pinMode(SpeakerPin, OUTPUT);
}

void loop()
{
  int Val = digitalRead(PushButton);
  digitalWrite(ledPin, Val);
  if (Val == HIGH) 
  {
    if (lastVal == LOW)
    {
      timerStart = millis();
    }
    if (millis() - timerStart >= 30000UL) //30 seconds continuous sensor 
    {
      if (!alarmState)
      {
        alarmStart = millis();
        alarmState = 1;
      }
    }
  }
  else 
  {
    alarmState = 0;
  }
  lastVal = Val;
  soundAlarm();
}
  
void soundAlarm() 
{
  if (millis() - alarmStart <= 30000UL) // 30 seconds alarm time
  {
    tone(SpeakerPin, freq);// Speaker give out 16.4KHZ frequency
  }
  else
  {
    tone(SpeakerPin, 0);// Speaker give out 0 frequency
  }
}

Hi jason89, welcome to the forum.

Are you in any way related to max1994 who made a very similar post here?

http://forum.arduino.cc/index.php?topic=265943.0

You seem to have the same IP address as max1994.

And do you recall me mentioning to not cross post?

Did you manage to forget all that?

I'll repeat what I said in the other thread:

What is really needed here is a device that sends out an annoying noise when people cross-post the same question four or five times in a row. Something only audible to cross-posters.

Cross-post deleted.

Let me try again:

DO NOT CROSS-POST, IT WASTES TIME.

Edit: Judging by your IP address, this is an academic project - do your own write-up.

Hi guys,i need to create a program which allow my ip camera to send out a noise from the speaker, when it detect a motion more than 30sec. (contentious HIGH for more than 30 sec) This program is working ,but dont understand the program at all. can someone pls correct the way i am explain the program in a simpler way. The program is working, just needing help explaining it, as my english is not good

int ledPin = 13;//Declare the led pin number
int PushButton = 2;//Declare the PushButton number
int SpeakerPin = 11;//Declare the speaker pin number
int freq = 16400;//The frequency output when HIGH
unsigned long alarmStart;//Alarm start counting
unsigned long timerStart;//Timer start counting
int lastVal;//Remembering the previous reading from the inputpin state
int alarmState;//The current state of the output pin

void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);//Set ledPin as output
pinMode(PushButton, INPUT);//Set pushButton as input
pinMode(SpeakerPin, OUTPUT);//Set Speakerpin as output
delay(30000);//give the processor time to start up
}

void loop()
{
int Val = digitalRead(PushButton);//Read PushButton state
digitalWrite(ledPin, Val);//LedPin turn on and off according to HIGH or LOW input
if (Val == HIGH)//If Val read pushButton as HIGH state
{
if (lastVal == LOW)if
{
timerStart = millis();//if Val become low timer start counting
}
if (millis() - timerStart >= 30000) //if the Val state is at there for longer than 30 seconds,it take it as actual current state
{
if (Val != alarmState)//Check to see if the Val state has changed,due to motion detectection
{
alarmStart = millis();//Start counting When Val equal to HIGH
alarmState = 1;//HIGH
}
}
}
else
{
alarmState = 0;//LOW
}
lastVal = Val;//lastVal equal to the Val input state
soundAlarm();//Speaker output
}

void soundAlarm() //Speaker function declaration
{
if (millis() - alarmStart <= 30000) // 30 seconds alarm time duration
{
tone(SpeakerPin, freq);// Speaker give out 16.4KHZ frequency
}
else
{
tone(SpeakerPin, 0);// Speaker give out 0 frequency
}
}

I made the comments better, this should help you.

void loop()
{
int Val = digitalRead(PushButton);//Read PushButton state
digitalWrite(ledPin, Val);//LedPin turn on and off according to the button's state

if (Val == HIGH)//If the button is pressed (HIGH), go into this IF statement
{
if (lastVal == LOW) // if the buttons last state was low, indicating it was released, go into this IF statement
{
// this is the result of Val being HIGH and lastVal being LOW
timerStart = millis(); // start the timer
}

if (millis() - timerStart >= 30000) //if 30 seconds or more has past, go into this IF statement
{
if (Val != alarmState)//If the button is HIGH and the alarm state is LOW (Motion detected), go into this IF statement
{
alarmStart = millis();//Start counting
alarmState = 1;// Set alarm state to HIGH
}
}
}
else // button was not pressed
{
alarmState = 0;//set alarm to LOW
}
lastVal = Val;//lastVal equal to the Val input state
soundAlarm();//Speaker output
}

void soundAlarm() //Speaker function declaration
{
if (millis() - alarmStart <= 30000) // play tone for 30 seconds
{
tone(SpeakerPin, freq);// Speaker give out 16.4KHZ frequency
}
else
{
tone(SpeakerPin, 0);// Speaker give out 0 frequency
}
}