makey makey code not working with sonar

hello , I am working on a project that uses sonar and an anlouge reading makey makey style circuit/coding to create a contact based door switch. strangely I have found that the both codes work independantly but when used in conjunction the reading for A0
will climb instead of reaching their contact and no contact values instantly and this creates a delay of a few seconds, I have tried removing all delays from the code and this has not helped. I wasn’t really sure if this was a programming issue but thought i’D ask here just in case , cheers.

heres my code.

#include <MovingAvarageFilter.h>

MovingAvarageFilter movingAvarageFilter1(20);



/* Ping))) SensorJJJJJJJJJJJJJJJJJLJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
 
 This sketch reads a PING))) ultrasonic rangefinder and returns the
 distance to the closest object in range. To do this, it sends a pulse
 to the sensor to initiate a reading, then listens for a pulse 
 to return.  The length of the returning pulse is proportional to 
 the distance of the object from the sensor.
 
 The circuit:
 	* +V connection of the PING))) attached to +5V
 	* GND connection of the PING))) attached to ground
 	* SIG connection of the PING))) attached to digital pin 7
 
 http://www.arduino.cc/en/Tutorial/Ping
 
 created 3 Nov 2008
 by David A. Mellis
 modified 30 Aug 2011
 by Tom Igoe
 hhhh
 This example code is in the public domain.ASSSSSSSADA
 
 */

// this constant won't change.  It's the pin number
// of the sensor's output:

boolean check1 = false;  

boolean pressed1a = false;

boolean pressed1b = false;

boolean pressed2a = false;

boolean pressed2b = false;

boolean pressed3a = false;

boolean pressed3b = false;




const int pingPin = 4;
const int pingPin1 = 2;
const int pingPin2 = 13;
const int pingPin3 = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm  , duration1,duration1b, inches1a,inches1b ,  cm1a,cm1b, duration2,duration2b, inches2a, inches2b, cm2a,cm2b, duration3,duration3b, inches3a,inches3b, cm3a,cm3b;
  float output1 = movingAvarageFilter1.process(analogRead(0));

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  delay (20);



  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin1, OUTPUT);
  digitalWrite(pingPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin1, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin1, LOW);

  pinMode(pingPin1, INPUT);
  duration1 = pulseIn(pingPin1, HIGH);

  delay (20);

  pinMode(pingPin2, OUTPUT);
  digitalWrite(pingPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin2, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin2, LOW);

  pinMode(pingPin2, INPUT);
  duration2 = pulseIn(pingPin2, HIGH);

  delay (20);

  pinMode(pingPin3, OUTPUT);
  digitalWrite(pingPin3, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin3, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin3, LOW);

  pinMode(pingPin3, INPUT);
  duration3 = pulseIn(pingPin3, HIGH);






  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.


  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  inches1a = microsecondsToInches(duration1);
  cm1a= microsecondsToCentimeters(duration1);

  inches1b = microsecondsToInches(duration1);
  cm1b= microsecondsToCentimeters(duration1);


  inches2a = microsecondsToInches(duration2);
  cm2a= microsecondsToCentimeters(duration2);

  inches2b = microsecondsToInches(duration2);
  cm2b= microsecondsToCentimeters(duration2);

  inches3a = microsecondsToInches(duration3);
  cm3a= microsecondsToCentimeters(duration3);

  inches3b = microsecondsToInches(duration3);
  cm3b= microsecondsToCentimeters(duration3);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  Serial.print(inches1a);
  Serial.print("in, ");
  Serial.print(cm1a);
  Serial.print("cm");
  Serial.println();

  Serial.print(inches2a);
  Serial.print("in, ");
  Serial.print(cm2a);
  Serial.print("cm");
  Serial.println();

  Serial.print(inches3a);
  Serial.print("in, ");
  Serial.print(cm3a);
  Serial.print("cm");
  Serial.println();


  if (inches1a >=6.00 && inches1a <=24.00) {
    if (!pressed1a){
      Keyboard.press ('h');
      Keyboard.press(KEY_CAPS_LOCK);
      pressed1a = !pressed1a;

    }
  }

  if ( inches1a >= 24.01 || inches1a <=5.99){
    if (pressed1a){
      Keyboard.release ('h');
      pressed1a = !pressed1a;

    }
  }
  if (inches2a >=6.00 && inches2a <=24.00) {
    if (!pressed2a){
      Keyboard.press ('j');
      Keyboard.press(KEY_CAPS_LOCK);
      pressed2a = !pressed2a;

    }
  }

  if ( inches2a >= 24.01 || inches2a <=5.99){
    if (pressed2a){
      Keyboard.release ('j');
      pressed2a = !pressed2a;

    }
  }
  if (inches3a >=6.00 && inches3a <=24.00) {
    if (!pressed3a){
      Keyboard.press ('k');
      Keyboard.press(KEY_CAPS_LOCK);
      pressed3a = !pressed3a;

    }
  }

  if ( inches3a >= 24.01 || inches3a <=5.99){
    if (pressed3a){
      Keyboard.release ('k');
      pressed3a = !pressed3a;

    }
  }
  if ( inches >= 0.00 && inches  <= 7.00)  {  

    Keyboard.print ("w");

  }

  if ( inches >= 7.01 && inches  <= 14.00)  {  

    Keyboard.print ("e");

  }
  if ( inches >= 14.01 && inches  <= 21.00)  {  

    Keyboard.print ("t");

  }
  if ( inches >= 21.01 && inches  <= 28.00)  {  

    Keyboard.print ("y");

  }
  if ( inches >= 28.01 && inches  <= 35.00)  {  

    Keyboard.print ("u");

  }
  if ( inches >= 35.01 && inches  <= 42.00)  {  

    Keyboard.print ("o");

  }




  if (output1 < 690 ) {   // you can change this parameter to fine tune the sensitivity
    if (!check1){         
      Keyboard.press(KEY_CAPS_LOCK);
      Keyboard.release ('f');     
      Serial.println(output1);           
      check1 = !check1;   
    }         
  }


  if (output1 >700) {     
    if (check1){ 
      Keyboard.press ('f');     
      check1 = !check1;   
    }     
  }
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;


}
const int pingPin = 4;
const int pingPin1 = 2;
const int pingPin2 = 13;
const int pingPin3 = 7;

