Nano timing issue [SOLVED]

I am making at vibration detector so I know when my washer or dryer has finished its program as they are located in a garage away from the house.

So, I have a Nano, two 140C001 vibration sensors (which are attached to my washer and dryer) and a RF433 transmitter/reciever (using virtualwiring.h). I am sending an interger between 1 & 4 depending on the the time a signal is recieved from one of the vibration sensors.

For testing I have my 'no_vibration_interval' set to 30 seconds and everything works fine, if no signal is received from either sensors for 30 seconds I get a "1" transmitted, signal from one and not the other I get a "2" etc (see below code for the 4 possible sensor states).

Now, my washer has periods in its program when it is idle (i.e pumping) in which no vibrations are detected for over 30 seconds. So I want to set the 'no_vibration interval' to 5 minutes or 300000ms, but when I do this the program fails to send signal corresponding to not vibration after a 5 minute period. I have tried several smaller time intervals but 30 seconds and lower are the only ones that seem to work.

It works as is and does what I want, but this issue is just sitting there at the back of my mind shouting 'But why???'.

#include <VirtualWire.h>

// Define the data pin for the VirtualWire module
const int transmit_pin = 12;

// Define the vibration sensor input pins
const int vibration_pin_1 = 4;
const int vibration_pin_2 = 7;

// Define the time interval for checking the vibration sensors
const int interval = 1000;

// Define the time interval for checking if there is no vibration
const int no_vibration_interval = 30000;

// Define variables to hold the last vibration time for each sensor
unsigned long last_vibration_time_1 = 0;
unsigned long last_vibration_time_2 = 0;

void setup() {
  // Initialize the VirtualWire module
  vw_set_tx_pin(transmit_pin);
  vw_setup(2000);

  // Initialize the vibration sensor input pins
  pinMode(vibration_pin_1, INPUT);
  pinMode(vibration_pin_2, INPUT);

  // Set the last vibration times to the current time
  last_vibration_time_1 = millis();
  last_vibration_time_2 = millis();
}

void loop() {
// Check the first sensor
  if (digitalRead(vibration_pin_1) == HIGH) {
    // If the sensor is vibrating, record the current time
    last_vibration_time_1 = millis();
  }

  // Check the second sensor
   if (digitalRead(vibration_pin_2) == HIGH) {
    // If the sensor is vibrating, record the current time
    last_vibration_time_2 = millis();
  }

    // Calculate the time since the last vibration for each sensor
  unsigned long timeSinceVibration1 = millis() - last_vibration_time_1;
  unsigned long timeSinceVibration2 = millis() - last_vibration_time_2;

  // If both sensors have no vibration, send a message
  if (timeSinceVibration1 > no_vibration_interval && timeSinceVibration2 > no_vibration_interval) {
    vw_send((uint8_t *)"1", 1);
    vw_wait_tx();
  }

  // If there is vibration on sensor 1 and no vibration on sensor 2, send a message
  else if (timeSinceVibration1 < no_vibration_interval && timeSinceVibration2 > no_vibration_interval) {
    vw_send((uint8_t *)"2", 1);
    vw_wait_tx();
  }

  // If there is vibration on sensor 2 and no vibration on sensor 1, send a message
  else if (timeSinceVibration1 > no_vibration_interval && timeSinceVibration2 < no_vibration_interval) {
    vw_send((uint8_t *)"3", 1);
    vw_wait_tx();
  }

  // If there is vibration on both sensors, send a message
  else if (timeSinceVibration1 < no_vibration_interval && timeSinceVibration2 < no_vibration_interval) {
    vw_send((uint8_t *)"4", 1);
    vw_wait_tx();
  }
  delay(interval);
}

An int is 16 bits. The max value it can hold is 32767. Change to unsigned long. Indeed, I would recommend changing any constants that are to be compared to millis() based time to unsigned long as well.

Brilliant, just made the change and now it is working after a 5 minute interval.

Thanks so much ToddL1962!!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.