error when trying to merge two pieces of code

hi there

i hope someone can please help me, i have posted this in the syntax and programs part of the forum but maybe here is a better place

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 and from there into max msp

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);    
  }

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 have tried to merge the two so that the arduino first checks the pir sensor then goes through a loop of the distance sensors then goes back to check the pir sensor again, this is what i have managed so far

int x = 0;                              // a place to hold pin values
int ledpin = 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(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()
{
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
}

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);
}

but when i try and compile it i get an error "ledPin was not declared in this scope in function void loop".
this is quite beyond my newbieness and i am struggling to understand.

can anyone help me out please?

many thanks

ledpin and ledPin are different variables. You declared ledpin, but are trying to use ledPin in setup()

:slight_smile: thanks for that, i’ll check it out tonight and see if it works or if any more errors occur

thanks again