How can i stop counting when data transmitted z-axis is less than threshold level more than one time which indicates the car stop moving above the sensor? Then count continue again if the next car passby.
Need help pls, thx.
#include <RH_ASK.h>// TX library for RF 433MHZ interfacing
#include <SPI.h> // Not actually used but needed to compile
#include <Wire.h> //I2C Arduino Library
#define address 0x1E //0011110b, I2C 7bit address of HMC5883
//Create ASK Object
RH_ASK rf_driver;
//Define Pin
const int TXPin= 12;
const int ledPin =13;
int sensorPin1 = A4; //Analog input 4 I2C SDA
int sensorPin2 =A5; //Analog input 5 I2C SCL
//Variables
int num = 1; //number of magnetometer sensor #1
int count = 0; //initially zero
int x,y,z; //triple axis data
const int threshold = 0; //threshold level z-axis
char Array [20];
//Subfunction
void txdataTrans();
void setup() {
Serial.begin (9600);
Wire.begin();
//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
rf_driver.init(); //Intialize ASK Object
pinMode(ledPin,OUTPUT);
}
void loop() {
txdataTrans();
digitalWrite(ledPin, HIGH); // sets the digital pin 13 on to show transmitting
digitalWrite(ledPin, LOW); // sets the digital pin 13 off
}
void txdataTrans(){
//Tell the HMC5883 where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();
//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){
x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}
if(z<threshold){ //if z-axis less than threshold, start counting number of vehicle
count++;
}
sprintf(Array, "%d,%d,%d,%d,%d.",num,x,y,z,count); //converts all variables into a string
rf_driver.send((uint8_t *)Array, strlen(Array)); //Send out the string including ",".
rf_driver.waitPacketSent();
// Message transmitted
Serial.print("#");
Serial.print(num);
Serial.print(", x:"); // x axis
Serial.print(x);
Serial.print(", y:"); // y axis
Serial.print(y);
Serial.print(", z:"); // z axis
Serial.print(z);
Serial.print(", Vehicle counted:");
Serial.println(count);
delay(250);
}