Stopping the light after a 20 seconds

I've been scouring the internet trying to find the answer to this dilemma however was unable to figure it out. A synopsis of what I'm attempting to do is move four servos after 20 seconds and make the light stay on. Code is down below. (Btw im new at this so if this doesn't make any sense sorry in advance)

#include <Servo.h>
#define LED 13

 Servo myservo1;
 Servo myservo2;
 Servo myservo3;
 Servo myservo4;
 
int angle = 0;
int number = 20;


void setup(){

  pinMode(LED,OUTPUT);
  pinMode(2, OUTPUT);
  //mosfet, LED, & Servo//
  
  myservo1.attach(11);
  myservo2.attach(10);
  myservo3.attach(9);
  myservo4.attach(8);

  
  myservo1.write(0);
  delay(100);
  myservo2.write(0);
  delay(100);
  myservo3.write(0);
  delay(100);
  myservo4.write(0);
  delay(100);
  
  digitalWrite(2,LOW);//mosfet which isn't needed rn//
  delay(100);
}

void loop(){
  
  if(number >= 10)
  digitalWrite(LED,HIGH);
  delay(100);
   
  digitalWrite(LED,LOW);
  delay(100);
  
   number - 2;
    if(number = 10){
       myservo1.write(90);
       myservo2.write(90);
       myservo3.write(90);
       myservo4.write(90);
       delay(100);
      
      digitalWrite(2,HIGH);//mosfet which isn't needed rn//
      delay(100);
      
      digitalWrite(LED,HIGH);
      delay(100);
    }
}

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

I don't see any code...

code:
#include <Servo.h>
#define LED 13

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int angle = 0;
int number = 20;

void setup(){

pinMode(LED,OUTPUT);
pinMode(2, OUTPUT);
//mosfet, LED, & Servo//

myservo1.attach(11);
myservo2.attach(10);
myservo3.attach(9);
myservo4.attach(8);

myservo1.write(0);
delay(100);
myservo2.write(0);
delay(100);
myservo3.write(0);
delay(100);
myservo4.write(0);
delay(100);

digitalWrite(2,LOW);//mosfet which isn't needed rn//
delay(100);
}

void loop(){

if(number >= 10)
digitalWrite(LED,HIGH);
delay(100);

digitalWrite(LED,LOW);
delay(100);

number - 2;
if(number = 10){
myservo1.write(90);
myservo2.write(90);
myservo3.write(90);
myservo4.write(90);
delay(100);

  digitalWrite(2,HIGH);//mosfet which isn't needed rn//
  delay(100);
  
  digitalWrite(LED,HIGH);
  delay(100);
}

}

code is now up. I just didn't expect a response so soon

But not formatted properly. Please read reply #2.

that is assignment, not a comparison '=' vs. '=='

= is not the same as ==


number - 2;

oops

move four servos after 20 seconds and make the light stay on

#include <Servo.h>
#define LED 13

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int angle = 0;
int number = 20;

void setup() {

  pinMode(LED, OUTPUT);
  pinMode(2, OUTPUT);
  //mosfet, LED, & Servo//

  myservo1.attach(11);
  myservo2.attach(10);
  myservo3.attach(9);
  myservo4.attach(8);

  myservo1.write(0);
  delay(100);
  myservo2.write(0);
  delay(100);
  myservo3.write(0);
  delay(100);
  myservo4.write(0);
  delay(100);

  digitalWrite(2, LOW); //mosfet which isn't needed rn//
  delay(100);
  digitalWrite(LED, LOW);

  delay(20000); // after 20 seconds
  myservo1.write(90);
  myservo2.write(90);
  myservo3.write(90);
  myservo4.write(90);
  digitalWrite(LED, HIGH); //make the light stay on.
}

void loop() {
}
1 Like

@gerivega has certainly provided a solution to what the OP asked for- remains to be seen if what they asked for is actually what they need :wink:

#include <Servo.h>

#define NUM_SERVOS 4
#define SV1_PIN 11
#define SV2_PIN 10
#define SV3_PIN 9
#define SV4_PIN 8
#define LED_PIN 13
#define MOSFET_PIN 2

Servo servos[NUM_SERVOS];
byte servoPins[] = { SV1_PIN, SV2_PIN, SV3_PIN, SV4_PIN };

void moveAllServos(int value) {
  for (byte i = 0; i < NUM_SERVOS; i++) {
    servos[i].write(value);
    delay(15);
  }
}

void setup() {
  for (byte i = 0; i < NUM_SERVOS; i++)
    servos[i].attach(servoPins[i]);
  pinMode(LED_PIN, OUTPUT);
  pinMode(MOSFET_PIN, OUTPUT);
  moveAllServos(0);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  if (millis() >= 20000) {
    moveAllServos(90);
    digitalWrite(LED_PIN, HIGH);
  }
}

See it in action: sketch.ino - Wokwi Arduino and ESP32 Simulator

millis() wraps around to zero after 49 days but if you're not running the Arduino for more than that then the above will be fine. Else, use this in loop:

static bool moveComplete = false;
  if (millis() >= 20000 && !moveComplete) {
    moveAllServos(90);
    digitalWrite(LED_PIN, HIGH);
    moveComplete = true;
  }

This is close but for every second until t-0, I want the light to be blinking on and off to indicate when I'll reach t-0. Thank you nonetheless

So just to be sure, apart from what you mention there as part of your need not yet met, you're happy that @gerivega 's solution works in setup() not loop() and happens just once at power up or reset, and that's what you want?

Yes I'm ok with moving things to setup() rather then loop(). I had put my work in loop() because of the if(){} statement. As long as the light blinks for every second until the timer reaches zero, and at zero the light stops blinking while the servos simultaneously actuate, I'm ok with whatever.

I want the light to be blinking on and off

Well you didn't mention that before :smiley: Do you want the light to flash ON at every second, or cycle for on for a seconds, off for a second? (so the light comes on every two seconds)

The "Critera" was changed this morning lol. Yes I want the light to flash ON at every second.

"They dont care" (from your edited/deleted message) - So you're on a forum farming out coding?

Anyway...

#include <Servo.h>

#define NUM_SERVOS 4
#define SV1_PIN 11
#define SV2_PIN 10
#define SV3_PIN 9
#define SV4_PIN 8
#define LED_PIN 13
#define MOSFET_PIN 2
#define FLASH_LED_EVERY 1000

Servo servos[NUM_SERVOS];
byte servoPins[] = { SV1_PIN, SV2_PIN, SV3_PIN, SV4_PIN };

void moveAllServos(int value) {
  for (byte i = 0; i < NUM_SERVOS; i++) {
    servos[i].write(value);
    delay(15);
  }
}

void setup() {
  for (byte i = 0; i < NUM_SERVOS; i++)
    servos[i].attach(servoPins[i]);
  pinMode(LED_PIN, OUTPUT);
  pinMode(MOSFET_PIN, OUTPUT);
  moveAllServos(0);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  static bool moveComplete = false;
  static bool timerExpired = false;
  static unsigned long timeCapture;

  if (millis() >= 20000 && !moveComplete) {
    moveAllServos(90);
    digitalWrite(LED_PIN, HIGH);
    timerExpired = true;
  }

  if ((millis() - timeCapture) > FLASH_LED_EVERY / 2 && !timerExpired) {
    timeCapture = millis();
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  }
}

See if they like this: sketch.ino - Wokwi Arduino and ESP32 Simulator

All I wanted was to make my previous code work because I thought it made sense. Then I saw other code that literally does the same thing with no if()else() statement and decided that I could do that. This isn't an assignment btw because that would be plagiarism. This is more for me to figure out what I did wrong and other means to figure out this problem. Also thank you for said assistance with the code

Yeah, that's fair. No problem at all for the code (providing it fits your need). If there's anything in it that doesn't make sense just ask.

For the void loop() can you please explain whats going on?

static bool moveComplete = false;
  static bool timerExpired = false;
  static unsigned long timeCapture;

  if (millis() >= 20000 && !moveComplete) {
    moveAllServos(90);
    digitalWrite(LED_PIN, HIGH);
    timerExpired = true;
  }

  if ((millis() - timeCapture) > FLASH_LED_EVERY / 2 && !timerExpired) {
    timeCapture = millis();
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  }
}
void loop() {
  // A static variable is only visible inside the scope it's declared in. So this keeps it local to loop();
  // It also 'exists across time' so to speak. I.e it's not 'reinitialised every time through the loop.


// We'll use a moveComplete flag so that we don't accidentally re-enter the if statement when millis() wraps around to zero.
  static bool moveComplete = false; 

// We'll use a timerExpired flag so that we don't continually flash the LED after the 20seconds has expired
  static bool timerExpired = false;

// timeCapture will be used to hold the current time at a given point so we can compare it to how much time has elapsed...
//... since we last toggled the LED
  static unsigned long timeCapture;


// millis() is an arduino function that gives you how many milliseconds the arduino has been running for since it started up...
// ... it's effectively the 'current time'. 

// We enter this statement if the current time in milliseconds is greater than or equal to 200000 millis (i.e 20 seconds)... as in if 20 seconds ...
// has passed since we started the Arduino, AND the moveComplete flag has not been set to true yet... 
  if (millis() >= 20000 && !moveComplete) { // ! before a variable means 'not' so !moveComplete means that moveComplete = false
    // 20000 above actually is a bit of a 'magic number' you should probably move it to a #define above like the rest.
    //Then we move the servos
    moveAllServos(90);
    // We set the LED HIGH
    digitalWrite(LED_PIN, HIGH);
    // We then set the timerExpired flag to true, so that the LED flash code block cannot be entered.
    timerExpired = true;
  }

// We enter this statement if the current time, minus the last time we captured the current time is greater than the flash interval / 2
// (the flash interval was every 1 second (1000ms) so we want to toggle the led every 500ms)
// AND the timerExpired flag is still false (because if the timer has expired we want to stop toggling the LED)
  if ((millis() - timeCapture) > FLASH_LED_EVERY / 2 && !timerExpired) {
    // If we're inside the statement we update our timeCapture with the current time so we can compare is to the next +500ms interval
    timeCapture = millis();
    // Read this inside out... so digitalRead(LED_PIN) returns true or false depending on if the pin is HIGH or LOW
    // so !digitalRead(LED_PIN) will give the opposite of whatever digitalRead returns. 
    // Effectively were saying write to the LED_PIN, the opposite of whatever it currently is. So this toggles it on/off.
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  }
}