a
wonder if it is possible to blink led's for a specific hz frequency?
Of course it is. If you look at the BlinkWithoutDelay example you will see how to blink an LED at 1 Hz. What would happen, do you think, if your changed the period between the LED turning on and off ?
It is very possible.
Why are you doing this ?
see https://www.arduino.cc/en/tutorial/BlinkWithoutDelay to see how to blink a led.
It describes the basic idea how to write non blocking code, you should learn it.
If you just want a simple to use library have a look at my page
https://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm
There is an easier way. Use tone() function. There are limitations, for example the minimum frequency is 31Hz and the duty cycle is always 50%.
Right now we know so little about what the OP wants to do. Why do they want to blink LEDs at a certain frequency, for example? Its not as if the eye can tell the difference. Often beginners don't understand the difference between frequency and duty cycle and can be surprised when finding that changing the frequency does not change the brightness.
[quote}Feels like i cannot get higher HZ out of the arduino.[/quote]
You are using a period of 40 milliseconds between blinks and a delay() of 50 milliseconds every time through loop() so this is going to adversely affect the highest achievable frequency, don't you think ?
You could use micros() instead of millis() but for goodness sake get rid of the delay()
Weren’t you told to get rid of: delay(50); // crude de-bouncing
Scan your switches every 50ms, do not use delay().
When you see the change in the switch to the needed level, you can set flags that can then control sections of your sketch.
When a switch is closed, a LED starts flashing at frequency Y.
After X minutes the LED flashes at frequency Z.
Then after another X minutes the LED stops flashing.
Is the above correct ?
Tell us what this code does:
// Version YY/MM/DD
// 1.00 20/04/01 Running code
#define PUSHED LOW // <------<<<<< HIGH or LOW as needed
//***********************************************************************
const byte startSwitch = 2;
const byte LEDpin = 3;
const byte heartbeatLED = 13;
byte currentState;
byte lastState;
bool flashFlag = false;
//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long flashMillis;
unsigned long timeOne = 1000000ul / (10 * 2); //for frequency 1
//***********************************************************************
void setup()
{
pinMode(heartbeatLED, OUTPUT);
pinMode(LEDpin, OUTPUT);
} //END of setup()
//***********************************************************************
void loop()
{
//**************************
//heartbeat LED will toggle as long as the sketch is non blocking
if (millis() - heartbeatMillis >= 200)
{
//restart timer
heartbeatMillis = millis();
//toggle LED
digitalWrite(heartbeatLED , !digitalRead(heartbeatLED));
}
//**************************
//time to check our switches ?
if (millis() - switchMillis >= 50)
{
//restart timer
switchMillis = millis();
checkSwitches();
}
//**************************
//time to toggle the LED ?
if (micros() - flashMillis >= timeOne)
{
//restart timer
flashMillis = micros();
digitalWrite(LEDpin, !digitalRead(LEDpin));
}
//**************************
//other non blocking code
//**************************
} //END of loop()
//***********************************************************************
void checkSwitches()
{
} //END of checkSwitches()
Yes, but what is occurring in the code for these things to happen ?
Tell us how this code works.
LEDs are flashing, YES, Right.
I am asking you to explain HOW this code works.
If you repeat the same thing, this conversation is ended.
Since you will not explain how the code works you will have to finish this code yourself.
Looks like you are playing an April Fools joke. Ha Ha
G o o d b y e
One last chance.
What does this code do?
//**************************
//time to toggle the LED ?
if (micros() - flashMicros >= timeOne)
{
//restart timer
flashMicros = micros();
digitalWrite(LEDpin, !digitalRead(LEDpin));
}
byte stores an 8-bit unsigned number, from 0 to 255 right? Yes
bool = truh or false. Yes
Unsigned long heartbeat,switchmillis and flashmillis must be the things that can change? HZ and time.
These are variables needed to create 3 different timers.
Line 22 (10*2) = 1 HZ is there any logic in what for example 43 hz are then?
setup
setting pins as output and to add button we add a input right? Yes
Now the nonblocking code starts? why do we put if infront of millis?
ms = milliseconds
Here is a new sketch.
What do you see happening with this new code ?
If you do not understand the code, what are you having problems with ?
// Version YY/MM/DD
// 2.00 20/04/02 Running code
//Switch connection
//+5V---[internal pullup resistor]---[input pin]---[N.O. switch]---GND(0V)
#define PUSHED LOW
#define firstFrequency 10 //10 Hz
#define secondFrequency 43 //43 Hz
//***********************************************************************
const byte startSwitch = 2;
const byte firstLEDpin = 3;
const byte secondLEDpin = 4;
const byte heartbeatLED = 13;
byte currentState;
byte lastState;
bool flashFlag = false;
//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long flashOneMicros;
unsigned long flashTwoMicros;
const unsigned long timeOne = 1000000ul / (firstFrequency * 2);
const unsigned long timeTwo = 1000000ul / (secondFrequency * 2);
//***********************************************************************
void setup()
{
pinMode(heartbeatLED, OUTPUT);
pinMode(firstLEDpin, OUTPUT);
pinMode(secondLEDpin, OUTPUT);
} //END of setup()
//***********************************************************************
void loop()
{
//**************************
//heartbeat LED will toggle as long as the sketch is non blocking
if (millis() - heartbeatMillis >= 200)
{
//restart timer
heartbeatMillis = millis();
//toggle LED
digitalWrite(heartbeatLED , !digitalRead(heartbeatLED));
}
//**************************
//time to check our switches ?
if (millis() - switchMillis >= 50)
{
//restart timer
switchMillis = millis();
checkSwitches();
}
//**************************
//time to toggle the 1st LED ?
if (micros() - flashOneMicros >= timeOne)
{
//restart timer
flashOneMicros = micros();
digitalWrite(firstLEDpin, !digitalRead(firstLEDpin));
}
//**************************
//time to toggle the 2nd LED ?
if (micros() - flashTwoMicros >= timeTwo)
{
//restart timer
flashTwoMicros = micros();
digitalWrite(secondLEDpin, !digitalRead(secondLEDpin));
}
//**************************
//other non blocking code
//**************************
} //END of loop()
//***********************************************************************
void checkSwitches()
{
} //END of checkSwitches()
We are trying to teach you the 'how and why' of the way things are done.
If you want to learn, we will continue with advancing 'you' to the final sketch.
This is for your benefit not ours.
Do you understand what was said in #26 post ?
Please answer the questions in #27 post if you want to proceed with learning.
If you understand all of the things being presented, you can proceed with the rest of the sketch yourself.
Patience, we are trying to teach you something.
What does this new sketch do ?
Do you understand what it does ?
What is the purpose of the variable flashFlag ?
// Version YY/MM/DD
// 3.00 20/04/01 Running code
//Switch connection
//+5V---[internal pullup resistor]---[input]---[switch]---GND(0V)
#define PUSHED LOW
#define firstFrequency 10 //10 Hz
#define secondFrequency 20 //20 Hz
//***********************************************************************
const byte startSwitch = 2; //pushed/closed = LOW
const byte firstLEDpin = 3; //HIGH turns on LED
const byte secondLEDpin = 4; //HIGH turns on LED
const byte heartbeatLED = 13; //HIGH turns on LED
byte currentState;
byte lastState;
bool flashFlag = false;
//timing stuff
unsigned long heartbeatMillis;
unsigned long switchMillis;
unsigned long flashOneMircos;
unsigned long flashTwoMicros;
unsigned long interval;
const unsigned long timeOne = 1000000ul / (firstFrequency * 2);
const unsigned long timeTwo = 1000000ul / (secondFrequency * 2);
//***********************************************************************
void setup()
{
pinMode(heartbeatLED, OUTPUT);
pinMode(firstLEDpin, OUTPUT);
pinMode(secondLEDpin, OUTPUT);
pinMode(startSwitch, INPUT_PULLUP); //pushed/closed = LOW
} //END of setup()
//***********************************************************************
void loop()
{
//**************************
//heartbeat LED will toggle as long as the sketch is non blocking
//time to toggle the heartbeaT led ?
if (millis() - heartbeatMillis >= 200)
{
//restart timer
heartbeatMillis = millis();
//toggle LED
digitalWrite(heartbeatLED , !digitalRead(heartbeatLED));
}
//**************************
//time to check our switches ?
if (millis() - switchMillis >= 50)
{
//restart timer
switchMillis = millis();
checkSwitches();
}
//**************************
//when flashing is enabled, is it time to toggle the LED ?
if (flashFlag == true && micros() - flashOneMircos >= timeOne)
{
//restart timer
flashOneMircos = micros();
//toggle LED
digitalWrite(firstLEDpin, !digitalRead(firstLEDpin));
}
//**************************
//other non blocking code
//**************************
} //END of loop()
//***********************************************************************
void checkSwitches()
{
//**************************
currentState = digitalRead(startSwitch);
//has the switch state changed ?
if (lastState != currentState)
{
//update to the new state
lastState = currentState;
//was the switch just pushed/closed ?
if (currentState == PUSHED)
{
//toggle the enabled Flag
flashFlag = !flashFlag;
if (flashFlag == false)
{
//ensure LED is OFF when flashing is disabled
digitalWrite(firstLEDpin, LOW);
}
}
}
//**************************
//place code for other switches here
} //END of checkSwitches()