Tilt sensor project help

Thread Starter

macman23

Joined Jan 6, 2023
13
Hi, I am working on a project to activate a relay module on a timer but also for the relay to activate when I tip the tilt sensor module. I have the timer working for the relay but cant get the tilt sensor to activate the relay. I will post the code. Any help would be appreciated.

Tilt sensor with relay:
const int sigPin = 2; // the number of the tilt switch pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
boolean sigState = 0; // variable for reading the tilt switch status

int control_pin = 7;
unsigned int time_on = 2;
unsigned int time_off = 3;

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the tilt switch pin as an input:
  pinMode(sigPin, INPUT);

  Serial.begin(9600);
  pinMode(control_pin, OUTPUT);

}

void loop() {
    // read the state of the tilt switch value:
  sigState = digitalRead(sigPin);
  if (sigState == HIGH)
  {
    // turn LED on:
    digitalWrite(control_pin, LOW);
  }
  else
  {
    // turn LED off:
    digitalWrite(control_pin, HIGH);
  }

digitalWrite(control_pin, LOW);
delay(time_on * 60000);
digitalWrite(control_pin, HIGH);
delay(time_on * 10000);

 

}
 

Jerry-Hat-Trick

Joined Aug 31, 2022
552
Not sure I’ve understood exactly what you are trying to do but it looks like you are briefly checking the status of the tilt switch and acting on it, but before the relay has time to change state line 35 takes over and your delays are dominant, during which time you are not checking the tilt switch

Maybe, instead of having a delay of time_on * 60000 try a loop with 6000 steps with each step having a delay of time_on * 10 and check the status of the tilt switch in each step.
 

Thread Starter

macman23

Joined Jan 6, 2023
13
Apologies if i wasn't clear about what i want to do. I have the arduino uno connected to a relay which I would like to turn on based on a time which I set in the delay function within the code. Also I want the relay to turn on when the tilt sensor is moved. Hope this is more clear.
 

Jerry-Hat-Trick

Joined Aug 31, 2022
552
Okay, I think I understood that. My response is still valid. Once your code reaches line 35 it isn’t looking at the status of the tilt switch until the delays have been completed in your timer code and the loop starts again when it only looks at the status of the tilt switch very briefly after which line 35 resets the relay regardless of the status of the tilt switch. You need to have the code looking at the status of the tilt switch on a regular basis during the time the timer is making the relay on and off.

With your variable time_on = 2 you seem to be driving the control pin low for two minutes followed by high for 20 seconds. When the delay instruction is active the processor is doing nothing else so you need to replace the delays with a much shorter time wrapped in loops which execute lots of times (making the total time what you want it to be) and include your code to look at the status of the tilt switch within those loops.

You will find the loops run very fast so instead of a 2 minute delay try a 200 milliseconds delay in a loop which runs 600 times and you will probably not notice the very slightly longer time it takes to execute
 
Last edited:
Top