Tilt Switch changes state once, but doesn't return...

Hey guys! I have a tilt switch on a project and i THOUGHT it would be easy enough to implement- ive for an adafruit feather HUZZAH but it isnt working as i thought. I used the example code from the adafruit website:

int inPin = 16;         // the number of the input pin
int outPin = 2;       // the number of the output pin
 
int LEDstate = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
 
// the following variables are long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long timer = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers
 
void setup()
{
  Serial.begin(9600);
  pinMode(inPin, INPUT);
  digitalWrite(inPin, HIGH);   // turn on the built in pull-up resistor
  pinMode(outPin, OUTPUT);
}
 
void loop()
{
  int switchstate;
 
  reading = digitalRead(inPin);
 
  // If the switch changed, due to bounce or pressing...
  if (reading != previous) {
    // reset the debouncing timer
    timer = millis();
  } 
 
  if ((millis() - timer) > debounce) {
     // whatever the switch is at, its been there for a long time
     // so lets settle on it!
     switchstate = reading;
 
     // Now invert the output on the pin13 LED
    if (switchstate == HIGH)
      Serial.println("low");
    else
      Serial.println("high");
      delay(100);
       previous = reading;
  }
  
 
  // Save the last reading so we keep a running tally
  previous = reading;
}

now when i look at the serial monitor it prints "low" as expected. when i tilt it, it begins to print "high", as expected. But once its returned to its original position it just continues to print "high" and i have no idea why.

I was wondering if anyone here had any experience that could help!

Hi,
Try it without all the debounce stuff.

Tom... :slight_smile:

time =r millis();  // ???
. . .
digitalWrite(outPin, LEDstate);  // LEDstate - missing declaration

or try compiling it first.

What has LedState got to do with anything?

How does this even compile

Beaten to it!

6v6gt:

time =r millis();  // ???

. . .
digitalWrite(outPin, LEDstate);  // LEDstate - missing declaration




or try compiling it first.

Hey! It was a copy paste error as i posted this before going to bed from another computer that wasnt my main one! i've fixed it in the code. Also the actual code or teh project im using the switch for is here: https://pastebin.com/0AA7NZvB

I found that the INPUT_PULLUP switch stuff i had there wasnt working so i decided to go for a tutorial example to see what i was missing.

pmagowan:
What has LedState got to do with anything?

How does this even compile

Beaten to it!

hey! this was a quick copy paste error- I posted this from a different PC than where i was compiling. originally I wasnt using the tilt switch on this project- I was using ti for a completely different project found over here: https://pastebin.com/0AA7NZvB

I found that the INPUT_PULLUP switch stuff i had there wasnt working so went online to find a tutorial that might have more of a chance of working. it didnt.

Hi,
How do you have the switch wired.
With pull_up ON, you should have the switch wired between the input pin and gnd.

Have you tried simple no debounce code to check your input?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
Try it without all the debounce stuff.

Tom... :slight_smile:

I have! The original code I was using is found here: https://pastebin.com/0AA7NZvB

I simply set it to input_pullup and set the digital write to tell me when it was tilted.

Same thing happened. It only shows me the first time it's tilted but never returns to previous state

It's wired with 3.3v, and pin 16 on one side, ground on the other

See if this makes a difference:

int inPin = 16;         // the number of the input pin
int outPin = 2;       // the number of the output pin
 
int LEDstate = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
 
// the following variables are long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long timer = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers
 
void setup()
{
  Serial.begin(9600);
  pinMode(inPin, INPUT);
  digitalWrite(inPin, HIGH);   // turn on the built in pull-up resistor
  pinMode(outPin, OUTPUT);
}
 
void loop()
{
  int switchstate;
 
  reading = digitalRead(inPin);
 
  // If the switch changed, due to bounce or pressing...
  if (reading != previous) {
    // reset the debouncing timer
    timer = millis();
  }
 
  if ((millis() - timer) > debounce) {
     // whatever the switch is at, its been there for a long time
     // so lets settle on it!
     switchstate = reading;
 
     // Now invert the output on the pin13 LED
    if (switchstate == HIGH)
      Serial.println("low");
      previous = reading; // <<<<<<<<< 
    else
      Serial.println("high");
      delay(100);
       previous = reading;
  }
 
 
  // Save the last reading so we keep a running tally
  // previous = reading; <<<<<<<<
}

