Can someone help me combine these two scripts

I’ve tried but it never works properly and I don’t know where to place each item

LCD Interface (Script one (long one))

/* ------------------------------------------------
 * General Purpose computer serial IO -- ver 41  4/07
 * copyleft Stephen Wilson  http://userwww.sfsu.edu/~swilson/
 * Created October, 2006
 * The code activates most arduino functions
 * (digital in, digital out, analog in, analog out)
 * by sending ascii strings via serial communication
 * It can be used by any program that can send & receive strings.
 * For example, I use it in Director with the serial Xtra.
 * The code lets the Arduino board function as an I/O device for the computer
 * Your host program will need to prepare the strings to send
 * and extract the data from the incoming text
 *====
 * send "A" =  read Arduino Analog in pins into computer 
 * (Send ascii "A".  Arduino returns a sequence of
 * lines with ascii encoded number of the current analog pin value 0-1024 
 * eg  0 398
 *  1 599
 *  2 888
 *  3 0
 *  4 22
 *  5 222
 * and "$" to indicate end )
 *====
 * send "B"  = output analog values to arduino pins 9,10,11
 * compose a string with "B" followed by 3 ascii equivalents
 * (0-255) of values to be send
 * eg  "B"ascii(128)ascii(255)ascii(10)
 *==== 
 * Send "E" = read Digital pins in from arduino into computer 
 * You need to the adjust header to indicate which pin starts
 * the digital output seqquence. Eg declare pin 8 to start output 
 * means that 2-7 will  be input and 8-13 will be output.
 * Arduino returns a string starting with letter F
 * followed by H's and L's to indicate state followed by $ to end
 *  eg  FHLLLHH$  - pin  2=high  3= low etc...7=high)
 *====
 * Send "D" = send Digital from  computer to control arduino digital pins
 * adjust header indicates which range of digital pins to be declared output eg 8-13
 * Send ascii "D" followed sequence of letters to indicate desired values
 * (eg DHLLLHH  pin 8 = make high  9=low...13= high)
 *====
 //
 //
 //* This routine based on SERIAL COM and other examples
 //* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 04_function development
 //* by beltran berrocal  created 16 Decembre 2005;
 //* copyleft 2005 Progetto25zero1  <http://www.progetto25zero1.com>
 //* --------------------------------------------------- */

char serInString[11];  // array that will hold the  bytes of the incoming string. 
// you need to adjust so it can hold the max values to be expected
// eg if you declared 7 digital in it would be 8 to 
// include first id letter and the following 7 H's & L's
char serOutString[11]; 
// array to hold string of arduino read values of digital pin
char anOutString[6]; 
// array hold output of analog out -- not used

int val=0;  // temporary value for inputs & outputs
int val4=0;  //temp
char val2=2;
char val3=0;
char val1=0;
int p=0; // temporary value for pin
int digout = 8;  // start pin for digital out sequence
// here 8-13 will be output and 2-7 will be read in
// can be changed depending on your needs
int digseq = 6;  //temp value for number of output pins
int outPin = 12; // Output connected to digital pin 12
int mail = LOW; // Is there new mail?
int val9; // Value read from the serial port





//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char * strArray) {
  int i = 0;
  if(Serial.available()) {    
    //Serial.print("reading Serial String: ");  //optional: for confirmation
    while (Serial.available()){            
      strArray[i] = Serial.read();
      i++;
      // Serial.print(strArray[(i-1)]);         //optional: for confirmation
    }
    // Serial.println();                          //optional: for confirmation
  }      
}

// cleanout the arrays
void cleanup(char *strArray){
  for (int i=0; i= 8; i++){
    serInString[i]=0;
    serOutString[i]=0;
  }
}


//Print the whole string at once - will be performed only if thers is data inside it
//you must supply the array variable
void printSerialString(char *strArray) {
  int i=0;
  if (strArray[i] != 0) {     
    while(strArray[i] != 0) {
      //Serial.print( strArray[i] );
      strArray[i] = 0;                  // optional: flush the content
      i++;          
    }
  }
} 

//utility function to know wither an array is empty or not
boolean isStringEmpty(char *strArray) {
  if (strArray[0] == 0) {
    return true;
  } 
  else {
    return false;
  }
}

