How to synchronize the SERVO step and the LED flashing phase?

Hi,
I have a SERVO rotate 2 degree every curtain time (50), I like the LED on/off 2 times at (22), can the below code do it?

  1. can the LED flashing exactly 2 time at each step? or it just keep flashing itself? possibly synchronize each step with a flashing phase?

  2. if I'd like the LED just on exactly 4000 - less than SERVO sweep 90 steps' total time? can just change: const unsigned long Ledperiod = 4000; is OK?
    and possibly synchronize each 90 steps with one flashing phase (4000)?
    how about the LED do in the other 500 when the SERVO sweep?

in testing 1, I can see barely the LED flashing when testing.

Thanks
Adam

code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

// BOTH FLSERVO and FRSERVO use same millis:
unsigned long SweepstartMillis;  //some global variables available anywhere in the program
unsigned long SweepcurrentMillis;
const unsigned long Sweepperiod = 50;  //the value is a number of milliseconds

unsigned long LedstartMillis;  //some global variables available anywhere in the program
unsigned long LedcurrentMillis;
const unsigned long Ledperiod = 22;  //the value is a number of milliseconds

int pos = 0;    // variable to store the servo position
int posn = 0;
int led = 13;

void setup() {

        Serial.begin(9600);
      Serial.println("xxx_setup!");
  Serial.print("File   : "), Serial.println(__FILE__);
  Serial.print("Date   : "), Serial.println(__DATE__);
  Serial.println("<Arduino is ready>");

  pinMode(led, OUTPUT);
  myservo.attach(47);  // attaches the servo on pin 9 to the servo object
}

void loop() {

  SweepcurrentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (SweepcurrentMillis - SweepstartMillis >= Sweepperiod)  //test whether the period has elapsed
  {
    //  digitalWrite(ledPin, !digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
    pos = pos + 2;
    if (pos > 180)  ///// WAS: 180, [int SERVO_range = 180;]  SERVO scan the first 180 degree and then no readings but just turn ~175 degree?
    {
      SERVO_return();
      pos = 0;
    }
    myservo.write(pos);
    SweepstartMillis = SweepcurrentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
  ledPin();
}


void SERVO_return()
{
 
  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 15 ms for the servo to reach the position
  }
}


void ledPin()
{
  LedcurrentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (LedcurrentMillis - LedstartMillis >= Ledperiod)  //test whether the period has elapsed
  {
      digitalWrite(led, !digitalRead(led)); 
    LedstartMillis = LedcurrentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}

The words are too confusing. Post a timing diagram.

1 Like

The LED is only on for 22 milliseconds and off for 22 milliseconds so no wonder that you can barely see it flashing

1 Like

Yes, sorry of the paint:
servo_t1

Also I don't see any readings from any devices... (see thread title).

1 Like

Yes, really, not to hurt your feelings but that diagram is not useful at all. I think you've probably seen a real one... it's not how it's painted, it's how it's designed.

1 Like

Thank you aarg.
I changed.

I just painted it by Windows paint. sorry of the bad work.
no hurt at all.

I told you, it's not the quality of the artwork. We're not artists here, we don't care. The problem is that it doesn't really convey useful information... it's vague.

Please follow the standard conventions for a timing diagram.

1 Like

well.
the SERVO move one step for 50ms, at the same time LED start to flashing at 22 ms once. so each step got two flashing and 6ms spare there.
how can accomplish it?
Thanks

More vague language? I can't deal with this. Good luck.

Last attempt! Pick up pen and paper, draw a real timing diagram, take a picture of it and post it. It should show all the on and off times and durations. LEDs can be modelled as square waves.

1 Like

OK.
see if this better.
and the asking said LED two flashing is wrong, should be one flashing (22) and one OFF (22). and the OFF is different from (6) spare time.

As written the flashing of the LED is not controlled by the movement of the servo. To fix this you need to reset LedstartMillis to the current time each time you move the servo.

I would also introduce a counter to ensure that the LED only blinks once for each servo move and also finishes in the correct state ready for the next flash

1 Like

Thank you UKHeliBob.
I'll test it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.