Arduino programs are written in the Arduino Integrated Development Environment (IDE). Arduino IDE is a special software running on your system that allows you to write sketches (synonym for program in Arduino language) for different Arduino boards. The Arduino programming language is based on a very simple hardware programming language called processing, which is similar to the C language. After the sketch is written in the Arduino IDE, it should be uploaded on the Arduino board for execution.
https://en.wikipedia.org/wiki/Arduino_IDE
The structure of Arduino program is pretty simple. Arduino programs have a minimum of 2 blocks: Preparation & Execution. Each block has a set of statements enclosed in curly braces. Here, void setup() is the preparation block and void loop() is an execution block.
The setup function is the first to execute when the program is executed, and this function is called only once. The setup function is used to initialize the pin modes and start serial communication. This function has to be included even if there are no statements to execute.
After the setup() function is executed, the loop() block runs next. The execution block hosts statements like reading inputs, triggering outputs, checking conditions etc..
In the example loop() function is a part of execution block. As the name suggests, the loop() function executes the set of statements (enclosed in curly braces) repeatedly.
Note: Arduino always measures the time duration in millisecond. Therefore, whenever you mention the delay, keep it in milli seconds.
This statement is used in the preparation block of the Arduino program, that is, in the void setup() function. The pinMode statement is used to configure a pin to behave in either the INPUT mode or OUTPUT mode.
The syntax of the “pinMode” statement is as follows:
pinMode (pin-number, behavior);
You can also declare, in the initial part of the code, a name for the component that goes into a pin, and use that name instead.
ANALOG INPUT readings can go from 0 to 1023
ANALOG OUTPUT signals go from 0 to 255 (PWM)
DIGITAL INPUTS or OUTPUTS signals ara always 1 or 0, HIGH or LOW, On or Off.
digitalRead:
This statement reads the state of the specified digital pin and the result will be either HIGH or LOW. The syntax of the “digitalRead" statement is as follows:
state = digitalRead(pin);
digitalWrite
This statement is used to either turn ON or OFF the device connected to a specified digital pin. The syntax of the “digitalWrite” statement is as follows:
digitalWrite(pin, status)
Note: Digital pins on the Arduino board are input by default, so these pins can be used in the INPUT mode even without declaration.
analogRead:
This statement reads the value from the specified analog pin on the Arduino board with 10-bit resolution. The result will be an integer value in the range of 0 to 1023. The syntax of the “analogRead statement” is as follows:
value = analogRead(pin);
analogWrite
This statement writes a pseudo-analog value to the specified pin. The value is called pseudo-analog because it is generated by Pulse Width Modulation pins (PMW) on the Arduino board. The value can be specified as a variable or constant in the range of 0 to 255. The syntax of “analogWrite” statement is as follows:
analogWrite(pin-number, value);
Note: Analog pins don't need to be declared as INPUT or OUTPUT pin.
The “if” statement is a conditional statement, it checks if a certain condition is met. If yes, it executes the set of statements enclosed in curly braces. If the condition is false, then the set of statements will skip the execution.
The syntax of the “if” statement is follows:
{
statement-1;
}
X==Y // Check if X is equal to Y
X!=Y // X is not equal to Y
X>Y
X<=Y
X >=Y
If you want to repeatedly execute a set of statements for a specific number of times, then you can use a for loop.
The syntax of the “for” statement is as follows:
for(initialization; condition; expression)
{
Statement-1;
Statement-n;
}
In the for loop, the variable initialization is done only once. After this the set of statements is executed, the initialization step is skipped and directly the condition will be checked. If the result is true, before executing the set of statements in the curly braces, the expression will be executed.
Note: X++ is same as X=X+1 or X+=1
The while loop executes a set of statements until the expression inside the parentheses is false.
The syntax of the “‘while” loop is as follows:
while (some variable ?? value)
{
Statement-1;
Statement-n;
}
The set of statements under a while loop is executed only if the condition is true. In the while loop, there should be a statement which changes the state of the tested variable. If not, the while loop will never exit. In the example above, the tested variable is “i” and the statement that changes its state is “i++.”
If you want to execute a set of statements once and repeatedly execute the set if a certain condition is true.
The syntax of the “do-while” loop is as follows:
do
{
statement-1;
statement-n;
}
while (some variable ?? value);
The do-while loop is a bottom-driven loop because the condition is checked at the end, after executing the set of statements at least once.
*The program above turns on the LED if the boiler temperature is less than or equal to 35.
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
CODE IS IN ARDUINO EXAMPLES
#define LDR A0
float LDRvalue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
LDRvalue = map(analogRead(LDR),0,1024,100,0);
Serial.print(LDRvalue);
Serial.println(" %");
}
CODE LINK
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(1000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(F("°F Heat index: "));
Serial.println(hic);
}
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
#define LDR A0
DHT dht(DHTPIN, DHTTYPE);
float LDRvalue = 0;
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(1000);
LDRvalue = map(analogRead(LDR),0,1024,100,0);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(F("Heat index: "));
Serial.print(hic);
Serial.println(F("°C "));
Serial.print(F("Light %: "));
Serial.print(LDRvalue);
Serial.println(" %");
}
CODE LINK
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11
#define LDR A0
#define ledPin 0
DHT dht(DHTPIN, DHTTYPE);
float LDRvalue = 0;
void setup() {
Serial.begin(9600);
dht.begin();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
delay(1000);
LDRvalue = map(analogRead(LDR),0,1024,100,0);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
if (LDRvalue < 50){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(F("Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(F("Light %: "));
Serial.print(LDRvalue);
Serial.println(" %");
}
Built with
Web Design Program