please help - merging 2 pieces of code

hi there

i hope someone can please help me

i have two pieces of code and i need to merge them together and i am a bit stuck.

the first bit of code is arduino2max code which has been changed a little so that it reads values from 6 distance sensors on a wall which are chained together so that it repeatedly reads the values from the sensors in a loop and sends them to the arduino analogue pins

i am trying to incorporate some code for a pir sensor that is connected to a digital pin so that when the pir sensor detects motion it then turns on sound in max msp and if no motion is detected it turns the sound off.

hopefully this will solve a problem that i am having with the distance sensors which sometimes give false readings when no one is present and therefore make unwanted sounds

this is the arduino2max code :

int x = 0;                              // a place to hold pin values
int ledpin = 13;


void setup()
{
  Serial.begin(115200);               // 115200 is the default Arduino Bluetooth speed
  digitalWrite(13,HIGH);              ///startup blink
  delay(600);
  digitalWrite(13,LOW);
  pinMode(13,INPUT);

  delay(500);
  digitalWrite(12,HIGH);              // this part is the code to send the first pin in the                                      
  delay(20);                               //  chain high so that the sensors then give continuous reading                                        
  digitalWrite(12,LOW);            //    one after the other          
  pinMode(12, INPUT);     
  }    
          
                                   
                                          



void loop()
{

if (Serial.available() > 0){         // Check serial buffer for characters
        
    if (Serial.read() == 'r') {       // If an 'r' is received then read the pins
    
    for (int pin= 0; pin<=5; pin++){      // Read and send analog pins 0-5
    x = analogRead(pin);
    sendValue (x);
    }

    for (int pin= 2; pin<=13; pin++){     // Read and send digital pins 2-13
    x = digitalRead(pin);
    sendValue (x);
    }
  
    Serial.println();                 // Send a carriage returnt to mark end of pin data.
    delay (5);                        // add a delay to prevent crashing/overloading of the serial port
  
  }

 }
}

void sendValue (int x){              // function to send the pin value followed by a "space".
 Serial.print(x);
 Serial.print(32, BYTE);
}

and this is the pir sensor code, from elsewhere on this site

int ledPin = 13; // led connected to control pin 13
int PIRSensor = 2; // the PIR sensor will be plugged at digital pin 2
int state = 0; // variable to store the value read from the sensor pin
int statePin = LOW; // variable used to store the last LED status, to toggle the light

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(PIRSensor, INPUT); // declare the PIRSensor as as OUTPUT
Serial.begin(9600); // use the serial port
}

void loop() {
state = digitalRead(PIRSensor); // read the sensor and store it in "state"

if (state != 0)
{
digitalWrite(LED1, HIGH);
Serial.println("Motion Detected!"); // send the string "Motion Detected!" back // to the computer
}
else
digitalWrite(LED1, LOW); // turns light off if motion is not detected

delay(100); // we have to make a delay to avoid overloading the serial port
}

i hope that this is clear and someone can help me out as i am desperately trying to get this sorted out for an impending show

many thanks in advance for any advice

:slight_smile:

PLEASE do not crosspost.

  • Brian

im sorry, i wasnt sure i had posted in the relevant forum

So if I read you right, you are trying to make a sound when something is detected by the distance sensors, but ONLY if the PIR sensor finds something first?

If thats right, you should be able to reuse the code from the PIR sensor mostly. What you can do is say while(PIR == LOW) { do nothing}, else {read distance sensors, send data}. That way you remove the need to listen on the serial port. Alternatively, you could leave the Serial.available() statement and just put it inside of a if(PIR==HIGH) statement. Good luck, let me know if you don't understand!