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
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?
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?
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?
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.
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;
}
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?
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?
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.
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.