Project 8 - Tilt Switch Appears To Have No Effect

Hello,

I am working on Project 8, I have checked other threads but I have not been able to find a solution to my problem.

I have double checked my circuit and my code with the project book, it seems to be all consistent. I have extended the leads of the tilt switch to make sure it was connected and I have also tried the circuit with a push switch to see if this was the problem.

I tired to attach photos of my circuit, tilt switch, and code but the files were too big - they can be found here: Imgur: The magic of the Internet

Any suggestions would be very much appreciated.

Thank you.

Surely you can write a tiny program to test the switch! Did you check it with an ohmmeter? Is the problem the switch is always closed, or is it always open?

Paul

Hi Paul,

I do not think it is a problem with the switch as I have switched it out with the back up tilt switch and a push switch - do you think it could be something wrong with the circuit or code itself?

Josh

joshm90:
Hi Paul,

I do not think it is a problem with the switch as I have switched it out with the back up tilt switch and a push switch - do you think it could be something wrong with the circuit or code itself?

Josh

And the results of each were WHAT?

Paul

read ' how to use this forum' then post your sketch.
read about code tags and please use them.

the timing seems a bit odd.

void loop() {
if(current time....... {
previous time = current time
} // end of IF
digitalWrite (led....
/} // end of loop ????

then more stuff...

Hi Paul,

The results of each of those were the same... all LEDs remained on and the switch did not change that.

joshm90:
Hi Paul,

The results of each of those were the same... all LEDs remained on and the switch did not change that.

Thank you! Only you can see the results of changes you make.

Paul

No problem - I thought it was implied that I had the same outcome trying the other two switches, otherwise I would've known what was wrong!

I will read the rules and repost my code, but this is why I came to the forum, my initial trouble shooting of checking if the switches were malfunctioning and double checking the code and circuit with the project book did not yield any results - I was hoping for a second pair of eyes.

Better than a second pair of eyes is to learn to debug your code line-by-line using the Serial.print() to show variable values and the actual result of any "if" conditions.

Paul

"I was hoping for a second pair of eyes."..

and you'll get it..

but we are not going to jump through hoops.. or try to read non-formatted code...etc..

That is why you are getting those requests.. (help us, help you) :slight_smile:

No problem! Here's my code in proper format.

const int switchPin = 8;
unsigned long previousTime = 0;
int switchState = 0;
int prevSwitchState = 0; //compare switch's position from one loop to next
int led = 2; //count which LED is the next to be turned on, start with pin 2
long interval = 10000; //interval between  each LED turning on (10 seconds)

void setup() {
 for(int x = 2; x<8; x++){
   pinMode(x, OUTPUT);
 }

 pinMode(switchPin, INPUT);
}

void loop() {
 unsigned long currentTime = millis(); //store amount of time Arduino is on

 if(currentTime - previousTime > interval){ //check if enough time has passed to turn on LED
   previousTime = currentTime; //if more than 10 seconds have passed, save previousTime as currentTime
 }

 //once previous time is set, turn on LED and increment led variable
 digitalWrite(led, HIGH);
 led++; 
/ }

 //after checking time check if switch state has changed - read value of switch 
 switchState = digitalRead(switchPin);

 //if switch is in different state than previously turn off LEDs and return led to first pin
 if(switchState != prevSwitchState){
   for(int x = 2; x<8;x++){
     digitalWrite(x,LOW);  
   }
  led = 2;
  previousTime=currentTime;
 }
 //save switch state in prevSwitchState so value can be compared for next loop
 prevSwitchState = switchState;
}

Do you have "pullup" or "pulldown" resistors on this pin?

pinMode(switchPin, INPUT);

Otherwise it is floating.

Paul

PS.
Also looks like you are unconditionally saving the switch state, not just when it changes.

//save switch state in prevSwitchState so value can be compared for next loop
prevSwitchState = switchState;

Hi Paul,

I have pull down resistor on this pin. I attached a photo of my circuit in my first post - I have connected the lead of the switch to a 10-kilohm resistor to ground and connected the junction where they meet to digital pin 8 as the project book lays out and set the pin it is connected to as an input pin.

The unconditionally saving of the switch state is also straight from the project book, it states "at the end of the loop(), save the switch state in prevSwitchState so you can compare it to the value you get for switchState in the next loop()" is the book incorrect here?

Thank you,

Josh

long interval = 10000; //interval between  each LED turning on (10 seconds)

This code does not do that

if(currentTime - previousTime > interval){ //check if enough time has passed to turn on LED
   previousTime = currentTime; //if more than 10 seconds have passed, save previousTime as currentTime
 }

 //once previous time is set, turn on LED and increment led variable
 digitalWrite(led, HIGH);
 led++;

The digitalWrite() needs to be inside the conditional block. It doesn't effect the performance of your code, but since you have leds on pins 2-7, there's really no need to let led++ keep incrementing the value when the switch is not pressed and all the leds are on.

if(switchState != prevSwitchState){

This will be true when the switch is both pressed and released. Is that what you want?

Hi Cattledog.

I just realized I was missing the closing bracket of that if loop - that did fix the problem of having the LEDs turn on one at a time. Thank you.

What I am struggling with now is getting the LEDs to restart once I change the condition of the switch. The intention of that if statement is to restart the LEDs once the switch is open and closed again - the idea is to mimic the turning over of an hour glass to restart the LEDs. I want it to be true when it is both pressed and released, I just want it to be true when the condition of the switch changes. This does not seem to be happening.

I used serial.print to see the output here and it looks to always be 0, regardless of what I am doing to the switch. I tested the switch with a simple circuit of just an LED and resistor so I know it is working.

Thank you again,

Josh

It turns out besides that besides the bracket, the tilt switch with the new arduino kits needs to have all four pins plugged it - everything else was fine. Thank you for your help.