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!
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.
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.
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.
/* 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;
}
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.
/*
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
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.