Go Down

Topic: debounce a tilt sensor and activate counter (Read 1 time) previous topic - next topic


Hi, I am programming a tilt sensor. The idea sounds quite simple but I got stuck in the programming.
Basically there are two options. Standing and standing on top (180 degree).
I programmed a debounce for the tilt. The debounce time is 1 second.
After the switching of the tilt sensor an LED shall light up for half a second. Then there is a waiting time (e.g. 10 minutes). After the 10 min the LED shall light up for another half a second. That´s it for the first szenario.
If you turn around the tilt sensor for another 180 degree it starts from the beginning. LED half a second, 10 min waiting, Led half a second. Finished so far.
But if you turn around the tilt sensor during the 10 min waiting time, the waiting time starts again. For instace: you turn the tilt around, the LED flashes, you wait 7 minutes, you turn around the tilt sensor again you wait for 10 min, the LED flashes.
Could need some help
Code: [Select]
int tiltSens = 2;
int signal = 13;
int tiltStat;
int oldtiltStat = LOW;

unsigned long counterOne = 0;
int debounceTime = 1000;
int flashTime = 100;
int waitTime = 10000;

void setup()
  pinMode(tiltSens, INPUT);
  digitalWrite(tiltSens, HIGH);
  pinMode(signal, OUTPUT);
  digitalWrite(signal, LOW);

void loop()
  tiltStat = digitalRead(tiltSens);
  if (tiltStat != oldtiltStat) {
    counterOne = millis();
  if ((millis() - counterOne) > debounceTime && (millis() - counterOne) < (debounceTime + flashTime) || (millis() + debounceTime + flashTime - counterOne) > waitTime && (millis() + debounceTime + flashTime - counterOne) < (waitTime + flashTime)) {
    digitalWrite(signal, HIGH);     
    digitalWrite(signal, LOW);

  oldtiltStat = tiltStat;


     May I know what is the name of the Tilt sensor you are using?




It is not clear from your post what the sketch is supposed to be doing that it is not. Please clarify a) what you want it to do and b) what it is actually doing.

Note that you are not actually debouncing the sensor input.  You change counterOne every time the sensor input changes. I think you need to do some more research on debouncing. There are plenty of forum posts about it and some code in the playground.

A suggestion: look into using a state machine. It might make coding this a bit easier to understand.  You probably don't need a full table-driven state machine, but having explicit states, and actions seems to be a natural fit for this problem.


Thanks for the replies.
to Josh:
I am using a pretty simple tilt sensor.. I bougth the extended workshopo kit from waterott (http://www.watterott.com/de/Arduino-Extended-Workshop-Kit) and the tilt sensor was included.
I know it´s not very exakt but I´m tryin some basic things - I thought.

to ckiick:
I think I wrote, what I want to achieve. And it does something. What it does not is: ignoring the second turning of the tilt and restart the 10 min counter. But as I wrote, I got stuck. Maybe with the logics. I just dont know, how to get farther.
I´ll do some research to state machine, but I hoped, there would be a simple solution, because I think that the idea, what it shall do is quite simple.


I got bored and wrote this little debounced input class you might find useful:



That big 'if' statement is pretty scary. Although I can't see any mistakes in it, I'm not at all convinced the logic is right.

I suggest you split the logic up a bit, perhaps something like this:

Code: [Select]

// incomplete, untested
tiltStat = digitalRead(tiltSens);
if (tiltStat != oldtiltStat)
    tiltTime = millis();

if (millis() - tiltTime < flashDuration)
  // turn LED on during initial flash
else if (millis() - tiltTime < (flashDuration + waitDuration))
  // turn LED off while waiting until the final flash is due
else if (millis() - tiltTime < (flashDuration + waitDuration + flashDuration))
  // turn LED on during final flash
  // turn LED off after final flash

You'll see I've renamed a few of your variables and constants to make it clearer how I'm using them

Go Up