Pins holding HIGH

New to the forums, hello all :slight_smile: Hate to just jump in asking questions but…imma jump in and ask a question ;p

Building a simple project that just needs to turn a servo, or not, depending on whether a switch is active or not. I use the switch to ‘lock’ out the servo. It activates at the push of a button. Issue at hand is:

Switch off, reads LOW, hit button, Servo turns, no problem.
then…
Switch on, reads HIGH, hit button, servo does NOT turn, no problem.
then the problem…
Switch back to off, reads HIGH???, hit button, no servo, big problem.

For some reason the pin holds that HIGH value for about 20 or so seconds before finally coming back down to LOW and allow the servo again. This is actually my first project so I might be in over my head, but I thought it would be pretty simplistic.

I have included the fritz and the code, help plox, very lost :confused:

DoorLock System
			Knobless Entry Modification

		DLS-Mod 1
		Jason Ruschmann
		Infinity Pixel
		Jason.Ruschmann@gmail.com

		10.31.2015 rev.1
		11.01.2015 rev.2
                11.02.2015 rev.3

	The goal is create a sytem which can easily be installed as
		a replacement for a door knob. Starting Module will be a
		simple indoor system requiring only a button push to open
		the door, provided it is not locked by the included switch
		lock system. The switch out will provide the lock-door
		privacy while maintaining easy access from both sides.
*/


// Includes
#include <Servo.h>														// Servo Library


// Create Global Defines
// Led Assignment
int ledRed = 12;														// Red LED indicates LOCK Status
int ledGreen = 7;														// Green LED indicates OPEN Status
int ledBlue = 8;														// Blue LED indicates System Ready

// Switch Level Assignment
int switchLock = 4;														// Pin Check to set status to LOCK

// Servo Set Assignment
Servo servoCtrl;														// Assign Servo to Control Object

// Button Assignment
int btnOpen = 2;														// Button press will open door (if it is unlocked)


// Create Setup Required
void setup()
{
	// Set Pin Modes for various Nodes
	// LED's
	pinMode(ledRed, OUTPUT);
	pinMode(ledGreen, OUTPUT);
	pinMode(ledBlue, OUTPUT);

	// Switch Pins
	pinMode(switchLock, INPUT);
		
	// Servo Power
	servoCtrl.attach(3);												// Control Point for Servo Motor Pin Number

	// Button Read
	pinMode(btnOpen, INPUT);
}



/*    Mod 1 -
		When checked upon Button Press the pin read will tell the program whether the state 
			is LOCKED or OPEN. If Status is set to LOCKED, flash RED LED a number of times 
			and return to begining of loop. If Status is set to OPEN, turn on GREEN LED and
			engage Servo to open door. Wait for given amount of time and return Servo to home 
			position and turn off GREEN LED. Return to beginning of loop. Wait for Button Press 
			to check, run, and repeat.
*/

// Program to Run
void loop()
{
// Local Defines
	// Settings
	int flash = 0;														// Flash counter to track number of LED cycles (initialized at 0)
	int cycle = 150;													// Delay time for LED flash cycle
	int hold = 15000;													// Hold door latch open for this ammount of time

	// State Containers
	int buttonState = digitalRead(btnOpen);								// Holds current state of button
	int lockState = digitalRead(switchLock);							// Holds current state of LOCK position

	// Servo Control Set
	int position;														// Hold Position set for Servo
	
	// What to do when Button is Pressed
	// - LOCKED
	if (buttonState == LOW && lockState == HIGH) {						// If TRUE Switch is set to LOCK State
		digitalWrite(ledBlue, LOW);										// Turn LED off to indicate Busy Status
		
		while (flash < 5) {												// Lock state confirmed, Flash LED 5 times
			digitalWrite(ledRed, HIGH);									// Set LED to ON
			delay(cycle);												// wait
			digitalWrite(ledRed, LOW);									// Set LED to OFF
			delay(cycle);												// wait
			flash = flash + 1;											// Add 1 to Flash Counter
		}

		// Reset to Ready Status
		flash = 0;														// Loop Concludes, Reset Flash Counter to 0
		digitalWrite(ledBlue, HIGH);									// Set LED to ON indicating Ready Status
	}

	// -OPEN
	else if (buttonState == LOW && lockState == LOW) {					// If TRUE Switch is set to OPEN State
		digitalWrite(ledBlue, LOW);										// Turn LED off to indicate Busy Status
		digitalWrite(ledGreen, HIGH);									// Set LED ON indicated OPEN Status

		// Release Latch
		servoCtrl.write(110);											// Turn Servo to given position to open Latch
		delay(hold);													// wait
		servoCtrl.write(0);												// Turn Servo back to starting postion to close Latch

		// Reset to Ready Status
		digitalWrite(ledGreen, LOW);									// Set LED OFF indicating CLOSED Status
		digitalWrite(ledBlue, HIGH);									// Set LED ON indicating Ready Status
	}

	// -READY
	else
		digitalWrite(ledBlue, HIGH);									// Set LED ON indication System on and in Ready Status
}

