I have multiple sketches and i want to combine them in one sketch

Hi all,

this is my first time at working with the Arduino and im loving it.

Im just having trouble at trying to run several things at the same time.

I have multiple sketches and i want to combine them in one sketch

sketch 1
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 5 // Configurable, see typical pin layout above
#define SS_PIN 53 // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

String read_rfid;
String ok_rfid_1=“aaaaaaa”;
//String ok_rfid_2=“ffffffff”; //add as many as you need.
int lock=7; //Which pin the lock will be on if using a relay or solenoid or similar

//Use the lines below if you plan on using a servo as a locking mechanism.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int posClosed = 0; // variable to store the servo position for locked
int posOpen = 360; //same for open…

void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card

myservo.attach(49); // attaches the servo on pin 2 to the servo object
}

/*

  • Helper routine to dump a byte array as hex values to Serial.
    /
    void dump_byte_array(byte buffer, byte bufferSize) {
    read_rfid="";
    for (byte i = 0; i < bufferSize; i++) {
    read_rfid=read_rfid + String(buffer
    , HEX);
  • }*
    }
    void open_lock() {
  • myservo.write(0);*
  • delay(6000);*
  • myservo.write(90);*
    }
    void loop() {
  • // Look for new cards*
  • if ( ! mfrc522.PICC_IsNewCardPresent())*
  • return;*
  • // Select one of the cards*
  • if ( ! mfrc522.PICC_ReadCardSerial())*
  • return;*
  • dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);*
  • Serial.println(read_rfid);*
  • if (read_rfid==ok_rfid_1) {*
  • //ok, open the door.*
  • open_lock();*
  • }*

}
sketch 2
// Digital pin 8 will be called ‘pin8’
int buzzer = 35;
// Analog pin 0 will be called ‘sensor’
int sensor = A1;
// Set the initial sensorValue to 0
int sensorValue = 0;
// The setup routine runs once when you press reset
void setup() {

  • // Initialize the digital pin 8 as an output*
  • pinMode(buzzer, OUTPUT);*
  • // Initialize serial communication at 9600 bits per second*
  • Serial.begin(9600);*
    }
    // The loop routine runs over and over again forever
    void loop() {
  • // Read the input on analog pin 0 (named ‘sensor’)*
  • sensorValue = analogRead(sensor);*
  • // Print out the value you read*
  • Serial.println(sensorValue, DEC);*
  • // If sensorValue is greater than 500*
  • if (sensorValue > 1000) {*
  • // Activate digital output pin 8 - the LED will light up*
  • tone(buzzer, 1000);*
  • }*
  • else {*
  • // Deactivate digital output pin 8 - the LED will not light up*
  • noTone(buzzer);*
  • }*
    }
    sketch 3
    int PirSensor=41 ;
    void setup() {
  • // put your setup code here, to run once:*
    pinMode(PirSensor,INPUT);
    pinMode(40,OUTPUT);
    Serial.begin(9600);
    }
    void loop() {
  • // put your main code here, to run repeatedly:*
    int sensorValue=digitalRead(PirSensor);
    if (sensorValue == HIGH){
  • digitalWrite(40,HIGH);*
  • Serial.println(“Motion Detected”);*
  • delay(500);*
  • }*
  • else*
  • {*
  • digitalWrite(40,LOW);*
  • Serial.println(“No Motion”);*
  • delay(500);*
  • }*
    }

And now with code tags, please.

That was easy:

void setup() {
  setup1();
  setup2();
  setup3();
}

void loop() {
  loop1();
  loop2();
  loop3();
}

#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN         5           // Configurable, see typical pin layout above
#define SS_PIN          53          // Configurable, see typical pin layout above

MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.

String read_rfid;
String ok_rfid_1 = "aaaaaaa";
//String ok_rfid_2="ffffffff"; //add as many as you need.
int lock = 7; //Which pin the lock will be on if using a relay or solenoid or similar

//Use the lines below if you plan on using a servo as a locking mechanism.
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int posClosed = 0;    // variable to store the servo position for locked
int posOpen = 360;    //same for open...

void setup1() {
  Serial.begin(9600);         // Initialize serial communications with the PC
  while (!Serial);            // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin();                // Init SPI bus
  mfrc522.PCD_Init();         // Init MFRC522 card


  myservo.attach(49);  // attaches the servo on pin 2 to the servo object
}

/*
   Helper routine to dump a byte array as hex values to Serial.
*/
void dump_byte_array(byte *buffer, byte bufferSize) {
  read_rfid = "";
  for (byte i = 0; i < bufferSize; i++) {
    read_rfid = read_rfid + String(buffer[i], HEX);
  }
}

