Hey everyone, I’m trying to add a delay to a led on my project, I have 7 servos in an iron man forearm, the servos all are on a sequence delay 150. There are 7 leds & I want one of them which is led 11 in my code to run on a delay & turn on once the servo is in the opened position. I tried putting a delay under led pin 11 under void loop however that just delayed all the servos. Here is the code
#include <Servo.h>
// constants won't change
int TOUCH_SENSOR_PIN = 12; // Arduino pin connected to touch sensor's pin
int SERVO_PIN2 = 2; // Arduino pin connected to servo motor's pin
int SERVO_PIN3 = 3;
int SERVO_PIN4 = 4;
int SERVO_PIN5 = 5;
int SERVO_PIN6 = 6;
int SERVO_PIN7 = 7;
int SERVO_PIN8 = 8;
const int LED_PIN9 = 9; // Arduino pin connected to LED's pin
const int LED_PIN10 = 10; // Arduino pin connected to LED's pin
const int LED_PIN11 = 11; // Arduino pin connected to LED's pin
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo
Servo myservo5; // create servo object to control a servo
Servo myservo6; // crceate servo object to control a servo
Servo myservo7; // create servo object to control a servo
Servo myservo8; // create servo object to control a servo
int ledState = LOW; // the current state of LED
int lastButtonState; // the previous state of button
int currentButtonState; // the current state of button
// variables will change:
int angle1 = -35; // the current angle of servo motor
int angle2 = 35;
int angle3 = 35;
int angle4 = -70;
int lastTouchState; // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor
void setup() {
Serial.begin(9600); // initialize serial
pinMode(TOUCH_SENSOR_PIN, INPUT_PULLUP); // set arduino pin to input mode
pinMode(LED_PIN9, OUTPUT);
pinMode(LED_PIN10, OUTPUT);
pinMode(LED_PIN11, OUTPUT);
myservo2.attach(SERVO_PIN2); // attaches the servo on pin 2 to the servo object
myservo2.write(angle1);
myservo3.attach(SERVO_PIN3); // attaches the servo on pin 3 to the servo object
myservo3.write(angle3);
myservo4.attach(SERVO_PIN4); // attaches the servo on pin 4 to the servo object
myservo4.write(angle3);
myservo5.attach(SERVO_PIN5); // attaches the servo on pin 5 to the servo object
myservo5.write(angle1);
myservo6.attach(SERVO_PIN6); // attaches the servo on pin 6 to the servo object
myservo6.write(angle3);
myservo7.attach(SERVO_PIN7); // attaches the servo on pin 7 to the servo object
myservo7.write(angle3);
myservo8.attach(SERVO_PIN8); // attaches the servo on pin 8 to the servo object
myservo8.write(angle4);
currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
}
void loop() {
lastTouchState = currentTouchState; // save the last state
currentTouchState = digitalRead(TOUCH_SENSOR_PIN); // read new state
if(lastTouchState == HIGH && currentTouchState == LOW) {
Serial.println("The sensor is touched");
// toggle state of LED
ledState = !ledState;
// control LED arccoding to the toggled state
digitalWrite(LED_PIN9, ledState);
digitalWrite(LED_PIN10, ledState);
digitalWrite(LED_PIN11, ledState);
if(angle1 ==-35)
angle1 = 35;
else
if(angle1 == 35)
angle1 = -35;
if(angle3 == 35)
angle3 =0;
else
if(angle3 ==0)
angle3 = 35;
if(angle4 == -70)
angle4 =70;
else
if(angle4 ==70)
angle4 = -70;
// control servo motor arccoding to the angle
myservo2.write(angle1);
delay(150);
myservo3.write(angle3);
delay(150);
myservo4.write(angle3);
delay(150);
myservo5.write(angle3);
delay(150);
myservo6.write(angle1);
delay(150);
myservo7.write(angle1);
delay(150);
myservo8.write(angle4);
delay(150);
}
}