doorLock_mod1.ino (5.1 KB)

Yo may have wired your switch wrong. (Of course, I don’t have the spec sheet on the switch.) Is it the slide switch or the pushutton?

Do you have a multimeter to check the switch terminals or the check the Arduino’s digital input voltage when you activate/deactivate the switch? Or, you can try the Or, you can try the [u]Button Example[/u] to check your button & switch operation.

With a SPDT or DPDT switch, the center terminal is usually the “switched” terminal(s). So that center terminal should go to the Arduino input.

But normally, you just use 2 switch terminals with a pull-up or pull-down resistor. Then, it doesn’t matter which terminal goes where because you just want to “make” or “break” a connection. If you use the [u]built-in pull-up resistors[/u] you don’t need any external resistors on the input.

Its a toggle style slide switch. I double checked from this small tutorial on the wiring: http://www.connectedly.com/using-buttons-and-switches-arduino

I did indeed miswire the switch, but strangely, the circuit didn't seem to care. It acted the same way.

I tried removing the switch completely and tried using a jumper wire from power straight to the input since a switch basically does the same. The circuit acted exactly the same. It held the high, I checked with a multimeter and all the measures check out correctly.

If I read your given link correctly, am I to understand that I can basically set the throughput value of a pin via code? Rather, tell it that when there is nothing there and/or a switch is switched 'OFF' the input should default to the value I set? Can this be used as a hardware debug method?

I am gonna mess around with my code some more, I will stick to using a jumper wire until I can get that to at least function correctly.

Thank you for the help, I will update tomorrow :)

pinMode(btnOpen, INPUT);
pinMode(switchLock, INPUT);

Change these as DVDdoug suggests so as to enable the internal pullup resistors.

pinMode(btnOpen, INPUT_PULLUP);
pinMode(switchLock, INPUT_PULLUP);

This will invert the logic, and the pin will read 1 when the switch is open and 0 when it is closed to ground. The switches should be connected between the input pin and ground. They should switch the pin to ground.

tried using a jumper wire from power straight to the input

For debugging, you can use your jumper to ground instead of power and connect it to the input pins (with the internal pullup resistors enabled) to model the switch.

Your circuit is not only miss wired it is wrong. Read this http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

cattledog, I changed those lines and it worked fantastically :slight_smile: Finally got it running!

BUT THEN…I may have complicated it. Since this is a door lock style system…I need two buttons, inside and outside. I thought that it should be an easy modification. Wire the button like i did the first, assign pins as such, and add a simple OR operator to answer for either one button OR the other…

The LOCK works for both sides no issue. but the OPEN only works for the inside, the outside remains locked. I can’t seem to find the lapse in my logic :confused:

// Includes
#include <Servo.h> // Servo Library


// Create Global Defines
// Led Assignment
int ledRed = 12; // Red LED indicates LOCK Status
int ledGreen = 7; // Green LED indicates OPEN Status
int ledBlue = 8; // Blue LED indicates System Ready

// Switch Level Assignment
int switchLock = 4; // Pin Check to set status to LOCK

// Servo Set Assignment
Servo servoCtrl; // Assign Servo to Control Object

