arduino alarm system

hello. i would like to have some help with a project i'm doing. basically doing a alarm system using arduino and a ping sensor the hc-sr04 .i would like to ask how do i save the distance received from the loop to a variable so that i can establish the distance from my sensor and the wall then from that distance save i will base if there is a certain change in it and triggers the alarm. thank you.

What have you done so far ? Can we assume that you know how to set a variable to a value ?

i'm really new to arduino and the program is copied from someone. i do understand how it works but i don't know how to implement my idea.i'm gonna post the code.

#include <NewPing.h>
#include <NewTone.h>

// Select which PWM-capable pins are to be used.
#define RED_PIN 10
#define GREEN_PIN 11
#define BLUE_PIN 9

#define TRIGGER_PIN 6 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
boolean triggered = false;

#define ALARM 3
float sinVal;
int toneVal;

void setup(){

//set pinModes for RGB strip


//reset lights

Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.


void loop(){
if(triggered == true){
delay(50);// Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS =; // Send ping, get ping time in microseconds (uS).
unsigned int distance = uS / US_ROUNDTRIP_CM;
if(distance < 50){
triggered = true;

void alarm(){
color(255,0,0); //red
color(255,255,0); //yelow

for (int x=0; x<180; x++) {
// convert degrees to radians then obtain sin value
sinVal = (sin(x*(3.1412/180)));
// generate a frequency from the sin value
toneVal = 2000+(int(sinVal*1000));
NewTone(ALARM, toneVal);

//helper function enabling us to send a colour in one command
void color (unsigned char red, unsigned char green, unsigned char blue) // the color generating function
analogWrite(RED_PIN, red);
analogWrite(BLUE_PIN, blue);
analogWrite(GREEN_PIN, green);

If you did

    unsigned int previousDistance = currentDistance;
    unsigned int currentDistance = uS / US_ROUNDTRIP_CM;

You could compare the current and previous distances to establish how much it had changed.

so i should do something like this

unsigned int uS =; // Send ping, get ping time in microseconds (uS).
   unsigned int previousDistance =currentDistance;[color=#222222][/color]
     unsigned int currentDistance = uS / US_ROUNDTRIP_CM;[color=#222222][/color]
        triggered = true;

i don't know if this is correct.with this wouldn't the previous distance always be the same as the current distance?

wouldn't the previous distance always be the same as the current distance?

No. Think about it. If the current distance were say 20 cm and was saved in previousDistance when currentDistance were measured again it could be different.

currentDistance!=previousDitanceNot such a good idea as the measurement is not likely to be accurate or consistent. In any case, the distance may have increased since the previous measurement. Do you want the alarm triggered in that case ?

Better to calculate the difference between previous and current distances and if it exceeds some threshold and the distance is reducing then raise the alarm.

thank you for the help.i'm gonna try various things. somehow get how i should do it and hoping for the best results.thanks again

hello. i solved my problem about the distace. i just want to ask something about the color ouput on the strip leds i used.

color(255,0,0); //red
  color(255,255,0); //yelow

the output i'm seeing isn't yellow but it a program based problem or i did something wrong with how i connected it.