"open and close" rotate 180 & stop, rotate to 0 & stop servo project almost done

I would like to hit a momentary switch (a door bell) to “open” turn a servo 180 deg. then hit it again to “close” and return to 0. Everything I can find is for pressure buttons and return the servo back to 0. Would the momentary switch use the analog signal values of 0 to 1023 as well? My plan is to run the serial monitor to see what values the doorbell button is putting out once I get further along in the code.

Here is the closest code to the plan, but I’m a TOTAL noob and thinking it needs some “if then” statements and a stop funtion. e.g. if open then close and stop, if closed then open and stop

Could someone help me please. I’d then like to start playing with the deg of rotation to open and close to learn more, then keep moving on from there. adding a step motor, I have built a blinds open and closing servo mechanism to open and close blinds including mounting brackets. The step motor could roll the blinds up and be my next project :slight_smile:

Thanks for helping me have fun in advance. This arduino nano is such a cool thing. I have been able to get the sweep example running the motor so far on an external batter pack, pretty cool!

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Please use code tags to enclose any code in your post. You can do this by highlighting code, then clicking the # icon above the editor's text window.

As for your project, the very first thing you should do is to look at Nick Gammon's tutorial on switches, at http://www.gammon.com.au/forum/?id=11955

You are right... this Arduino stuff IS fun.

Would the momentary switch use the analog signal values of 0 to 1023 as well?

No. A momentary contact switch is a digital device. It is either pressed or not.

Basic servo/button toggle code:

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}

Got the code inserted, Thanks Lar3ry

I was reading about stopping the servo. If it is not changed to a continuous I assume I do not need myservo.detach(); at the begining and end since I do not see it in there.

will learn how to manually add code and I will add the button and see how that button code works thanks Zoomkat

excited :D

way cool

I changed the values of 160 to 180 and then changed 20 to 1 and got a full rotation. I’ll try 0 instead of 1

Do I change the length of time needed to press the button on the 500 to 250 for example? It seems finicky and requires a long button press.

could I add the myservo.detach(); so that the motor turns off so that someone could still open and close the blinds manually? I noticed to the servo wont let you turn it manually oncethis is going. Seems like turning it off would not waste power as well.

This is what I was thinking

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(180);
      toggle = !toggle;
    }
then 
    }
      myservo.detach();
    }
    
else
    {
      servo.write(0);
      toggle = !toggle;
    }
then
    }
     myservo.detach(); 
    }
   
}
  delay(500);  //delay for debounce
}

You should probably go back to Nick Gammon's tutorial and have a look at Debouncing and Debouncing Without Delay.

The idea would be (in pseudo-code):

loop() {
  read switch, debounce without delay()
  if switch LOW
     if prev switch was HIGH
        call servo function
}

servo function() {
   if servo position is open
      drive servo to closed
   else
      drive servo to closed
}

I’m comparing code after reading the links above about debouncing. I wanted to add an on off switch to my now working (YAHOO and thanks to you guys) monetary switch servo controlled blinds over to zwave switched blinds. However then I go from an “on” or “on” monetary button to a on and off switch of the ground.

Then I have a question about resistor or not later.

Switch code that I cannot figure out comparing to other code that is fully labeled. Where is the 180 deg open limit to adjust to say 160 deg. I see the 0 so does it mean just open and close to full limit. 9=white servo control wire

what about ground wire back from the z wave

#include <Servo.h>

const int button_pin = 2 ;          //[b] assuming this is the zwave line back in[/b]
int led = 13;                              

Servo s1;
int pos = 0; // variable to store the servo position (0-135) // 0to 135??? if this is 0 where is 180 code or is it just full limit of the servo 
int button_value; //variable to store switch position     //do I need to add 180 here? or this is the auto full open full close?

void setup()
{
  s1.attach(9);                       // attaches the servo's control wire on pin 9
  pinMode(led, OUTPUT);    
 
}
                                        //is the rest of this code below just LED or servo position with the 135 and 45? what is sl.write?
void loop()
{
  button_value = digitalRead(button_pin);
  if (button_value==HIGH){
    s1.write(135); 
    digitalWrite(led, HIGH);   // turn the LED on 
    }
  else {
    s1.write(45);
   digitalWrite(led, LOW);    // turn the LED off 
    }
}

