Combining two pieces of code

Hi guys,

I'm finishing off a project and need to add a second function to my original code. I have written this code but whenever I seem to combine them, it comes up with an error. The two codes are below, could someone have a look at combining them into 1 for me please.

Many thanks

[code]
#include <TinyGPS++.h>
#include <SoftwareSerial.h>

static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);

const int buzzer = 7;
const int motorPin = 5;

void setup()
{
  pinMode (buzzer, OUTPUT); 
  pinMode(8, OUTPUT);
  Serial.begin(9600);
  ss.begin(GPSBaud);

  Serial.println(F("Initialising"));
  Serial.println(F("Geofencing Track Worker Protection System"));
  Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println();
  Serial.println(F("Sats HDOP  Latitude   Longitude   Fix  Date       Time     Date Alt    Course Speed Card  Distance Course Card  Chars Sentences Checksum"));
  Serial.println(F("           (deg)      (deg)       Age                      Age  (m)    --- from GPS ----  ---- to Worksite  ----  RX    RX        Fail"));
  Serial.println(F("----------------------------------------------------------------------------------------------------------------------------------------"));

}

void loop()

{
  static const double WORKSITE_LAT = 53.310671, WORKSITE_LON = -4.633096;


  printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
  printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
  printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
  printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
  printInt(gps.location.age(), gps.location.isValid(), 5);
  printDateTime(gps.date, gps.time);
  printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
  printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
  printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
  printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.deg()) : "*** ", 6);

  unsigned long distanceKmToWorksite =
    (unsigned long)TinyGPSPlus::distanceBetween(
      gps.location.lat(),
      gps.location.lng(),
      WORKSITE_LAT, 
      WORKSITE_LON) / 1000;
  printInt(distanceKmToWorksite, gps.location.isValid(), 9);

  if (distanceKmToWorksite >40)

  digitalWrite(8, HIGH);   
  delay(100);              
  digitalWrite(8, LOW);   
  delay(0);      

  tone(buzzer, 1000); 
  delay(1000);        
  noTone(buzzer);
  delay(100);

  digitalWrite(motorPin, HIGH); //vibrate
  delay(500);  // delay one second
  digitalWrite(motorPin, LOW);  //stop vibrating
  delay(500); //wait 50 seconds.


  double courseToWorksite =
    TinyGPSPlus::courseTo(
      gps.location.lat(),
      gps.location.lng(),
      WORKSITE_LAT, 
      WORKSITE_LON);

  printFloat(courseToWorksite, gps.location.isValid(), 7, 2);

  const char *cardinalToWorksite = TinyGPSPlus::cardinal(courseToWorksite);

  printStr(gps.location.isValid() ? cardinalToWorksite : "*** ", 6);

  printInt(gps.charsProcessed(), true, 6);
  printInt(gps.sentencesWithFix(), true, 10);
  printInt(gps.failedChecksum(), true, 9);
  Serial.println();
  
  smartDelay(1000);

  if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.println(F("No GPS data received: check wiring"));
}

static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do 
  {
    while (ss.available())
      gps.encode(ss.read());
  } while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}

static void printInt(unsigned long val, bool valid, int len)
{
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
  if (!d.isValid())
  {
    Serial.print(F("********** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
    Serial.print(sz);
  }
  
  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }

  printInt(d.age(), d.isValid(), 5);
  smartDelay(0);
}

static void printStr(const char *str, int len)
{
  int slen = strlen(str);
  for (int i=0; i<len; ++i)
    Serial.print(i<slen ? str[i] : ' ');
  smartDelay(0);

}

[/code]

#include <SPI.h>
#include <MFRC522.h>
const int buzzer = 7; //buzzer to arduino pin 7

 
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
 
void setup() 
{
  Serial.begin(9600);   // Initiate a serial communication
  SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
  Serial.println("Approximate your card to the reader...");
  Serial.println();

}
void loop() 
{
  // Look for new cards
  if ( ! mfrc522.PICC_IsNewCardPresent()) 
  {
    return;
  }
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) 
  {
    return;
  }
  //Show UID on serial monitor
  Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "A3 49 6E 40") //change here the UID of the card/cards that you want to give access
  {
    tone(buzzer, 1000); // Send 1KHz sound signal...
  delay(100);        // ...for 1 sec
  noTone(buzzer);     // Stop sound...
  delay(10);        // ...for 1sec
   
  }
 
 else   {
    Serial.println(" Access denied");
    delay(3000);
  }
}

I think there's some extra bits in the second code as I just want the alarm and not the serial monitor. Any help would be appreciated

How did you go about combining them and what does this mean, exactly?

it comes up with an error

What error?

The usual way to combine code, at least as a first cut, is to:

  • Do a save..as of one to be the recipient of the second one
  • Copy the stuff from above setup() in the second one to above setup() in the recipient
  • Ditto with the stuff in setup() to the recipient setup()
  • Ditto with loop()
  • De-duplicate any calls like Serial.begin(), and any variable redefinitions and pin clashes etc
  • Walk yourself thru the logic to see if the result actually makes sense
  • Compile and try

Your heavy use of delay, especially in the first one, will make neither of them work very well if at all. Read the tutorials on doing several things at once and study blink without delay. Until you understand how to do multi tasking, non blocking code there is little point trying to combine them.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.