here is my final code that seems to work without a delay:
//int outputPin1 = 10; // Digital output pin for pulse generation
//int outputPin2 = 11; // Additional digital output pin for another random pulse
//int outputPin3 = 3; // PWM output pin for random value 1
//int outputPin4 = 6; // PWM output pin for random value 2
//const int potPin1 = A4; // Analog input pin for the first potentiometer
//const int potPin2 = A7; // Analog input pin for the second potentiometer
//
//unsigned long lastPulseTime1 = 0; // Variable to store the time of the last pulse for outputPin1
//unsigned long lastPulseTime2 = 0; // Variable to store the time of the last pulse for outputPin2
//unsigned long currentDelay1 = 0; // Variable to store the current delay between pulses for outputPin1
//unsigned long currentDelay2 = 0; // Variable to store the current delay between pulses for outputPin2
//bool pulseInProgress1 = false; // Flag to track if a pulse is in progress for outputPin1
//bool pulseInProgress2 = false; // Flag to track if a pulse is in progress for outputPin2
//
//void setup() {
// Serial.begin(115200);
// pinMode(outputPin1, OUTPUT);
// pinMode(outputPin2, OUTPUT);
// pinMode(outputPin3, OUTPUT);
// pinMode(outputPin4, OUTPUT);
// randomSeed(analogRead(0)); // Seed the random number generator with an analog input value
//}
//
//void loop() {
// unsigned long currentTime = millis();
//
// // Check if it's time for a new pulse on outputPin1
// if (!pulseInProgress1 && currentTime - lastPulseTime1 >= currentDelay1) {
// // Start a new pulse on outputPin1
// digitalWrite(outputPin1, HIGH);
// pulseInProgress1 = true;
//
// // Update the last pulse time for outputPin1
// lastPulseTime1 = currentTime;
//
// // Generate a random value on outputPin3
// analogWrite(outputPin3, random(256));
// }
//
// // Check if the pulse duration has passed for outputPin1
// if (pulseInProgress1 && currentTime - lastPulseTime1 >= 10) {
// // End the pulse on outputPin1
// digitalWrite(outputPin1, LOW);
// pulseInProgress1 = false;
//
// // Generate a random delay between the constant minimum (10) and the variable upper limit for outputPin1
// int upperLimitDelay1 = map(analogRead(potPin1), 0, 1023, 50, 2000);
// currentDelay1 = random(10, upperLimitDelay1);
//
// // Print the current delay duration for outputPin1
// Serial.print("Current Delay (OutputPin1): ");
// Serial.println(currentDelay1);
// }
//
// // Check if it's time for a new pulse on outputPin2
// if (!pulseInProgress2 && currentTime - lastPulseTime2 >= currentDelay2) {
// // Start a new pulse on outputPin2
// digitalWrite(outputPin2, HIGH);
// pulseInProgress2 = true;
//
// // Update the last pulse time for outputPin2
// lastPulseTime2 = currentTime;
//
// // Generate a random value on outputPin4
// analogWrite(outputPin4, random(256));
// }
//
// // Check if the pulse duration has passed for outputPin2
// if (pulseInProgress2 && currentTime - lastPulseTime2 >= 10) {
// // End the pulse on outputPin2
// digitalWrite(outputPin2, LOW);
// pulseInProgress2 = false;
//
// // Generate a random delay between the constant minimum (10) and the variable upper limit for outputPin2
// int upperLimitDelay2 = map(analogRead(potPin2), 0, 1023, 50, 2000);
// currentDelay2 = random(10, upperLimitDelay2);
//
// // Print the current delay duration for outputPin2
// Serial.print("Current Delay (OutputPin2): ");
// Serial.println(currentDelay2);
// }
//
// // Add other non-blocking tasks here
//}
//
int outputPin1 = 10; // Digital output pin for pulse generation
int outputPin2 = 11; // Additional digital output pin for another random pulse
int outputPin3 = 3; // PWM output pin for random value 1
int outputPin4 = 6; // PWM output pin for random value 2
const int potPin1 = A4; // Analog input pin for the first potentiometer
const int potPin2 = A7; // Analog input pin for the second potentiometer
unsigned long lastPulseTime1 = 0; // Variable to store the time of the last pulse for outputPin1
unsigned long lastPulseTime2 = 0; // Variable to store the time of the last pulse for outputPin2
unsigned long currentDelay1 = 0; // Variable to store the current delay between pulses for outputPin1
unsigned long currentDelay2 = 0; // Variable to store the current delay between pulses for outputPin2
bool pulseInProgress1 = false; // Flag to track if a pulse is in progress for outputPin1
bool pulseInProgress2 = false; // Flag to track if a pulse is in progress for outputPin2
void setup() {
Serial.begin(115200);
pinMode(outputPin1, OUTPUT);
pinMode(outputPin2, OUTPUT);
pinMode(outputPin3, OUTPUT);
pinMode(outputPin4, OUTPUT);
randomSeed(analogRead(0)); // Seed the random number generator with an analog input value
}
void loop() {
unsigned long currentTime = millis();
// Check if it's time for a new pulse on outputPin1
if (!pulseInProgress1 && currentTime - lastPulseTime1 >= currentDelay1) {
// Start a new pulse on outputPin1
digitalWrite(outputPin1, HIGH);
pulseInProgress1 = true;
// Update the last pulse time for outputPin1
lastPulseTime1 = currentTime;
// Generate a random value on outputPin3
int pwmValue1 = random(256);
analogWrite(outputPin3, pwmValue1);
// Convert PWM value to voltage (assuming a 5V Arduino)
float voltage1 = map(pwmValue1, 0, 255, 0, 5000) / 1000.0;
// Print the voltage value on Serial
Serial.print("Voltage (OutputPin3): ");
Serial.println(voltage1);
}
// Check if the pulse duration has passed for outputPin1
if (pulseInProgress1 && currentTime - lastPulseTime1 >= 10) {
// End the pulse on outputPin1
digitalWrite(outputPin1, LOW);
pulseInProgress1 = false;
// Generate a random delay between the constant minimum (10) and the variable upper limit for outputPin1
int upperLimitDelay1 = map(analogRead(potPin1), 0, 1023, 50, 2000);
currentDelay1 = random(10, upperLimitDelay1);
// Print the current delay duration for outputPin1
Serial.print("Current Delay (OutputPin1): ");
Serial.println(currentDelay1);
}
// Check if it's time for a new pulse on outputPin2
if (!pulseInProgress2 && currentTime - lastPulseTime2 >= currentDelay2) {
// Start a new pulse on outputPin2
digitalWrite(outputPin2, HIGH);
pulseInProgress2 = true;
// Update the last pulse time for outputPin2
lastPulseTime2 = currentTime;
// Generate a random value on outputPin4
int pwmValue2 = random(256);
analogWrite(outputPin4, pwmValue2);
// Convert PWM value to voltage (assuming a 5V Arduino)
float voltage2 = map(pwmValue2, 0, 255, 0, 5000) / 1000.0;
// Print the voltage value on Serial
Serial.print("Voltage (OutputPin4): ");
Serial.println(voltage2);
}
// Check if the pulse duration has passed for outputPin2
if (pulseInProgress2 && currentTime - lastPulseTime2 >= 10) {
// End the pulse on outputPin2
digitalWrite(outputPin2, LOW);
pulseInProgress2 = false;
// Generate a random delay between the constant minimum (10) and the variable upper limit for outputPin2
int upperLimitDelay2 = map(analogRead(potPin2), 0, 1023, 50, 2000);
currentDelay2 = random(10, upperLimitDelay2);
// Print the current delay duration for outputPin2
Serial.print("Current Delay (OutputPin2): ");
Serial.println(currentDelay2);
}
// Add other non-blocking tasks here
}
The thing is that I have not clean output from the pwm pins. How can I do filtering or changing the pwm frequency to br above the hearing frequency (20k)