okay I could use a little help after another day of reading/ help from others/ trying to learn stuff albeit reverse engineering do to my lack of coding knowledge at this point. So far I have learned the following labeled in the code. Can anyone please help me with the code for sending the ground from GRN to pin 7 to complete my circuit thus leading to run the second part of the code

and secondly please let me know what you think of the combination of this circuit/the code from zoomkat that worked and was set up for the monetary switch combined to be used with an on off is okay vs adding a resistor. I’d like to void Gammon’s example having just a floating input “Beginners tend to wire their switch like this (not recommended):” But I thougt maybe that since I am using a separate power source then this would be ok.

			//Directions are using the same pins from zoomkat servo button toggle test code so that a zwave switch could easily be added to test new code
		        //run 4.8 to 6v ground wire spliced to servo and ground on digital side of nano,
			// and a 3rd splice to z wave isolated switch to switch the ground for the nano to read
			//then the other side of the z-wave isolated switch (same ground) back to pin D7 on nano
			//the 4.8 to 6v positive/red wire to servo motor
			//run servo's white control wire to pin D5
			// run separate 12v to VIN pin on nano and the same 12v source's ground to the GRN pin next to VIN

                        // Shout out and Thanks a ton to Zoomkat for the first code for the momentary switch. I have learned a ton!
                        //Thanks Lar3ry for showing me things to learn to better my skills, I have learned a ton!

//zoomkat servo button toggle test 4-28-2012  combined from code suggested my reallifeonhold source unknown

#include <Servo.h>

const int button_pin = 7 ;         			//The issue I see is this takes voltage from the nano and NOT completing my circuit which would be to run ground from digital 
							//side GRN to pin 7, in short this just uses the nano to send 5V to pin D7 what code is needed?


int led = 13;                              		// use LED aka pin 13 for visual confirmation as to which state the Arduino was in.

Servo s1;
int pos = 0; 						// variable to store the servo start position deg
int button_value = 180; 				//variable to store switch position e.g. 180 turn 

void setup()
{
  s1.attach(5); 					// attaches the servo's control wire on pin 5 White wire
  pinMode(led, OUTPUT);    
 
}
                                        		// code for z wave switch recognition 
void loop()
{
  button_value = digitalRead(button_pin);
  if (button_value==HIGH){				//do this
    s1.write(135); 					//Value to open servo to on/off of switch
    digitalWrite(led, HIGH);   				// turn the LED on LED corresponding to switch
    }
  else {						//or else do this
    s1.write(45);					//Value to open servo to on/off of switch
   digitalWrite(led, LOW);   				// turn the LED of corresponding to switch
    }
}

Can anyone please help me with the code for sending the ground from GRN to pin 7 to complete my circuit thus leading to run the second part of the code

Writing pin 7 LOW connects it to ground.

and secondly please let me know what you think of the

code. Not much, All that useless indenting is sill. The piss poor indenting throughout the code makes it very difficult to see the logic.

There is no excuse for that, when Tools + Auto Format can fix that with two mouse clicks or two key presses.

What do you think of this “NEW code” to get ground from the zwave isolated switch to ground of the arduino. I added a debounce delay as well as suggested by Gammon’s tutorial. Am I way off?

			//Directions are using the same pins from zoomkat servo button toggle test code so that a zwave switch could easily be added to test new code
		        //run 4.8 to 6v ground wire spliced to servo and ground on digital side of nano,
			// and a 3rd splice to z wave isolated switch to switch the ground for the nano to read
			//then the other side of the z-wave isolated switch (same ground) back to pin D7 on nano
			//the 4.8 to 6v positive/red wire to servo motor
			//run servo's white control wire to pin D5
			// run separate 12v to VIN pin on nano and the same 12v source's ground to the GRN pin next to VIN

                        // Shout out and Thanks a ton to Zoomkat for the first code for the momentary switch. I have learned a ton!
                        //Thanks Lar3ry for showing me things to learn to better my skills, I have learned a ton!

//zoomkat servo button toggle test 4-28-2012  combined from code suggested my reallifeonhold source unknown

#include <Servo.h>


//NEW code to send ground through pin 7 when using a secondary power source for the servo

int switchPin = 7;   //  switch on 7, other side connected to ground

boolean running = false;

void setup()
{
  pinMode(pin, OUTPUT);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, low);     
}

