Hello, I am trying to be able to blink LEDs at specific hz and the program is working when using lower hz like, 10, 100 and 300 hz. The problem starts when I want up on higher hz. For example if i place 10, 100 and 300 it displays
10 in arduino = 9,98 hz on multimeter, thats fine, close...
100 = 99.77 hz also ok.
300 = 298.2 hz. It is also ok.
Problem starts here...
1000 in arduino is = 975
3000 in arduino is = 2841 hz. Thats to much drop!
also 5000 hz in arduino is = 4633 hz, also way to much drop.
What is wrong here? Cannot really find a solution...
Thanks!
#define PUSHED LOW
#define LEDon HIGH
#define LEDoff LOW
#define firstFrequency 1000// Hz
#define secondFrequency 3000// Hz
#define thirdFrequency 100000// HZ
//***********************************************************************
//Switch connection
//+5V---[internal pullup resistor]---[input]---[switch]---GND(0V)
const byte startSwitch = 2;
const byte LEDpin = 10; //[output pin]---[LED >|]---[220R]---GND/0V
const byte onIndicationLED = 7;
byte currentState;
byte lastStartState;
byte lastStopState;
byte frequencyCounter;
bool flashFlag = false;
//timing stuff
unsigned long onIndicationMillis;
unsigned long switchMillis;
unsigned long xMillis;
unsigned long flashMircos;
//const unsigned long xMinutes = 10 * 60 * 1000ul; //10 minutes
const unsigned long xMinutes = 4 * 1000ul; //for testing use only 4 seconds
const unsigned long timeOne = 1000000ul / (firstFrequency * 2);
const unsigned long timeTwo = 1000000ul / (secondFrequency * 2);
const unsigned long timeThree = 1000000ul / (thirdFrequency * 2);
unsigned long interval;
//***********************************************************************
void setup()
{
pinMode(onIndicationLED, OUTPUT);
pinMode(LEDpin, OUTPUT);
pinMode(startSwitch, INPUT_PULLUP); //pushed/closed = LOW
interval = timeOne;
} //END of setup()
//***********************************************************************
void loop()
{
//**************************
//onIndicationLED will toggle as long as the sketch is non blocking and the arduino is on
//if (millis() - onIndicationMillis >= 1)
{
//restart timer
//onIndicationMillis = millis();
//toggle LED
//digitalWrite(onIndicationLED , !digitalRead(onIndicationLED));
}
//**************************
//check the switches every 50ms
//time to check our switches ?
if (millis() - switchMillis >= 50)
{
//restart timer
switchMillis = millis();
checkSwitches();
}
//**************************
//is flashing enabled ?
if (flashFlag == true)
{
//*************
//flashing timer
//is it time to toggle the LED ?
if (micros() - flashMircos >= interval)
{
//restart timer
flashMircos = micros();
//toggle LED
digitalWrite(LEDpin, !digitalRead(LEDpin));
digitalWrite(onIndicationLED, HIGH);
}
//**************
//X minute timer
//has X minutes expired ?
if (millis() - xMillis >= xMinutes)
{
//restart the timer
xMillis = millis();
//next frequency
frequencyCounter++;
//**************
if (frequencyCounter == 1)
{
interval = timeTwo;
}
//**************
else if (frequencyCounter == 2)
{
interval = timeThree;
}
//**************
else if (frequencyCounter > 2)
{
//disable flashing
flashFlag = false;
//turn LED OFF
digitalWrite(LEDpin, LEDoff);
digitalWrite(onIndicationLED, LEDoff);
}
}
} //END of if (flashFlag == true)
//**************************
} //END of loop()
void checkSwitches()
{
//**************************
//Start switch
currentState = digitalRead(startSwitch);
//has the switch changed state ?
if (lastStartState != currentState)
{
//update to the new state
lastStartState = currentState;
//when we 'are not' flashing, was the switch just pushed/closed ?
if (flashFlag == false && currentState == PUSHED)
{
//start the x timer
xMillis = millis();
//initalize frequency counter
frequencyCounter = 0;
//select the flash frequency
interval = timeOne;
//start the flashing timer
flashMircos = micros();
//enable flashing
flashFlag = true;
}
else if (flashFlag == true && currentState == PUSHED)
{
//disable flashing
flashFlag = false;
//turn LED OFF
digitalWrite(LEDpin, LEDoff);
digitalWrite(onIndicationLED, LEDoff);
}
}
}