void setup() {
  Serial.begin(9600);
  // digout starts at digout (8 in this example)
  pinMode(outPin, OUTPUT); // sets the digital pin as output
  for (int i=digout; i <= 13; i++){
    pinMode(i, OUTPUT);
  }
  for (int i=2; i <= digout-1; i++){
    pinMode(i, INPUT);
  }
 mail = LOW; // start off with lights out
 Serial.begin(9600);
 Serial.flush();
}



void loop () {
  //simple feedback from Arduino that it is working - optional
  //Serial.println("Hello World");

  //read the serial port and create a string out of what you read
  readSerialString(serInString);
  if (Serial.available())
   {
     val9 = Serial.read();
     Serial.println(val9, BYTE);

     if (val9 == 110) // n = 110 in dec
     {
       mail = LOW;  // HIGH is off because led is connected to +5v on the other side
       digitalWrite(outPin, mail);
     }

     else if (val9 == 109) //109 = m in dec
     {
       mail = HIGH; // LOW is on because led is connected to +5v on the other side
       digitalWrite(outPin, mail);
     }
   }
   digitalWrite(outPin, mail);
   delay(1000);



  if( isStringEmpty(serInString) == false) {
    //it will do the following if there actually is some info

    //--------
    // "D" = code for digital out to arduino looks for D as first 
    // character in the input string (ascii 68)
    // then looks for H & L letters
    // for example computer sends DHLHHLL
    if (serInString[0]==68)  
      digseq= 13-digout+1; // how many output pins

    {

      for (int i=1; i <= digseq; i++){
        p = i+digout-1;  // output to the right pins
        // for example if output starts at pin 8
        // then second item in string will be applied to pin 1+7
        val=serInString[i];  // what is the letter
        // if it is an H (high - ascii 72- then subtract 71 to make high)
        // 
        // if it is a L (low - ascii 76- then subtract 76 to make low)
        if (val == 72)
        { 
          val = 1;
        }
        else
        {
          val=0;
        }

        digitalWrite(p, val);// output to the pin

      } 
      //Serial.println("digoutconfirm"); 
    }


    //-------
    // "E" (ascii69) = code for read & send arduino digital state to computer 
    if (serInString[0]==69)  // just send an E from computer

    {

      serOutString[0] =70;  // F for return to computer
      // read pins from 2 to one below digout
      serOutString[1] =65;

      for (int i=2; i <= digout-1; i++){
        val = digitalRead(i);
        if (val == HIGH)
        {
          //val = digitalRead(i);  
          //val4=i-1;
          serOutString[i-1]=72; //H for high
          // Serial.println("high");
        }

        if (val == LOW) 
        {
          // val = digitalRead(i);  
          //val4=i-1;
          serOutString[i-1]=76; //L for low
          // Serial.println("low");
        }




      } 

      Serial.print(serOutString); 
      Serial.println("$");// for end
    }

    //-----------
    // A (asci65)  = code for send analog read pins to computer
    if (serInString[0]==65)  
    {
      //Serial.println("F");
      for (int i=0; i <= 5; i++){
        val = analogRead(i); 
        val2=val/4;
        anOutString[i]=val2;
        Serial.println(i);
        Serial.println(val,DEC); 
        //Serial.println(val2,DEC);  // 1 byte version 
      } 


      //Serial.println(anOutString);
      Serial.println("$");
    }

    //----------
    // B = code for analog sent to control arduino analog out
    if (serInString[0]==66)  
    {
      val1=serInString[1];  //pwm0 - pin9
      val2=serInString[2]; //pwm1 - pin10
      val3=serInString[3];  //pwm2 - pin11
      //Serial.println(val1);
      // Serial.println(val2);
      //Serial.println(val3);
      analogWrite(9,val1);
      analogWrite(10,val2);
      analogWrite(11,val3);
      //Serial.println("anaconfirm");
    }


    //  Serial.println();
  }





  // Serial.println();  
  //cleans out arrays
  for (int i=0; i <= 11; i++){

    serInString[i]=0;
    serOutString[i]=0;
    anOutString[i]=0;

  }
  //slows down the visualization in the terminal
  delay(200);
}

