Square Wave In/Square Wave Out = Electrical Interference

I have a project where I am inputting 2 square waves into the Atmega 328, calculating the frequency of these two signals using the "pulseIn" function and some math, deciding which signal is the lowest frequency, and then outputting a new square wave signal at this lower frequency using the "tone" function. There is a summing amplifier on the output side of the circuit as well.

The program is doing what I ask it to do. However, the problem I am facing is I am getting some electrical interference between the input and output signals that is distorting all the square waves. I need some help to get rid of this interference.

Things I have tried: low pass filters/high pass filters, shielding wires with aluminum foil, twisting wires around each other, moving the input/output wires far away from each other, installing capacitors between power and ground on micro and op-amp, grounding unused pins on the microcontroller.

I have also just bought some shielded wire so I will be trying that soon.

Any ideas would be helpful. Thanks in advance

Here is my code:

int freqInputLeft = 4;  //pin to read the new sensor wave at 0-5V (left wheel)
int freqInputRight = 6;  //pin to read the new sensor wave at 0-5V (right wheel)
float pulseLengthLeft = 0; //initialize pulse length (left wheel)
float freqLeft = 0; //initialize frequency (left wheel)
float pulseLengthRight = 0; //initialize pulse length (right wheel)
float freqRight = 0; //initialize frequency (right wheel)
int FinalOutput = 12; //pin to output the modified square wave


void setup() {
 //Serial.begin(9600);   // setup serial
  pinMode(freqInputLeft, INPUT);
  pinMode(freqInputRight, INPUT);
  pinMode(FinalOutput, OUTPUT);
  
}

void loop() {
  pulseLengthLeft = pulseIn(freqInputLeft, HIGH, 15000); //measure the pulse length of the left sensor
  freqLeft = 1/(2*pulseLengthLeft*.000001); //convert left pulse length to freq in Hz
  
  pulseLengthRight = pulseIn(freqInputRight, HIGH,15000); //measure the pulse length of the right sensor
  freqRight = 1/(2*pulseLengthRight*.000001); //convert right pulse length to freq in Hz
 
//  if (pulseLengthRight > 0) { 
//      tone(FinalOutput, freqRight);
//      //Serial.println(23);
//  }
//
//    else {
//      noTone(FinalOutput);
//      //Serial.println(10);
//    }
 
 
  //Serial.println(""); 
 // Serial.println(freqLeft);
  //Serial.println(freqRight);
 // Serial.println(""); 

 if (pulseLengthLeft && pulseLengthRight > 0) { 
  if (freqRight/freqLeft < .9) {
    tone(FinalOutput, freqRight);

  }
    else{
     tone(FinalOutput, freqLeft);

    }
 }
    else {
      noTone(FinalOutput);
    }

}

However, the problem I am facing is I am getting some electrical interference between the input and output signals that is distorting all the square waves.

What evidence do you have for this?
What are the sources of the "square waves" and how do you have them wired to the Arduino?

Please post a schematic diagram of the entire circuit.

My evidence is oscilloscope readings of the input and output square waves. If I edit my program to not output anything the input square wave looks great, and if I edit the program to output a square wave with no input wired in the output square wave looks great. However, when they are both trying in input and output together, both square waves are distorted. See the scope pic attached. Top in input bottom is output.

The sources of the input square waves are wheel speed sensors. See the circuit diagram attached

Thanks

Your attachments as embedded images (technique described here):

Sigh... Leave it to Fritzing to make even a "standard" schematic very difficult to read. What a disaster!

Evidently, the concept of a ground symbol has escaped the developers.

OP: I don't know what to make of the scope signal. The difficult-to-read schematic offers no obvious clue. I suspect that either the circuit is actually not wired according to the schematic, and/or the scope leads are picking up interference, possibly due to a floating ground connection.

Edit: ALL unused inputs and outputs on op amps should be connected somewhere or the amplifiers may oscillate. One practice is to connect the output to the - input and the + input to ground.

Thank you for the replies.

Yes, I agree Fritzing is complicated to make the schematic not confusing. As a matter of fact, I just noticed an error (R8 was wired wrong) in the Fritzing schematic I previously posted. Attached is the updated Fritzing schematic and a less confusing handwritten schematic. Hope this helps people decipher the schematic.

jremington the I/O connections you suggest is a good idea I have not tried yet. The quad amp is all I have on hand, and I didn't know what to do with the other three amps. I will try this, but it may be a few days before I have my test apparatus put back together.

Any other ideas would be great. Thanks again.

Drop Fritzing. Your hand-drawn schematic is MUCH better.

I recommend Schemeit for ultraclear, professional schematics.