I have a program that sweeps between 1hz and 6hz which works fine. I want to introduce an independant output square wave of 75hz on pin 11 .
I've added the code for blink without delay into the program. My problem is what ever I put the value in the interval it seems to stay at 1000ms.
If I run the blink without delay seperately it works.
It's got to be some sort of timing issue
Could anyone give me any clues as to were I'm going wrong
uint8_t freq_Hz[6] ={1,2,3,4,5,6};// Set up an array for the 6 frequencies
uint8_t pulses =25; // Number of pulses per frequecncy
uint8_t duty =10; // Sets Duty cycle
unsigned long period_ms;
unsigned long OnTime;
int out_pin=5;
int out_pin1 =2;
int Led1=8;
const int ledpin=11; //blinkwithout delay pin
int ledState = HIGH;
int Led2=9;
unsigned long previousMillis = 0;
const long interval =10; // set the blink without delay interval.
void setup() {
// put your setup code here, to run once:
{
pinMode(out_pin,OUTPUT);
pinMode(out_pin1, OUTPUT);
pinMode(Led1,OUTPUT);
pinMode(Led2,OUTPUT);
pinMode(11,OUTPUT);
}
}
void loop()
{
for (uint8_t i=0;i<6;++i){ //Cycle through the 6 elements of freq_hz array
period_ms =1000/freq_Hz[i]; // Calculate the period in ms of freq_hz
OnTime = (period_ms*duty)/100; // Calculate from % duty cycle ON Time in ms
for(uint8_t j=0; j<pulses;++j){ // loops frequency x pulses at each frequency
digitalWrite (out_pin,HIGH);
digitalWrite (out_pin1,HIGH);
digitalWrite (Led1,HIGH);
digitalWrite (Led2,HIGH);
delay(OnTime);
digitalWrite(out_pin,LOW);
digitalWrite(out_pin1,LOW);
digitalWrite(Led1,LOW);
digitalWrite(Led2,LOW);
delay(period_ms - OnTime);
unsigned long currentMillis =millis();
if(currentMillis - previousMillis>= interval){
previousMillis= currentMillis;
if(ledState==LOW){
ledState=HIGH;
}else{
ledState= LOW;
}
digitalWrite (ledpin,ledState);
}
}
}
}
uint8_t freq_Hz[6] = {1, 2, 3, 4, 5, 6}; // Set up an array for the 6 frequencies
uint8_t pulses = 25; // Number of pulses per frequecncy
uint8_t duty = 10; // Sets Duty cycle
unsigned long period_ms;
unsigned long OnTime;
int out_pin = 5;
int out_pin1 = 2;
int Led1 = 8;
const int ledpin = 11; //blinkwithout delay pin
int ledState = HIGH;
int Led2 = 9;
unsigned long previousMillis = 0;
const long interval = 10; // set the blink without delay interval.
void setup()
{
// put your setup code here, to run once:
{
pinMode(out_pin, OUTPUT);
pinMode(out_pin1, OUTPUT);
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(11, OUTPUT);
}
}
void loop()
{
for (uint8_t i = 0; i < 6; ++i) //Cycle through the 6 elements of freq_hz array
{
period_ms = 1000 / freq_Hz[i]; // Calculate the period in ms of freq_hz
OnTime = (period_ms * duty) / 100; // Calculate from % duty cycle ON Time in ms
for (uint8_t j = 0; j < pulses; ++j) // loops frequency x pulses at each frequency
{
digitalWrite (out_pin, HIGH);
digitalWrite (out_pin1, HIGH);
digitalWrite (Led1, HIGH);
digitalWrite (Led2, HIGH);
delay(OnTime);
digitalWrite(out_pin, LOW);
digitalWrite(out_pin1, LOW);
digitalWrite(Led1, LOW);
digitalWrite(Led2, LOW);
delay(period_ms - OnTime);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (ledState == LOW)
{
ledState = HIGH;
}
else
{
ledState = LOW;
}
digitalWrite (ledpin, ledState);
}
}
}
}
Seems to me that the BWOD bit is in the wrong place (inside the nested for loops).
I want to introduce an independant output square wave of 75hz on pin 11
The clue is in your requirement. The loop() function must be free running with no blocking code. Once that is working you put the BWoD code in loop() but not inside any other code. The end of loop() is a good place.
The delay was to get the frequency sweep to work.Te blink without delay refers to the independant led I want to flash
IT'S there three line of code stop the independant led from flashing at a set interval but still can't work out how to reso9lve it
period_ms =1000/freq_hz*;* ontime= (period_ms*duty)/100; for (uint8_t j=0;j<pulses;++j) {
I think the problem may be with the delay ontime, The whole program stops and there for can not run the independant led whilst the delay is in place . Is there a way around this please?
The whole program stops and there for can not run the independant led whilst the delay is in place .
Your diagnosis is correct
Is there a way around this please?
Yes, use millis() for timing throughout the program and let loop() do what its name suggests. At any time the program will be in one of several states such as writing a pin HIGH or writing a pin LOW, for instance and a short period of time is needed between changing its state. You can time this using millis() or micros(). Think of it as a BWoD LED with a very short period and check if it time to change state each time through loop().
Derive the on/off periods from an array if you want/need to and count how many times the pin changes state and change the periods when the required number of pulses has been created.
Quite independently you can blink an LED using BWoD with a much longer period elsewhere in loop().