motion detector script (script two (short one))

void setup()
{
pinMode(5, OUTPUT);
pinMode(8, INPUT);
digitalWrite(5, LOW);
}

int pinin = 0;
long countint = 0;

void loop()
{
pinin = digitalRead(8);
while (pinin == 0) {
pinin = digitalRead(8);
}
Serial.println("motion");
digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);
}

I am kind of newb I’ve been using 2 different arduinos for this and I would be much easier to use one

if necessary the

digitalWrite(5, HIGH);
delay(500);
digitalWrite(5, LOW);

in the motion detector script dosn’t have to be there if it causes problems, the only thing I really need is the

Serial.println(“motion”);

in there

The setup method in the 1st script has two calls to Serial.begin. It only needs one.

Move the code from the setup method in the second script into the setup method in the first script. Where isn't important.

Move the code between the setup and loop methods, in the second script to the top of the 1st script. Precise position isn't too important.

The code in the loop method in the second script can go at the end of the loop method in the first script, or it can go at the beginning. If it goes at the end, the check for, and processing of, serial data will happen before the sensor on pin 8 goes high. I think, though, that pin 8 will conflict with the LCD, because that pin is used by the LCD (at least according to the 1st script).

If the code in the second script goes at the start of loop, nothing will happen until the pin 8 sensor goes HIGH. When it does, pin 5 will be set HIGH, 1/2 a second will be wasted, pin 5 will be set LOW, and then the serial check, read, and processing will be performed.

how can I make the serial check, read, and processing happen regardless if the motion sensor has been activated yet, because i want that to happen continuously and if the motion sensor goes off I want that to go in between while the other code is still going off, or is there any pin I can use on the arduino Duemilanov that isn't already being utilized for the lcd so It wont conflict?

would so kind of a DoWhile allow these to all work together?

If you want your code to process the serial data continuously, until the motion sensor has triggered an event, you will need to use an interrupt handler.

http://arduino.cc/en/Reference/AttachInterrupt

The interrupt handler allows your loop code to process normally, until the motion sensor yells "Yoohoo, look at me!". The interrupt handler gets called to "Look at me!".

You need to keep what happens in the interrupt handler short and fast - no calls to Serial.print, no calls to delay.

The best thing to do in the interrupt handler is just set a flag that says motion-has-happened. Then, in loop, see if motion-has-happened is true, deal with it, and then set it false again.

so I would include this in my script then, using that function.

int pin = 5;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(8, blink, CHANGE);
}

void loop()
{
  digitalWrite(pin, state);
  if state = HIGH
      Serial.println("motion");
}

void blink()
{
  state = !state;
}

The interrupts are handled for pins 2 and 3 only, on the Duemilanove, and they are numbered 0 and 1. There is no interrupt 8.

The "if state = HIGH" statement won't compile.

The CHANGE argument in the attachInterrupt function will cause an interrupt whenever the state changes. If you just want to know when there is motion, change CHANGE to HIGH. Your handler will, then, only be called when there is motion.

Then, the "if state = HIGH" statement can be corrected to "if(state == HIGH)".

In the handler, change "state = !state" to "state = HIGH". In loop, add {} after the if statement. Inside the braces, add "state = LOW;".

Otherwise, this code will work.

okay so last time to double check you mean this

int pin = 5;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(1, blink, HIGH);
}

void loop()
{
  digitalWrite(pin, state);
  if(state == HIGH){
      state = LOW;
      Serial.println("motion");
  }
}

void blink()
{
  state = HIGH;
}

and my full code should look like this

