Arduino Tutorial - How the code structure works
Posted: Sun Jun 02, 2013 7:06 am
Here's some code I generated with the Wizard.
Lets go over it line by line.
The first part:
These are instructions to the compiler. This says "Before we start with my program, go get all this stuff and include it in." This is where our RA functions are kept. When we say "Hey, ReefAngel, do this" most likely it's something stored in these files.
Next part:
Anything after // on the current line is ignored. It is a comment for the reader's eyes. The controller couldn't care less about it. Note there are lots of extra /'s in these lines. They're for aesthetic purposes only. They're ignored too.
Another way to comment is like this:
Note that if you just put in "/*" without "*/", it will comment out the rest of your code! In the RA Arduino editor, stuff that is commented is greyed out. If your code is greyed out when it shouldn't be, then check for this.
Now we're getting to the fun stuff. This starts a function called setup. It's one of the two functions you must have in Aruduino to have a valid sketch (Arduino's word for program).
void means that when we call this function, we don't expect it to tell us anything back. It's a little out of place here since we never really call setup, it is automatically run once when the program starts up. The empty () means we aren't sending any data to it. Again, kind of extra in this case.
Next, we have {. This starts a section of code. In this case, it's the beginning of setup(). Every function will have a { at the beginning and a } at the end. In addition, you may have other sets of these inside the function. An "if" statement is a good example of this. Again, they just define a section of the code.
This line says call the function ReefAngel.Init, and don't send it anything, hence the empty parentheses. The ";" marks the end of the line, and the // is for the comment that is nice for us to read but ignored by the program.
Same stuff here.
This one is a little tricky. This sets the ports turned off by feeding mode. In this example, it's port (relay) #3, which is my skimmer. The Port3Bit business is advanced stuff that I don't have a good enough grasp on to teach, so just know that that's how it works. Again, notice the ";" at the end of the line.
Same stuff. On my setup I don't use these things, so they're set to 0, meaning no ports are set for these.
This sets the TempProbe and the OverheatProbe to the value stored in T1_PROBE. T1_PROBE is defined in one of the #includes as the first temperature probe.
Please note that everything is CaSe SeNsItIvE! T1_PROBE is not the same as t1_probe. Capitalization is important.
OK, a little more action here. This calls a function that writes the internal memory overheat and tells it 869. This is actually our overheat temperature of 86.9F. RA stores it as 869 because it takes up a lot less memory than 86.9.
A note on function names. You can name functions just about anything you like. Writers will use "." and "_" as part of the function name. The "." is used to denote different functions in a group. Like ReefAngel.TempProbe and ReefAngel.OverheatProbe, both parts of the ReefAngel group. The "_" is used intstead of spaces, which are not allowed.
These call the function ReefAngel.Relay.On and tell it to turn on Port3 and Port4. There is also a corresponding function called ReefAngel.Relay.Off. Notice the clever use of periods to make things neat and tidy.
Finally,
More comment lines, followed by the all-important "}". This is the end of setup().
Part 2 to follow.
--Colin
Code: Select all
#include <ReefAngel_Features.h>
#include <Globals.h>
#include <RA_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <InternalEEPROM.h>
#include <RA_NokiaLCD.h>
#include <RA_ATO.h>
#include <RA_Joystick.h>
#include <LED.h>
#include <RA_TempSensor.h>
#include <Relay.h>
#include <RA_PWM.h>
#include <Timer.h>
#include <Memory.h>
#include <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <ReefAngel.h>
////// Place global variable code below here
////// Place global variable code above here
void setup()
{
// This must be the first line
ReefAngel.Init(); //Initialize controller
ReefAngel.AddStandardMenu(); // Add Standard Menu
// Ports toggled in Feeding Mode
ReefAngel.FeedingModePorts = Port3Bit;
// Ports toggled in Water Change Mode
ReefAngel.WaterChangePorts = 0;
// Ports toggled when Lights On / Off menu entry selected
ReefAngel.LightsOnPorts = 0;
// Ports turned off when Overheat temperature exceeded
ReefAngel.OverheatShutoffPorts = 0;
// Use T1 probe as temperature and overheat functions
ReefAngel.TempProbe = T1_PROBE;
ReefAngel.OverheatProbe = T1_PROBE;
// Set the Overheat temperature setting
InternalMemory.OverheatTemp_write( 869 );
// Ports that are always on
ReefAngel.Relay.On( Port3 );
ReefAngel.Relay.On( Port4 );
////// Place additional initialization code below here
////// Place additional initialization code above here
}
void loop()
{
ReefAngel.StandardFan( Port1,771,781 );
ReefAngel.StandardHeater( Port2,751,761 );
ReefAngel.DosingPumpRepeat( Port5,0,240,120 );
ReefAngel.Wavemaker( Port6,60 );
ReefAngel.StandardLights( Port7,9,0,19,0 );
ReefAngel.StandardLights( Port8,9,0,19,0 );
ReefAngel.DosingPumpRepeat( Box1_Port1,0,360,120 );
ReefAngel.DosingPumpRepeat( Box1_Port2,60,360,120 );
ReefAngel.SingleATO( true,Box1_Port3,60,0 );
ReefAngel.PWM.SetDaylight( PWMSlope(9,0,20,0,0,100,60,15) );
ReefAngel.PWM.SetActinic( PWMSlope(9,0,20,0,0,100,60,15) );
////// Place your custom code below here
////// Place your custom code above here
// This should always be the last line
ReefAngel.ShowInterface();
}
The first part:
Code: Select all
#include <ReefAngel_Features.h>
#include <Globals.h>
#include <RA_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <InternalEEPROM.h>
#include <RA_NokiaLCD.h>
#include <RA_ATO.h>
#include <RA_Joystick.h>
#include <LED.h>
#include <RA_TempSensor.h>
#include <Relay.h>
#include <RA_PWM.h>
#include <Timer.h>
#include <Memory.h>
#include <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <ReefAngel.h>
Next part:
Code: Select all
////// Place global variable code below here
////// Place global variable code above here
Another way to comment is like this:
Code: Select all
/* These symbols
Make Comments
Out of Everything
Between Them.
*/
Code: Select all
void setup()
void means that when we call this function, we don't expect it to tell us anything back. It's a little out of place here since we never really call setup, it is automatically run once when the program starts up. The empty () means we aren't sending any data to it. Again, kind of extra in this case.
Next, we have {. This starts a section of code. In this case, it's the beginning of setup(). Every function will have a { at the beginning and a } at the end. In addition, you may have other sets of these inside the function. An "if" statement is a good example of this. Again, they just define a section of the code.
Code: Select all
ReefAngel.Init(); //Initialize controller
Code: Select all
ReefAngel.AddStandardMenu(); // Add Standard Menu
Code: Select all
// Ports toggled in Feeding Mode
ReefAngel.FeedingModePorts = Port3Bit;
Code: Select all
// Ports toggled in Water Change Mode
ReefAngel.WaterChangePorts = 0;
// Ports toggled when Lights On / Off menu entry selected
ReefAngel.LightsOnPorts = 0;
// Ports turned off when Overheat temperature exceeded
ReefAngel.OverheatShutoffPorts = 0;
Code: Select all
// Use T1 probe as temperature and overheat functions
ReefAngel.TempProbe = T1_PROBE;
ReefAngel.OverheatProbe = T1_PROBE;
Please note that everything is CaSe SeNsItIvE! T1_PROBE is not the same as t1_probe. Capitalization is important.
Code: Select all
// Set the Overheat temperature setting
InternalMemory.OverheatTemp_write( 869 );
A note on function names. You can name functions just about anything you like. Writers will use "." and "_" as part of the function name. The "." is used to denote different functions in a group. Like ReefAngel.TempProbe and ReefAngel.OverheatProbe, both parts of the ReefAngel group. The "_" is used intstead of spaces, which are not allowed.
Code: Select all
// Ports that are always on
ReefAngel.Relay.On( Port3 );
ReefAngel.Relay.On( Port4 );
Finally,
Code: Select all
////// Place additional initialization code below here
////// Place additional initialization code above here
}
Part 2 to follow.
--Colin