// Button Assignment
int btnInside = 5; // Inside Button press will open door (if it is unlocked)
int btnOutside = 6; // Outside Button press will open door (if it is unlocked)

// Create Setup Required
void setup()
{
 // Set Pin Modes for various Nodes
 // LED's
 pinMode(ledRed, OUTPUT);
 pinMode(ledGreen, OUTPUT);
 pinMode(ledBlue, OUTPUT);

 // Switch Pins
 pinMode(switchLock, INPUT_PULLUP);
 
 // Servo Power
 servoCtrl.attach(3); // Control Point for Servo Motor Pin Number

 // Button Read
 pinMode(btnInside, INPUT_PULLUP);
 pinMode(btnOutside, INPUT_PULLUP);
}



/*    Mod 1 -
 When checked upon Button Press the pin read will tell the program whether the state 
 is LOCKED or OPEN. If Status is set to LOCKED, flash RED LED a number of times 
 and return to begining of loop. If Status is set to OPEN, turn on GREEN LED and
 engage Servo to open door. Wait for given amount of time and return Servo to home 
 position and turn off GREEN LED. Return to beginning of loop. Wait for Button Press 
 to check, run, and repeat.
*/

// Program to Run
void loop()
{
// Local Defines
 // Settings
 int flash = 0; // Flash counter to track number of LED cycles (initialized at 0)
 int cycle = 150; // Delay time for LED flash cycle
 int hold = 15000; // Hold door latch open for this ammount of time

 // State Containers
 int btnInState = digitalRead(btnInside); // Holds current state of Inside button
 int btnOutState = digitalRead(btnOutside); // Holds current state of Outside button
 int lockState = digitalRead(switchLock); // Holds current state of LOCK position

 // Servo Control Set
 int position; // Hold Position set for Servo
 
 // What to do when Button is Pressed
 // - LOCKED
 if (btnInState == LOW || btnOutState == LOW && lockState == HIGH) { // If TRUE Switch is set to LOCK State
 digitalWrite(ledBlue, LOW); // Turn LED off to indicate Busy Status
 
 while (flash < 5) { // Lock state confirmed, Flash LED 5 times
 digitalWrite(ledRed, HIGH); // Set LED to ON
 delay(cycle); // wait
 digitalWrite(ledRed, LOW); // Set LED to OFF
 delay(cycle); // wait
 flash = flash + 1; // Add 1 to Flash Counter
 }

 // Reset to Ready Status
 flash = 0; // Loop Concludes, Reset Flash Counter to 0
 digitalWrite(ledBlue, HIGH); // Set LED to ON indicating Ready Status
 }

 // -OPEN
 else if (btnInState == LOW || btnOutState == LOW && lockState == LOW) { // If TRUE Switch is set to OPEN State
 digitalWrite(ledBlue, LOW); // Turn LED off to indicate Busy Status
 digitalWrite(ledGreen, HIGH); // Set LED ON indicated OPEN Status

 // Release Latch
 servoCtrl.write(110); // Turn Servo to given position to open Latch
 delay(hold); // wait
 servoCtrl.write(0); // Turn Servo back to starting postion to close Latch

 // Reset to Ready Status
 digitalWrite(ledGreen, LOW); // Set LED OFF indicating CLOSED Status
 digitalWrite(ledBlue, HIGH); // Set LED ON indicating Ready Status
 }

 // -READY
 else
 digitalWrite(ledBlue, HIGH); // Set LED ON indication System on and in Ready Status
}
else if (btnInState == LOW || btnOutState == LOW && lockState == LOW)

On a line like this I always err on the side of too many parenthesis even when they may not be entirely necessary. That way, even if I get my order of operators messed up, the code still does what I want.

Do you mean:

else if ((btnInState == LOW || btnOutState == LOW) && lockState == LOW)

or

else if (btnInState == LOW || (btnOutState == LOW && lockState == LOW))

Delta_G: HOLY CRAP! Well now I feel like a noob for forgetting my 101's ;p That absolutely did it :)

Thank you all for your help, this prototype is running just as I had hoped. You were all a huge help and I appreciate it :) Hopefully further project won't be so bothersome know that I know some of the little hang-ups. :)