Go Down

Topic: servo with LED lights help (Read 814 times) previous topic - next topic

ok i have programed my arduino to operate a servo with one button with an LED, but I'm trying to put a delay on the light for when it turns on and i can't figure that part out. here the coding that i used.


#include <Servo.h>

int inputPin = 10;      // Push button input pin
int btnVal = 0;         // Current value of pushbutton
int ledPin = 13;         // Pin for Green LED built into proto shield
int outputPin = 9;      // Servo pin
int servo_pos = 0;      // Current servo position
int servo_pos_a = 90;   // Position destination a
int servo_pos_b = 174;   // Position destination b
int servo_state = 0;    // 0 = position a, 1 = position b

Servo mrservo;  // create servo object to control the servo

void setup() {
pinMode(inputPin, INPUT); // Push Button
pinMode(ledPin, OUTPUT); // LED
mrservo.attach(outputPin);  // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
Serial.println("Starting...");
}

void loop() {
btnVal = digitalRead(inputPin);
delay (5);
if(btnVal == LOW) {
Serial.println("Button: LOW");
if(servo_state) {
Serial.println("Position: a");

} else {
Serial.println("Position: b");
}
}
if(btnVal == HIGH) {
Serial.println("Button: HIGH");
if(servo_state) {
Serial.println("Position: a");
} else {
Serial.println("Position: b");
}
// Move from position a to position b
if(servo_state == 0) {
// I don't know how to concatenate
Serial.print(servo_pos_a); Serial.print(" to "); Serial.println(servo_pos_b);
for(servo_pos = servo_pos_a; servo_pos < servo_pos_b; servo_pos += 4) {
mrservo.write(servo_pos);
digitalWrite(ledPin, HIGH);
delay(15);                       // waits 15ms for the servo to reach the position
}
servo_state = 1;
} else {  // Move from position b to position a
Serial.print(servo_pos_b); Serial.print(" to "); Serial.println(servo_pos_a);
for(servo_pos = servo_pos_b; servo_pos >= servo_pos_a ; servo_pos -= 4) {
mrservo.write(servo_pos);
digitalWrite(ledPin, LOW);
delay(15);
}
servo_state = 0;
}
}
// Separate each cycle's output
Serial.println(); Serial.println();
delay(250);
}
any help will be appreciated. thanks in advance.

Not quite sure what you mean.
1. Delay before led comes on?
2. Delay before led goes off?
3. Only keep led on for a period of time?

Go Up