void open_lock() {
  myservo.write(0);
  delay(6000);
  myservo.write(90);
}

void loop1() {

  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent())
    return;

  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial())
    return;

  dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  Serial.println(read_rfid);
  if (read_rfid == ok_rfid_1) {
    //ok, open the door.
    open_lock();
  }

}


// Digital pin 8 will be called 'pin8'
int buzzer = 35;
// Analog pin 0 will be called 'sensor'
int sensor = A1;
// Set the initial sensorValue to 0
int sensorValue = 0;

// The setup routine runs once when you press reset
void setup2() {
  // Initialize the digital pin 8 as an output
  pinMode(buzzer, OUTPUT);
  // Initialize serial communication at 9600 bits per second
  Serial.begin(9600);
}

// The loop routine runs over and over again forever
void loop2() {
  // Read the input on analog pin 0 (named 'sensor')
  sensorValue = analogRead(sensor);
  // Print out the value you read
  Serial.println(sensorValue, DEC);
  // If sensorValue is greater than 500
  if (sensorValue > 1000) {
    // Activate digital output pin 8 - the LED will light up
    tone(buzzer, 1000);
  }
  else {
    // Deactivate digital output pin 8 - the LED will not light up
    noTone(buzzer);
  }

}

int PirSensor = 41 ;

void setup3() {
  // put your setup code here, to run once:
  pinMode(PirSensor, INPUT);
  pinMode(40, OUTPUT);
  Serial.begin(9600);
}

void loop3() {
  // put your main code here, to run repeatedly:
  int sensorValue = digitalRead(PirSensor);
  if (sensorValue == HIGH) {
    digitalWrite(40, HIGH);
    Serial.println("Motion Detected");
    delay(500);
  }
  else
  {
    digitalWrite(40, LOW);
    Serial.println("No Motion");
    delay(500);
  }
}

thank's a lot Mr johnwasser...
i have 2 more sketchs , can i use the same methode ?? thank's again

ramzijsk:
thank's a lot Mr johnwasser...
i have 2 more sketchs , can i use the same methode ?? thank's again

Yes, you can add more sketches as long as they don't have name conflicts, or hardware conflicts.
Note that all of the loop() functions must return quickly (like BlinkWithoutDelay example, not Blink example) so that each loop() gets a fair share of the processor's time. If you have any delay() or other functions that take longer than a few milliseconds your sketches will not be very responsive.

johnwasser:
Yes, you can add more sketches as long as they don’t have name conflicts, or hardware conflicts.
Note that all of the loop() functions must return quickly (like BlinkWithoutDelay example, not Blink example) so that each loop() gets a fair share of the processor’s time. If you have any delay() or other functions that take longer than a few milliseconds your sketches will not be very responsive.

Yes sir, I noticed that cause i have some delays in the servos functions,so i’ll try decrease them…
thank’s again Mr.johnwasser , i really appreciate your help

ramzijsk:
i have some delays in the servos functions,so i'll try decrease them....

You should get rid of any delays, not decrease their waiting time.

Whandall:
You should get rid of any delays, not decrease their waiting time.

thank's Mr.Whandall