You got something against arrays?

MovingAvarageFilter movingAvarageFilter1(20);

Perhaps you need a longer name… 8)

What is the 1 for?

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

Give me one good reason why this code is in loop(), instead of in a function that takes a pin number as it’s argument?

You use nearly identical code, three more times, with the pin number and where to store the value as the only differences.

duration = pin(pingPin);
duration1 = pin(pingPin1);
duration2 = pin(pingPin2);
duration3 = pin(pingPin3);

would be so much shorter.

And, using arrays would make it even easier:

for(byte i=0; i<PING_COUNT; i++)
{
   duration[i] = ping(pingPin[i]);
}
  cm1a= microsecondsToCentimeters(duration1);

You never use this value. Why are you computing it?

Why does that function exist in your code, at all?

  if (inches1a >=6.00 && inches1a <=24.00) {

inches1a (Use arrays, damn it!) is a long. Why are you comparing it to floats?

OK. Now to your problem.

and an anlouge reading makey makey style circuit

Some clue as to what a “makey makey style circuit” is would be useful. Some clue as to what an “anlouge” reading is would be good.

If that is supposed to be analog, what is connected to the pin?

I think a lot of your complaints have to go to the authors of the sample code that the OP is using, not the OP. The lack of arrays and the lack of a function to set the pins is all in the example.

PaulS: MovingAvarageFilter movingAvarageFilter1(20);

What is the 1 for?

To avoid namespace conflicts with the MovingAvarageFilter class? Personally, I would just have done "MovingAvarageFilter myFilter" or something. But I get it.

essentially the goal of the project I'm working on has changed allot (I'm working for someone else) and iv'e been qiute lazy and left allot of things in when I should have deleted them after they were no longer in use.

the makey makey circuit is in my case one wire connected to ground and soldered at the other end to a metal door frame. the other half of the circuit is connected to 5v which connects to a 1meg ohm resistor which is then soldered to a metal door handle which is read at A0, basically when the circuit is closed A0 reads at 100 and when is is open it reads 1000. this works fine as a standalone piece of code by when it is included in the code for the sonars , the values climb slowly to their two states and after removing every delay I cannot see why this is the case.

in any case It doesn't matter because i'm now using another rangefinder to tell whether the door is open or not. would be good if someone could shed light on the problem though.

ps I know the sonars wouldn't work with pw if I took out every delay it was just a process of elimination thing to see if that was the reason the makey makey circuit wasn't working (qiute pointless as the makey makey code works fine with delays)

the makey makey circuit is in my case one wire connected to ground and soldered at the other end to a metal door frame. the other half of the circuit is connected to 5v which connects to a 1meg ohm resistor which is then soldered to a metal door handle which is read at A0

How was the 1meg Ohm value selected? What I see you describing is a digital circuit. The door is either open or it isn't. You are using analogRead() to read a digital value. I can't figure out why.

your right the outcome is either one or the other , the advantage of the makey makey cicuit is that it can be connected by anything conductive. in our case this was useful because we could disguise the two wires connecting to the door handle and frame (both are metal , our project is "re-inventing" portaloo's ) with more ease than we could have disguised a reed switch.

I don't have a good enough grasp of the technical side of it to know why the 1 mega ohms resistor is the correct one , I'm just copying this

http://www.youtube.com/watch?v=WDPTA0-fbNE

i think choice of resistor may bear some relation to the objective of making the circuit completeable with the human body