I have found a sketch that is exactlly what I want for my project, but I want to modify it.
The sketch does the following: the push-button is pushed once, the servo goes from 0 to 180 degrees and back to 0.
I want to add a pause when the servoe reaches 180. So it goes from 0 to 180, pauses for X seconds, and goes back to 0 without further input.
I have tried adding the delay-function into the code I have but I cant seem to find the right place.
Would delay(5000); be the proper function to use?
My question is: where do I supplement the code that I have with the proper function to make it pause at 180 degrees?
Here is the code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2
int angle =90; // initial angle for servo (beteen 1 and 179)
int angleStep =10;
const int minAngle = 0;
const int maxAngle = 180;
const int type =2;//watch video for details. Link is at the top of this code (robojax)
int buttonPushed =0;
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
myservo.write(angle);//initial position
}
void loop() {
if(digitalRead(pushButtonPin) == LOW){
buttonPushed = 1;
}
if( buttonPushed ){
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle >= maxAngle) {
angleStep = -angleStep;
if(type ==1)
{
buttonPushed =0;
}
}
if (angle <= minAngle) {
angleStep = -angleStep;
if(type ==2)
{
buttonPushed =0;
}
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
}
}
Try this, dirty but but maybe working. Look for "Railroader" in the code.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
#define servoPin 3 //~
#define pushButtonPin 2
int angle = 90; // initial angle for servo (beteen 1 and 179)
int angleStep = 10;
const int minAngle = 0;
const int maxAngle = 180;
const int type = 2; //watch video for details. Link is at the top of this code (robojax)
int buttonPushed = 0;
void setup() {
// Servo button demo by Robojax.com
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
pinMode(pushButtonPin, INPUT_PULLUP);
Serial.println("Robojax Servo Button ");
myservo.write(angle);//initial position
}
void loop() {
if (digitalRead(pushButtonPin) == LOW) {
buttonPushed = 1;
}
if ( buttonPushed ) {
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle >= maxAngle) {
delaye(5000);// added by Railroader
angleStep = -angleStep;
if (type == 1)
{
buttonPushed = 0;
}
}
if (angle <= minAngle) {
angleStep = -angleStep;
if (type == 2)
{
buttonPushed = 0;
}
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
delay(100); // waits for the servo to get there
}
}
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
delay(3000);
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
dinoclassic:
I want to add a pause when the servoe reaches 180. So it goes from 0 to 180, pauses for X seconds, and goes back to 0 without further input.
Would delay(5000); be the proper function to use?
Yes, if 'X' is equal to 5.
dinoclassic:
My question is: where do I supplement the code that I have with the proper function to make it pause at 180 degrees?
Right after:
// reverse the direction of the moving at the ends of the angle:
if (angle >= maxAngle) {