how to use push button to control servo motor

Hi,

This is my first post and I am just starting to learn arduino.

I am having some problem in my codes. Currently i am using Force Sensitive Resistor (FSR) to run the servo motor. I don't know how to use push button instead of FSR. These are the codes...

Please help me.....

Mugilan

int setPoint = 500;
int outputValue = 0;
int servoPin = 9;     // Control pin for servo motor
int minPulse = 700;   // Minimum servo position
int maxPulse = 1100;  // Maximum servo position
int pulse = 0;        // Amount to pulse the servo

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int analogValue = 0;  // the value returned from the analog sensor
int analogPin = 0;    // the analog pin that the sensor's on

void setup() {
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
  pulse = minPulse;           // Set the motor position value to the minimum
  Serial.begin(9600);
}

void loop() {
  analogValue = analogRead(analogPin);      // read the analog input
  Serial.println(analogValue);
  
  outputValue =( setPoint - analogValue)+512;
  Serial.print(setPoint);
  Serial.print("-");
  Serial.print(analogValue);
  Serial.print("=");
  Serial.println(outputValue);
  pulse = map(outputValue,0,500,minPulse,maxPulse);
  // convert the analog value to a range between minPulse and maxPulse

  // pulse the servo again if the refresh time (20 ms) has passed:
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // Turn the motor on
    delayMicroseconds(pulse);       // Length of the pulse sets the motor position
    digitalWrite(servoPin, LOW);    // Turn the motor off
    lastPulse = millis();           // save the time of the last pulse
  }
}

Read this post: http://forum.arduino.cc/index.php?topic=234577.new;topicseen#new

is there any possibilities to use push button without 'delay'???

You need to think of your problem in small pieces.

Write a short sketch that allows you to switch an LED on and off using a push button.

Write a short sketch that causes the servo to move when a variable is HIGH and move differently when the same variable is LOW.

Then it should be easy to combine them by using the push button to change the value of the variable.

Write another short sketch that lights an LED for a set duration using the technique in the Blink Without Delay example sketch. When you understand how that works you can add the concept to your servo sketch.

...R

Simple servo button code.

//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);
  }
}