It's wired with 3.3v, and pin 16 on one side, ground on the other

That will cause a short circuit when the switch is closed, possibly damaging the 3.3V regulator on the Huzzah, and probably causing it to reset until the switch is opened again.

celesmeh:
It's wired with 3.3v, and pin 16 on one side, ground on the other

Is your switch wired like any of these examples?

celesmeh:
It's wired with 3.3v, and pin 16 on one side, ground on the other

Remove the 3V3 connection, just have the switch between Pin16 and gnd.
Tom... :slight_smile:

dougp:
Is your switch wired like any of these examples?

I have it wired like the s2 on there.

TomGeorge:
Remove the 3V3 connection, just have the switch between Pin16 and gnd.
Tom... :slight_smile:

Its been removed! it still does the same thing though.

JCA34F:
See if this makes a difference:

int inPin = 16;         // the number of the input pin

int outPin = 2;       // the number of the output pin

int LEDstate = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the following variables are long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long timer = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers

void setup()
{
 Serial.begin(9600);
 pinMode(inPin, INPUT);
 digitalWrite(inPin, HIGH);   // turn on the built in pull-up resistor
 pinMode(outPin, OUTPUT);
}

void loop()
{
 int switchstate;

reading = digitalRead(inPin);

// If the switch changed, due to bounce or pressing...
 if (reading != previous) {
   // reset the debouncing timer
   timer = millis();
 }

if ((millis() - timer) > debounce) {
    // whatever the switch is at, its been there for a long time
    // so lets settle on it!
    switchstate = reading;

// Now invert the output on the pin13 LED
   if (switchstate == HIGH)
     Serial.println("low");
     previous = reading; // <<<<<<<<<
   else
     Serial.println("high");
     delay(100);
      previous = reading;
 }

// Save the last reading so we keep a running tally
 // previous = reading; <<<<<<<<
}

with this is wont output anything to the serial monitor until i flip the tilt switch- it then begins to output high to the monitor and does not stop.

That code won't compile without errors. Neither does that posted in reply #8.

the one i'm uploading right now withotu error is:

/* Better Debouncer
 * 
 * This debouncing circuit is more rugged, and will work with tilt switches!
 *
 * http://www.ladyada.net/learn/sensor/tilt.html
 */
 
int inPin = 13;         // the number of the input pin
int outPin = 2;       // the number of the output pin
 
int LEDstate = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
 
// the following variables are long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long timer = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers
 
void setup()
{
  Serial.begin(9600);
  pinMode(inPin, INPUT);
  digitalWrite(inPin, HIGH);   // turn on the built in pull-up resistor
  pinMode(outPin, OUTPUT);
}
 
void loop()
{
  int switchstate;
 
  reading = digitalRead(inPin);
 
  // If the switch changed, due to bounce or pressing...
  if (reading != previous) {
    // reset the debouncing timer
    timer = millis();
  } 
 
  if ((millis() - timer) > debounce) {
     // whatever the switch is at, its been there for a long time
     // so lets settle on it!
     switchstate = reading;
 
     // Now invert the output on the pin13 LED
    if (switchstate == HIGH)
      Serial.println("low");
    else
      Serial.println("high");
      delay(100);
       previous = reading;
  }
  digitalWrite(inPin, HIGH);
 
  // Save the last reading so we keep a running tally
  previous = reading;
}

ADDED INFO:

Tried it on an Adafruit Metro and an elegoo uno- same issue.
also even if i disconnect the sensor entirerly it continues tro write whatever output it switched to.

EDIT: Works perfectly on an adafruit metro using the arduino button tutrial. somehting MUST be funky with code/wiring.

Maybe it's time to post a photo. Well lit and focused, so we can see where every wire is connected to.

So i got this code and wiring to work:

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 16;     // the number of the pushbutton pin
const int ledPin =  2;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    Serial.println("high");
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    Serial.println("low");
    digitalWrite(ledPin, LOW);
  }
}

i am using an external resistor- but it works fine :confused:

showing the wiring is hard- since this project has... weird wiring... its for a project htat has a cyberpunk style so some of the wire wraps around weird- but i can make a wiring diagram.