void loop()
{
  if (digitalRead(switchPin) == LOW)                        // switch is pressed 
  { 
    delay(100);                        // delay to debounce switch
  }
}

//ORIGINAL code

int led = 13;                              		             // use LED aka pin 13 for visual confirmation as to which state the Arduino was in.

Servo s1;
int pos = 0; 						             // variable to store the servo start position deg
int button_value = 180; 		            //variable to store switch position e.g. 180 turn 

void setup()
{
  s1.attach(5); 			               		// attaches the servo's control wire on pin 5 White wire
  pinMode(led, OUTPUT);    
 
}
                                        		                                 // code for z wave switch recognition 
void loop()
{
buton_value = digitalRead(button_pin);
if (button_value==HIGH){				        //do this
 s1.write(135); 					                        //Value to open servo to on/off of switch
 digitalWrite(led, HIGH);   				        // turn the LED on LED corresponding to switch
    }
 else {                                 						//or else do this
 s1.write(45);			                        		//Value to open servo to on/off of switch
digitalWrite(led, LOW);   			         	// turn the LED of corresponding to switch
}
}
void loop()
{
  if (digitalRead(switchPin) == LOW)                        // switch is pressed 
  { 
    delay(100);                        // delay to debounce switch
  }
}

Do nothing for 100 milliseconds if the switch is pressed. Do nothing for a much shorter period of time if the switch is not pressed.

What is the purpose of testing the switch pin?

  digitalWrite(switchPin, low);

Did that even compile?

PaulS:
Did that even compile?

I didn’t try to compile it yet. I’m trying to understand the code and wanted opinions. I’m a total noob and trying to learn, but adruino’s website descriptions seem limited. I think I have the debounce coded better now after reading a little more. Do I need boonlean for the if then? The adruino page was vague about boonlean.

                      //Directions: 
			// We are using the same pins from zoomkat servo button toggle test code so that a isolated zwave switch could easily be added to test new code
		        //run 4.8 to 6v ground wire spliced to servo and ground on digital side of nano,
			// and a 3rd splice to z wave isolated switch to switch the ground for the nano to read
			//then the other side of the z-wave isolated switch (same ground) back to pin D7 on nano
			//the 4.8 to 6v positive/red wire to servo motor
			//run servo's white control wire to pin D5
			// run separate 12v to VIN pin on nano and the same 12v source's ground to the GRN pin next to VIN

                        // Shout out and Thanks a ton to Zoomkat for the first code for the momentary switch. I have learned a ton!
                        //Thanks Lar3ry for showing me things to learn to better my skills, I have learned a ton!


//zoomkat servo button toggle test 4-28-2012  combined from code suggested my reallifeonhold source unknown

#include <Servo.h>

int button = 5; 					//button pin, connects button through pin 7 to ground to move servo
int press = 0;						//Set servo at zero to start
Servo servo;
boolean toggle = true;					//Boonlean for if/then statements
const unsigned long debounceTime = 10;  		//debounce milliseconds

void setup()
{
inMode(button, INPUT); 					//arduino monitor pin state
servo.attach(7); 					//pin for servo control signal
digitalWrite(5, LOW); 					//enable pullups to make pin lOW
}

int led = 13;                              		//use LED aka pin 13 for visual confirmation as to which state the Arduino is in.

Servo s1;
int pos = 0; 						//variable to store the servo start position deg
int button_value = 180; 				//variable to store switch position e.g. 180 turn 

void setup()
{
s1.attach(5); 						//attaches the servo's control wire on pin 5 White wire
pinMode(led, OUTPUT);    
 
}
                                        		//code for z wave switch recognition 
void loop	
{
delay (debounceTime);  					//debounce
button_value = digitalRead(button_pin);
if (button_value==HIGH){				//do this
s1.write(135); 						//Value to open servo to on/off of switch
digitalWrite(led, HIGH);   				//turn the LED on LED corresponding to switch
}
else 
{							//or else do this
s1.write(45);						//Value to open servo to on/off of switch
digitalWrite(led, LOW);   				//turn the LED of corresponding to switch
}
}

The adruino page was vague about boonlean.

I'm pretty sure the Arduino (capital A) doesn't even mention "boonlean". It does mention boolean. I know that if does not talk about if/then statements, either. if/else does get mentioned.

thanks for the help