Explain me what this code does

Hello everybody!

Im new to arduino and all the programming regarding to it. I have a code for my alarm system that I made and it works perfect, but I need help understanding the actual code because Ive just put it together from pieces, examples, tutorials and so on. I kinda know C/C++ programming but arduino is little different and my knowledge of those two languages isn’t perfect either. So maybe someone could enlighten me and explain me how the code is put together and what it does in every section?

I would be VERY VERY thankful! ::slight_smile:

#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
#include "Sodaq_DS3231.h"
#include <Wire.h>

SMSGSM sms;
String smsText = "MOVEMENT AT  ";
boolean started=false;
char sms_text[160];
int ledPin = 13;
int pirPin = 7;
int val = 0;

void setup() 
{
  Wire.begin();
  rtc.begin();
  pinMode (ledPin,OUTPUT);
  pinMode (pirPin, INPUT);
  Serial.begin(3000);
  if (gsm.begin(3000)) 
    {
        Serial.println("\nstatus=READY");
        started=true;
    } 
    else 
        Serial.println("\nstatus=IDLE");
}
void loop () 
{
  
  String timestamp;
  val = digitalRead(pirPin);
  digitalWrite(ledPin,val);

if (val == 1)
{
  timestamp = getDateTime();
  Serial.println(timestamp);
  smsText = smsText+timestamp;
  smsText.toCharArray(sms_text,160);
  sms.SendSMS("NUMBER",sms_text); //Insert phone number here!!!
  String smsText = "MOVEMENT AT ";
  delay(2000);
}
  
}

String getDateTime()
{
   DateTime now = rtc.now(); //Gets real time
  
  String timeString;
  String hourString;
  String minuteString;
  
  if(now.hour() <10)
  {
     hourString = "0"+String(now.hour());  
  }else
  {
    hourString = String(now.hour());
  }

  if(now.minute() <10)
  {
    minuteString = "0"+String(now.minute());
  }else
  {
     minuteString = String(now.minute());
  }

  timeString = hourString+":"+minuteString+" "+String(now.month())+"/"+String(now.date())+"/"+String(now.year());
  return timeString;
}

. I kinda know C/C++ programming but arduino is little different

No, it's C++.

You've got some strange serial line speeds there

So maybe someone could enlighten me and explain me how the code is put together and what it does in every section?

How about you add comments to the parts that you understand explaining how it works so that we don’t need to explain those to you ?

Arduino code is C++ with the simple difference that you do not have a main() function. You have a setup() function called once after reboot, and a loop() function that will be called over and over.

Your code will turn on and off an LED according to the PIR input.
And it will every 2 seconds send a timestamp on serial and send an SMS.
Not too usefull I guess.

/Mogens

There is a main(), it’s included as part of the compilation process so one doesn’t need to call it out.

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

Add your own setup & loop at a minumum:

void setup(){ 
}
void loop() { 
}

You are correct, that part is "under the hood" so the user only sees setup() and loop() in the sketches.

/Mogens