What I surmised was the following code which does work very well except for: Min frequency is 31Hz and max seems to be about 80KHz. I am designing an Electronic DC Load, I would like to go from 1Hz to around 1Khz @50% duty cycle to test power supply curve responses with.
Here is the code I am using I will be using a multiturn trim pot later on to set the frequency with, the same nano will also be used to monitor current draw and load voltage via an external ADC an ADS1115..
int frequency = 100; //Set frequency in Hertz
double delayTime = 1000UL*1000UL / (frequency * 2);
void setup()
{
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
}
void loop()
{
digitalWrite(11, LOW);
digitalWrite(13, HIGH);
delayMicroseconds(delayTime);
digitalWrite(13, LOW);
digitalWrite(11, HIGH);
delayMicroseconds(delayTime);
}
Any Help will be appreciated.
Bare in mind I am a noob please.
I tried your code thank you.
It is more than stable enough for my needs.
I should not be going above 1KHz max.
The only thing I found is that connecting an analogRead does induce a lot of noise into the whole circuit, so, one of two options, analog smoothing or an encoder... I am afraid that this might induce more instability as it will take more time to process.
As for timers and interrupts, I am afraid that this is still above my fireplace of understanding.
I was already using the delayMicroseconds in my OP sketch so I understand how that works.
I fear that ec2021 may be correct instating that I may need to use timers and interrupts but I fear that I am still to learn that. The code ec2021 provided works, though as I said to him in my reply that adding analogRead to the mix made it all unstable due to the noise induced, so I will either have to smooth or I'll need to put in an encoder....
Anyway,
Thank you for your input, it is appreciated.
I can't draw a diagram now, limited in time I will post one tomorrow as an addendum to this post.
Basically all is on a bread board being powered from the PC's usb.
using the onboard led pin 13 and an external 2.2k resistor from ground to led and then to pin 11.
I have soldered on a 5K linear bourns multiturn panel mount pot. Wiper to pin A0 one end to ground and the other to the 5V pin on the Arduino Nano Every, and am monitoring the A0 via the serial monitor, and frequency via my fluke multimeter.
Your code as written is stable, no fluctuation whatsoever except the last significant digit.
when I comment out your "Frequency'' setup and substitute the analog one then on the serial monitor and my meter there is a lot of fluctuation, as this is present on the A0 input the output also fluctuates, I am sure this has nothing to do with the code but is a by product of noise on the input. The pots wires are only 100mm or 10cm long but I can see noise present on the output from my meter and the serial monitor input from the pot.
Here is my code along with yours.
const byte pin1 = 11;
const byte pin2 = 13;
byte pin1State = LOW;
byte pin2State = HIGH;
unsigned long halfwave;
//int frequ = A0; //analog input
void setup() {
Serial.begin(115200);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
SetFrequency (100); //Uncomment for digital set
}
void loop() {
Rectangle();
//SetFrequency (); //Uncomment for analog input
}
////////// Analog \\\\\\\\\\\\
/*
void SetFrequency() { // Create void
frequ = analogRead(A0); // Placeholder A0 Value
halfwave = 1000000UL / frequ / 2; // Value to set SetFrequency in void loop with
//Serial.println (halfwave); // Print Value to Serial monitor
//Serial.println (frequ); // Print Value to serial monitor
}
*/
void SetFrequency(int frequ){
halfwave = 1000000UL / frequ / 2;
}
void Rectangle() {
static unsigned long lastChange = 0;
if (micros() - lastChange >= halfwave) {
lastChange = micros();
digitalWrite(pin1, pin1State);
digitalWrite(pin2, pin2State);
pin1State = !pin1State;
pin2State = !pin2State;
}
}
I mrasured the 5V out, there is 4.6V as the usb is also just under 5V itself, but, the instability is definitely caused by noise. I am relatively sure of that.