/* ------------------------------------------------
 * General Purpose computer serial IO -- ver 41  4/07
 * copyleft Stephen Wilson  http://userwww.sfsu.edu/~swilson/
 * Created October, 2006
 * The code activates most arduino functions
 * (digital in, digital out, analog in, analog out)
 * by sending ascii strings via serial communication
 * It can be used by any program that can send & receive strings.
 * For example, I use it in Director with the serial Xtra.
 * The code lets the Arduino board function as an I/O device for the computer
 * Your host program will need to prepare the strings to send
 * and extract the data from the incoming text
 *====
 * send "A" =  read Arduino Analog in pins into computer 
 * (Send ascii "A".  Arduino returns a sequence of
 * lines with ascii encoded number of the current analog pin value 0-1024 
 * eg  0 398
 *  1 599
 *  2 888
 *  3 0
 *  4 22
 *  5 222
 * and "$" to indicate end )
 *====
 * send "B"  = output analog values to arduino pins 9,10,11
 * compose a string with "B" followed by 3 ascii equivalents
 * (0-255) of values to be send
 * eg  "B"ascii(128)ascii(255)ascii(10)
 *==== 
 * Send "E" = read Digital pins in from arduino into computer 
 * You need to the adjust header to indicate which pin starts
 * the digital output seqquence. Eg declare pin 8 to start output 
 * means that 2-7 will  be input and 8-13 will be output.
 * Arduino returns a string starting with letter F
 * followed by H's and L's to indicate state followed by $ to end
 *  eg  FHLLLHH$  - pin  2=high  3= low etc...7=high)
 *====
 * Send "D" = send Digital from  computer to control arduino digital pins
 * adjust header indicates which range of digital pins to be declared output eg 8-13
 * Send ascii "D" followed sequence of letters to indicate desired values
 * (eg DHLLLHH  pin 8 = make high  9=low...13= high)
 *====
 //
 //
 //* This routine based on SERIAL COM and other examples
 //* SERIAL COM - HANDELING MULTIPLE BYTES inside ARDUINO - 04_function development
 //* by beltran berrocal  created 16 Decembre 2005;
 //* copyleft 2005 Progetto25zero1  <http://www.progetto25zero1.com>
 //* --------------------------------------------------- */

char serInString[11];  // array that will hold the  bytes of the incoming string. 
// you need to adjust so it can hold the max values to be expected
// eg if you declared 7 digital in it would be 8 to 
// include first id letter and the following 7 H's & L's
char serOutString[11]; 
// array to hold string of arduino read values of digital pin
char anOutString[6]; 
// array hold output of analog out -- not used

int val=0;  // temporary value for inputs & outputs
int val4=0;  //temp
char val2=2;
char val3=0;
char val1=0;
int p=0; // temporary value for pin
int digout = 8;  // start pin for digital out sequence
// here 8-13 will be output and 2-7 will be read in
// can be changed depending on your needs
int digseq = 6;  //temp value for number of output pins
int outPin = 12; // Output connected to digital pin 12
int mail = LOW; // Is there new mail?
int val9; // Value read from the serial port
int pinin = 0;
long countint = 0;
int redledpin = 5;
volatile int state = LOW;





//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char * strArray) {
  int i = 0;
  if(Serial.available()) {    
    //Serial.print("reading Serial String: ");  //optional: for confirmation
    while (Serial.available()){            
      strArray[i] = Serial.read();
      i++;
      // Serial.print(strArray[(i-1)]);         //optional: for confirmation
    }
    // Serial.println();                          //optional: for confirmation
  }      
}

// cleanout the arrays
void cleanup(char *strArray){
  for (int i=0; i= 8; i++){
    serInString[i]=0;
    serOutString[i]=0;
  }
}


//Print the whole string at once - will be performed only if thers is data inside it
//you must supply the array variable
void printSerialString(char *strArray) {
  int i=0;
  if (strArray[i] != 0) {     
    while(strArray[i] != 0) {
      //Serial.print( strArray[i] );
      strArray[i] = 0;                  // optional: flush the content
      i++;          
    }
  }
} 

//utility function to know wither an array is empty or not
boolean isStringEmpty(char *strArray) {
  if (strArray[0] == 0) {
    return true;
  } 
  else {
    return false;
  }
}

void setup() {
  Serial.begin(9600);
  // digout starts at digout (8 in this example)
  pinMode(outPin, OUTPUT); // sets the digital pin as output
  for (int i=digout; i <= 13; i++){
    pinMode(i, OUTPUT);
  }
  for (int i=2; i <= digout-1; i++){
    pinMode(i, INPUT);
  }
 mail = LOW; // start off with lights out
 pinMode(redledpin, OUTPUT);
 attachInterrupt(1, blink, HIGH);
 digitalWrite(5, LOW);
 Serial.flush();
}



