Hello everyone.
I want to implement a led that operates at a specific frequency, from 1 to 70 Hz.
I used the function that " delay", "delaymicros", "if time >n sec => on, else off structure"...
But there were a limitation.
In case of "delay",
this function can use only milliseconds unit.
However, if it is 6Hz, it must operate each 83.3333...ms ,50% duty cycle.
It occurs an error of 0.3333ms.
In case of delaymicros,
This function works very accurately in the range 3 microseconds and up. We cannot assure that delayMicroseconds will perform precisely for smaller delay-times.
Last,
In case of If structure,
I used the currentmillis, but it was occur the operate delay.
I don't understand this phenomenon.
The method does not matter.
All I need is a way to implement the LEDs, which operates at intervals of 1 to 70 Hz.
Thank you
// These variables store the flash pattern
// and the current state of the LED
int ledPin1 = 11; // the number of the LED pin
int ledState1 = LOW; // ledState used to set the LED
double previousMillis1=0;
double previousMillis2 = 0; // will store last time LED was updated
double OnTime1 = ((1000.00/10)/2); // milliseconds of on-time
double OffTime1 = ((1000.00/10)/2); // milliseconds of off-time
void setup()
{
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
Serial.begin(115200); // open the serial port at 9600 bps:
}
void loop()
{
// check to see if it's time to change the state of the LED
double currentMillis = micros()/1000.0;
// Serial.print(previousMillis1);
// Serial.print("A");
//Serial.print(currentMillis);
//Serial.print("\n");
//Serial.print(micros());
if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
{
ledState1 = LOW; // Turn it off
previousMillis1 = currentMillis; // Remember the time
digitalWrite(ledPin1, ledState1); // Update the actual LED
//Serial.print(previousMillis1);
}
else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
{
ledState1 = HIGH; // turn it on
Serial.print(currentMillis);
Serial.print("A");
Serial.print(previousMillis1);
Serial.print("\n");
previousMillis1 = currentMillis; // Remember the time
digitalWrite(ledPin1, ledState1); // Update the actual LED
//Serial.print(previousMillis2-previousMillis1);
//Serial.print("\n");
//Serial.print(digitalWrite);
//Serial.print("\n");
// Serial.print(previousMillis2);
}
}