# Making two waves simultaneously without using tone()

Hey everyone

Looking for some guidance with my project. I need to create two separate square waves at variable frequencies. The frequency will be determined after the arduino has deciphered the frequency of an input wave. I have already gotten the frequency deciphering completed, now I need to figure out how to output two square waves simultaneously.

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}

void loop()
{
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > ((1/infreq)/2)) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

The idea here is to create a 50% duty cycle by finding the period of the incoming wave and dividing by two to make the period for the new wave.
The problem with the code is that if I have, for example, a period of 3.3 for the incoming wave, I would divide this by two and use it as the inverval for the new wave.

This should return a frequency of 300hz, but on the scope I am only getting 246.23 hz.

Any idea on how I can fix the code, or any other ideas on how I can generate two waves simultaneously

Your code seems to be incomplete. I'm not sure where the second wave comes from, but if you want to output a logic-level square wave at double the frequency of an input logic level square wave then you could use interrupts or polling to measure the input frequency and hence calculate the output interval. At that frequency range, I'd use polling.

Given that polling would give you a value for the period rather than the frequency, you could simplify the code and reduce the execution time by halving the period rather than converting to frequency and back again.

The code you already have to do the output looks OK except that your timing doesn't account for the execution time of loop() so it will slip slightly. To avoid that, replace

previousMillis = currentMillis;

with

previousMillis += interval;

(Where unsigned long interval = your calculated half period for the output signal.)

You should understand, that arduino generate signal based on clock (16MHz) and integer divider, so you can't get 300 Hz sharp, there always would be round error.
Look here, nice table: Gammon Forum : Electronics : Microprocessors : Timers and counters

206:    77,295   12.938      9,662  103.500      1,208  828.000        302 3312.000         75 13248.000
207:    76,923   13.000      9,615  104.000      1,202  832.000        300 3328.000         75 13312.000
208:    76,555   13.062      9,569  104.500      1,196  836.000        299 3344.000         75 13376.000
209:    76,190   13.125      9,524  105.000      1,190  840.000        298 3360.000         74 13440.000

How accurate your outputs should be, probably it'd make sense to use hardware PLL multiplier like 74HC4046