void loop () {
  //simple feedback from Arduino that it is working - optional
  //Serial.println("Hello World");

  //read the serial port and create a string out of what you read
  digitalWrite(redledpin, state);
  if(state == HIGH)
  {
   state = LOW;
   Serial.println("motion");
  }
  readSerialString(serInString);
  if (Serial.available())
   {
     val9 = Serial.read();
     Serial.println(val9, BYTE);

     if (val9 == 110) // n = 110 in dec
     {
       mail = LOW;  // HIGH is off because led is connected to +5v on the other side
       digitalWrite(outPin, mail);
     }

     else if (val9 == 109) //109 = m in dec
     {
       mail = HIGH; // LOW is on because led is connected to +5v on the other side
       digitalWrite(outPin, mail);
     }
   }
   digitalWrite(outPin, mail);
   delay(1000);



  if( isStringEmpty(serInString) == false) {
    //it will do the following if there actually is some info

    //--------
    // "D" = code for digital out to arduino looks for D as first 
    // character in the input string (ascii 68)
    // then looks for H & L letters
    // for example computer sends DHLHHLL
    if (serInString[0]==68)  
      digseq= 13-digout+1; // how many output pins

    {

      for (int i=1; i <= digseq; i++){
        p = i+digout-1;  // output to the right pins
        // for example if output starts at pin 8
        // then second item in string will be applied to pin 1+7
        val=serInString[i];  // what is the letter
        // if it is an H (high - ascii 72- then subtract 71 to make high)
        // 
        // if it is a L (low - ascii 76- then subtract 76 to make low)
        if (val == 72)
        { 
          val = 1;
        }
        else
        {
          val=0;
        }

        digitalWrite(p, val);// output to the pin

      } 
      //Serial.println("digoutconfirm"); 
    }


    //-------
    // "E" (ascii69) = code for read & send arduino digital state to computer 
    if (serInString[0]==69)  // just send an E from computer

    {

      serOutString[0] =70;  // F for return to computer
      // read pins from 2 to one below digout
      serOutString[1] =65;

      for (int i=2; i <= digout-1; i++){
        val = digitalRead(i);
        if (val == HIGH)
        {
          //val = digitalRead(i);  
          //val4=i-1;
          serOutString[i-1]=72; //H for high
          // Serial.println("high");
        }

        if (val == LOW) 
        {
          // val = digitalRead(i);  
          //val4=i-1;
          serOutString[i-1]=76; //L for low
          // Serial.println("low");
        }




      } 

      Serial.print(serOutString); 
      Serial.println("$");// for end
    }

    //-----------
    // A (asci65)  = code for send analog read pins to computer
    if (serInString[0]==65)  
    {
      //Serial.println("F");
      for (int i=0; i <= 5; i++){
        val = analogRead(i); 
        val2=val/4;
        anOutString[i]=val2;
        Serial.println(i);
        Serial.println(val,DEC); 
        //Serial.println(val2,DEC);  // 1 byte version 
      } 


      //Serial.println(anOutString);
      Serial.println("$");
    }

    //----------
    // B = code for analog sent to control arduino analog out
    if (serInString[0]==66)  
    {
      val1=serInString[1];  //pwm0 - pin9
      val2=serInString[2]; //pwm1 - pin10
      val3=serInString[3];  //pwm2 - pin11
      //Serial.println(val1);
      // Serial.println(val2);
      //Serial.println(val3);
      analogWrite(9,val1);
      analogWrite(10,val2);
      analogWrite(11,val3);
      //Serial.println("anaconfirm");
    }


    //  Serial.println();
  }





  // Serial.println();  
  //cleans out arrays
  for (int i=0; i <= 11; i++){

    serInString[i]=0;
    serOutString[i]=0;
    anOutString[i]=0;

  }
  //slows down the visualization in the terminal
  delay(200);
}
void blink()
{
  state = HIGH;
}

The first sketch looks correct. The 2nd sketch also looks good. Try it and you’ll know.

You did (will) move the sensor to pin 3, right?

everything is perfect, thank you