Introduction To Embedded C Coding 2
Introduction To Embedded C Coding 2
TO EMBEDDED C
CODING
ARDUINO - PROGRAM
STRUCTURE
ARDUINO - PROGRAM
STRUCTURE
What is Setup? What type of code is written in the
setup block?
It contains an initial part of the code to be executed. The pin modes,
libraries, variables, etc., are initialized in the setup section. It is
executed only once during the uploading of the program and after reset
or power up of the Arduino board.
What is Loop? What type of code is written in the Loop
block?
The loop contains statements that are executed repeatedly. The
section of code inside the curly brackets is repeated depending on the
value of variables.
ARDUINO - DATA TYPES
VARIABLE NAME
BASIC FUNCTIONS AND
USAGE
Configures the specified pin to behave either
PINMODE()
as an input or an output
Pauses the program for the amount of
time (in milliseconds) specified as
DELAY( ) FUNCTION
parameter.
(There are 1000 milliseconds in a
second.)
DIGITALWRITE()
Write a HIGH or a LOW value to a digital pin.
If the pin has been configured as an OUTPUT with pinMode(), its voltage
will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for
HIGH, 0V (ground) for LOW.
Syntax
digitalWrite(pin, value)
PIN DETAILS
Pin Number 33: LED 1: OUTPUT Pin
void loop()
{
digitalWrite(33, HIGH);
delay(1000);
digitalWrite(33, LOW);
delay(1000);
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
delay(1000);
}
PROGRAM 5: TO BLINK LED-2 WITH PIN 4 FIRST
THEN LED-1
void setup()
WITH PIN 33
{
pinMode(33, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
delay(1000);
digitalWrite(33, HIGH);
delay(1000);
digitalWrite(33, LOW);
delay(1000);
}
PROGRAM 6: TO BLINK LED-1 WITH PIN 33 AND LED 2
WITH PIN 4 SIMULTANEOUSLY
void setup()
{
pinMode(33, OUTPUT);
pinMode(4, OUTPUT);
}
void loop()
{
digitalWrite(33, HIGH);
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(33, LOW);
digitalWrite(4, LOW);
delay(1000);
}
SWITCH AND
LED
PROGRAM 1: TO BLINK LED-1 WITH PIN 33 UPON PRESSING THE
SWITCH/BUTTON WITH PIN 32
int ledPin=33;
int buttonPin=32;
int buttonState=0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState=digitalRead(buttonPin);
if(buttonState==LOW)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
}
PROGRAM 2: TO SHINE LED-1 AND UPON PRESSING THE BUTTON BLINK LED-2
int buttonPin=32;
int buttonState=0;
void setup()
{
pinMode(33, OUTPUT);
pinMode(4, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
digitalWrite(33, HIGH);
buttonState=digitalRead(buttonPin);
if(buttonState==LOW)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
}
}
PROGRAM 3: TO BLINK BOTH LED-1 AND LED-2 UPON PRESSING THE BUTTON
int buttonPin=32;
int buttonState=0;
void setup()
{
pinMode(33, OUTPUT);
pinMode(4, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState=digitalRead(buttonPin);
if(buttonState==LOW)
{
digitalWrite(4, HIGH);
digitalWrite(33, HIGH);
}
else
{
digitalWrite(4, LOW);
digitalWrite(33, LOW);
}
}
PROGRAM 4: TO BLINK THE SHINED LED-1 AND LED-2 UPON PRESSING THE BUTTON
int buttonPin=32;
int buttonState=0;
void setup()
pinMode(33, OUTPUT);
pinMode(4, OUTPUT);
pinMode(buttonPin, INPUT);
void loop()
buttonState=digitalRead(buttonPin);
if(buttonState==LOW)
digitalWrite(4, LOW);
digitalWrite(33, LOW);
else
digitalWrite(4, HIGH);
digitalWrite(33, HIGH);
}
PROGRAM 5: TO BLINK LED-2 FOLLOWED BY LED-1 UPON PRESSING THE BUTTON
void loop()
{
buttonState=digitalRead(buttonPin);
int buttonPin=32; if(buttonState==LOW)
int buttonState=0; {
digitalWrite(4, HIGH);
void setup() delay(1000);
{ digitalWrite(4, LOW);
delay(1000);
pinMode(33, OUTPUT); digitalWrite(33, HIGH);
pinMode(4, OUTPUT); delay(1000);
digitalWrite(33, LOW);
pinMode(buttonPin, }
INPUT);
else
} {
digitalWrite(4, LOW);
digitalWrite(33, LOW);
}
}
PROGRAM 6: TO BLINK LED-1 WHEN THE BUTTON IS PRESSED THEN BLINK LED-2 WHEN THE
BUTTON IS PRESSED AGAIN AND SO ON…
void loop()
buttonState=digitalRead(buttonPin);
int buttonPin=32; if(buttonState==LOW)
int buttonState=0; {
pressCount++;
if(odd_even)
boolean odd_even; {
digitalWrite(33, HIGH);
}
{ else if(!odd_even)
{
pinMode(33, OUTPUT); digitalWrite(4, HIGH);
pinMode(4, OUTPUT); }
delay(1000);
pinMode(buttonPin, }
INPUT); else
} digitalWrite(4, LOW);
digitalWrite(33, LOW);