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?
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.
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;
}