How many times the sensor is activated a second.

I would like it to work out how many times the sensor is activated a second and convert it to a counter to show an average of how many times it happens a second This is the code right now:

int led = 13;//LED pin
int sensor = 3; //sensor pin
int val; //numeric variable
int pass = 0;
int num = 0;
double time = 0;

void setup()
{
    pinMode(led, OUTPUT); //set LED pin as output
    pinMode(sensor, INPUT); //set sensor pin as input
    Serial.begin(9600);
}

void loop()
{

    val = digitalRead(sensor); //Read the sensor

    if(val == LOW) //when magnetic field is detected, turn led on
    {

        if(num == 0)
            {
                ++pass;
                ++num;
                Serial.println(pass);

            }
    }
    else
    {
       time = time - time;

       if(num == 1)
            {
                --num;
            }

     }

}
       time = time - time;

You do realize this is always the same as

       time = 0;

right?

is the sensor activated when the 'val' is low?

I think i know what you are trying to do, but without extra info its hard to tell, so i have guessed.

int led = 13;//LED pin
int sensor = 3; //sensor pin
int val; //numeric variable
int pass = 0;
int num = 0;
double time = 0;



void setup()
{
  pinMode(led, OUTPUT); //set LED pin as output
  pinMode(sensor, INPUT); //set sensor pin as input
  Serial.begin(9600);
}

void loop()
{



  val = digitalRead(sensor); //Read the sensor

  if (val == LOW) {          // When magnetic field is detected, turn led on

    digitalWrite(led, HIGH); // Turns led on when val is low
    num = num + 1;           // Adds 1 to num everytime the program loops when val is LOW
  }


  else {
    digitalWrite(led, LOW);  // Turns led off if val is HIGH
    num = 0;                 // resets num back to 0
  }


  Serial.println(num);        // prints num to serial monitor, move this inside your if statement if you only want it to print while val is LOW

}

You should increment the count not when the sensor is LOW but only when the sensor has changed from HIGH to LOW. To get an average per second you also need to keep track of how many seconds the program has been running.

So there are two counters, one increments when the sensor changes state to LOW, the other increments when a second has passed. Something like the following.

int led = 13;//LED pin
int sensor = 3; //sensor pin
int val; //numeric variable
int pass = 0;
int num = 0;
double time = 0;

int oldVal = HIGH;
unsigned long startTime;
unsigned int seconds = 0;

void setup()
{
  pinMode(led, OUTPUT); //set LED pin as output
  pinMode(sensor, INPUT); //set sensor pin as input
  Serial.begin(9600);
  startTime = millis(); // mark time
}

void loop()
{

  val = digitalRead(sensor); //Read the sensor

  if ( (val == LOW) && (oldVal == HIGH) ) //when magnetic field is detected, turn led on
  {
    ++num;
    Serial.println(num);

  }

  digitalWrite(led, !val);    // LED on if val is low

  oldVal = val;  // remember last read value

  // if a second has passed
  if ( millis() - startTime >= 1000 )
  {
    // increment seconds
    seconds++;

    // show average
    Serial.print("Avg:  "); Serial.println((float)num / seconds);

    // reset start time
    startTime = millis();
  }

}

This will not be 100% accurate because you won't be hitting the second if statement at exactly one second intervals, but maybe it will be close enough?