I have a simple LED connected to a switch and my program has a conditional statement that basically says if that switch is turned on, digitalWrite(ledpin, HIGH), else, digitalWrite(ledpin, LOW). When I turn the switch off, the LED takes about 2 seconds to turn off, slowly dimming out. It is sort of like a bleeding problem but I do not get why. Any help is appreciated.
Hi deeg92
Please post a diagram of how your hardware is connected. A photo of a hand drawn diagram is ok.
Regards
Ray
Show the whole code.
But - read the rules first.
I am not able to draw it out at this moment but I do have the code:
// include the library code:
// these constants describe the pins. They won't change:
int StartPin = 3; // the number of the pushbutton pin = > START BUTTON
int checkAir = 5; // the number of the pin for checking air
int checkPres = 2; // the number of the pin for checking if a cable is presence(reads switch underneath cable placement)
int Press1 = A1 ; // the number of the pin for getting voltage at press and validating a pass/fail
int Release1 = A2 ; // the number of the pin for getting voltage after release and validating a pass/fail
int Vref = A3;
//Represents various states
int SState = 0; // variable for reading the START pushbutton status
int RState = 0; // variable for reading the RESET status
int airState = 0; // variable for reading the AIR status
int presState = 0; // variable for reading the PRESENCE of a cable status
int VState = 0;
boolean flag = true;
void setup()
{
Serial.begin(9600);
// initialize the pins as an inputs/outputs:
pinMode(StartPin, INPUT);
pinMode(checkAir, INPUT);
pinMode(checkPres, INPUT);
pinMode(Press1, INPUT);
pinMode(Release1, INPUT);
pinMode(Vref, INPUT);
pinMode(4, OUTPUT); // Air Switch Ok LED
pinMode(6, OUTPUT); // Presence Ok LED
pinMode(8, OUTPUT); // Ready light LED
pinMode(10, OUTPUT); // Pass light LED
pinMode(12, OUTPUT); // Fail light LED
pinMode(7, OUTPUT); // Fire Solenoid w/ Transistor
}
void loop() {
SState = 0;
digitalWrite(6, LOW);
presState = digitalRead(checkPres);
airState = digitalRead(checkAir);
SState = digitalRead(StartPin);
if(presState == 1){
digitalWrite(6, HIGH);
}
else{
digitalWrite(6, LOW);
}
if(airState == 1){
digitalWrite(4, HIGH);
}
else{
digitalWrite(4, LOW);
}
// while(presState == 1 && airState == 1){
// digitalWrite(8, HIGH);
// }
//}//end of while
}
Please edit your previous post and put the code inside code tags (the </> button on the toolbar above the reply window).
Remember that loop() executes thousands of times per second. Every time you set pin 6 to HIGH with this code ...
if (presState == 1) {
digitalWrite(6, HIGH);
}
else {
digitalWrite(6, LOW);
}
... you set it back to LOW the next time round loop() with this code:
digitalWrite(6, LOW);
The wire going from the pin you're reading to the switch. Is anything else connected to it?
With neither a pullup or pulldown resistor, when switch is open (not connected), the wire will be electrically connected to nothing, and since it's an input, it's floating (and you can make it read high (or low) . If closing the switch connects it to ground, you need a pullup to make sure it's at Vcc when the button isn't pressed - this can be done with pinMode(pin,INPUT_PULLUP) instead of using an external resistor. If closing the switch connects it to Vcc, you need a pulldown to make sure it's at ground when the button isn't pressed (this can only be done with an external resistor, there's no internal pulldown). The internal pullups are around 25-30k ohm, external pullups can be basically anything from like 1k to 100k, with people generally defaulting 10k.
So what would you suggest is an easier way to write that code so its conditional soley based on the digitalRead of that pin to see if it is a 0 or 1??
deeg92:
So what would you suggest is an easier way to write that code so its conditional soley based on the digitalRead of that pin to see if it is a 0 or 1??
Is this in response to reply #4? If so, start by deleting this statement in loop() and tell us if that has solved your problem ...
void loop()
{
SState = 0;
digitalWrite(6, LOW); // DELETE THIS STATEMENT
etc
I am a dumbass and sent you that code with that statement at the beginning for no reason. I put that in there to troubleshoot what was going on with the LEDs. I fixed the problem with help of Reply #5 and set those switches to tie to ground instead of Vcc. I apologize for misleading you with code. Thanks a lot for both of your help.
Hackscribble:
Please edit your previous post and put the code inside code tags (the </> button on the toolbar above the reply window).
Sigh!
Now fixed, but I did say "read the rules first", didn't I?