Introduction:
As a part of the Build-Your-Own program, Puriphico has introduced the "Learn" sub-category, which focuses on teaching general product-design skills with the hope that students can progress to the point at which they can build (and understand how to build) their very own Puriphico device from home. The upcoming series of videos (to be found under the Build-Your-Own page as well as the YouTube channel @puriphico) and written articles will be based on the following Arduino-related subjects: code structures, code functions, and code variables.
Tutorial - The basics of setting up your first Arduino sketch:
Complete overview:
Your program will most likely, at least at the very beginning, be structured in 3 parts, the “introduction/clarification,” the “void setup”, and the “void loop.”
Introduction:
In the first part of your program, you can execute a range of operations, which will be described in great detail in future articles. To name some examples, you can define constants, include libraries, and more.
Setup()
The setup function, which is called at the beginning of a sketch, is used to initialize variables, declare pinModes (which is where a pin is specified as an INPUT or OUTPUT), or incorporate libraries, among other things. It will only run once, after each power-up or reset of the arduino board.
Loop()
After creating a setup() function, a loop function, put simply, loops the code inside consecutively.
Differently to the setup function, the loop function allows for changes in the outcome of a program (the outputs) based upon the truth of specific conditions (in the form of if statements, for loops, or while loops).
Loops can have other loops within them; for this reason, it is important to ensure that your brackets align, or else the Arduino will return a compiling error.
Below is an example of the structure of a Puriphico program:
In this program, I used a sensor to detect the motion of handwashing with an ATtiny microprocessor, instructing the LED's to flash upon detection of motion.
While it may seem complicated, it is truly a more sophisticated example of the 3 'parts' of a program. At the top, you can see that, although I have not included libraries or defined many constants, I have defined variables as integers and bytes (a certain data type, which will also be described later). Just below that, you can see the void setup, where I specify the pinModes of the pins I am using. Lastly, the void loop falls after the setup, and it contains the primary bulk of my program. Each section has been colored differently for better clarity.
-------
int switchState = 0;
int prevSwitchState = 0;
int reply;
byte sensorPin = 2;
byte timer = 0;
byte counter = 0;
byte underCounter = 0;
void setup()
{
pinMode(sensorPin,INPUT);
pinMode(0,OUTPUT); // green LED
pinMode(1,OUTPUT); // red LED
}
void loop()
{
byte state = digitalRead(sensorPin);
if(state == 1) {
digitalWrite(0,HIGH);
digitalWrite(1,LOW);
delay(500);
digitalWrite(0,LOW);
delay(500);
timer = timer + 1;
if (timer == 3) {
underCounter = underCounter + 1;
}
if (timer == 20) {
digitalWrite(1,LOW);
digitalWrite(0,HIGH);
delay(500);
digitalWrite(0,LOW);
delay(500);
digitalWrite(0,HIGH);
delay(500);
digitalWrite(1,LOW);
delay(500);
digitalWrite(0,HIGH);
delay(500);
timer = 0;
counter = counter + 1;
delay(500);
delay(500);
}
}else if(state == 0) {
digitalWrite(1,LOW);
digitalWrite(0,LOW);
delay(1000);
timer = 0;
}
}
------------
Here is another example, using the standard "Blink" Arduino program (however, it is important that this program does not require the use of an introduction):
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
----------
Comments