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?
-
can the LED flashing exactly 2 time at each step? or it just keep flashing itself? possibly synchronize each step with a flashing phase?
-
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.
}
}