logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

Arduino – Sketch, Taking a Look at Programs

Device Plus Editorial Team
Published by Device Plus Editorial Team at September 16, 2015
Categories
  • Arduino
Tags
Arduino

Arduino

Last time, we looked at various calculations using Ohm’s law as a starting point. It’s an unavoidable subject in order to design an electrical circuit, but expanding your knowledge while thinking hard and worrying about all those things is on the other hand really fun. So, I would be very happy if you could put things into practical exercise as you immerse yourself in Arduino.
Starting from this article, alongside designing an electrical circuit, we will touch a little bit at a time on Arduino programming.

Understanding the Function and Flow of a Program That Makes LEDs Light Up

Below, you can see the program code for lighting up a LED, as we learned last time.

1
2
3
4
5
6
7
8
9
10
11
12
//setup - The very first process which is called when Arduino has started up
void setup() {
//This command is used to let Arduino know to use pin 13 for output
pinMode(13, OUTPUT);
}
//loop - after setup has been processed, the process contained in the loop will be repeated until power is disconnected
void loop() {
digitalWrite(13, HIGH); //Setting the pin 13 output to HIGH=5V
delay(1000); //1000 msec = wait 1 sec
digitalWrite(13, LOW); //Setting the pin 13 output to LOW=0V
delay(1000); //1000 msec = wait 1 sec
}

Seeing this code might be difficult to comprehend for people who have no experience with programming. Please don’t be worried, because you will get used to it as we investigate each item one by one. Programming is basically defined by a programming language, so each language has its own grammar, and following the rules of this grammar, we will go on giving commands to the computer. The computer will go on executing the commands in the given order, so through programming, we will have the computer achieve what we have in mind.

Putting it easily, the computer will not do anything which we haven’t included in the program. (It will only do the things we wrote.)
Looking at it from the other side, a program is like a very obedient child, which moves completely according to the commands.

Among the various programming languages, programming in Arduino is simply called making a sketch. In the case of other programming languages, in order to command a single process, it is often required to explain the computer down to the most subtle behaviors in order to have it work as you intend. In Arduino it is very simple to describe the required processes. Well then, let us go into each element one by one.

Arduino’s Fundamental Structure: Setup and Loop

Basically, an Arduino program runs on two main big structures. These are setup() and loop().

setup() function

1
2
3
4
5
//setup - The very first process which is called when Arduino has started up
void setup() {
//This command is used to let Arduino know to use pin 13 for output
pinMode(13, OUTPUT);
}

The setup() function is the very first process which is called when Arduino is connected to power and starts up. In this instance, we use it to set up the pins.

setup() – The very first process which is called when Arduino has started up

1
2
3
void setup() {
//Here you will give the commands which Arduino needs to execute after starting up
}

In a program to light up LEDs, we use the command pinMode(). pinMode() is a command which is used to set up which of the several Arduino pins should be used in which way. In this instance, we used the command pinMode(13,OUTPUT), which announces that we use pin 13 as an output. If you would set it up as pinMode(13,INPUT) on the other hand, when the electric current reaches the Arduino main body from the wire connected to pin 13, pin 13 will use the value of this current as an input into the Arduino main body.

loop() function

1
2
3
4
5
6
7
//loop - after setup has been processed, the process contained in the loop will be repeated until power is disconnected
void loop() {
digitalWrite(13, HIGH); //Setting the pin 13 output to HIGH=5V
delay(1000); //1000 msec = wait 1 sec
digitalWrite(13, LOW); //Setting the pin 13 output to LOW=0V
delay(1000); //1000 msec = wait 1 sec
}

loop() is the main programming component of Arduino. When Arduino is connected to a power source, after setup() has been processed, this loop() function will execute the process contained in the loop repeatedly until power is disconnected.

loop() – initiating after setup(), the process will be repeated until power is disconnected

Now, let us take a look at the concrete content of the loop() function.

1
digitalWrite(13, HIGH); //Setting the pin 13 output to HIGH=5V

This digitalWrite() function is used when setting an electric current as output for a specified pin number. The output is generally 5V.

1
(1000); //1000 msec = wait 1 sec

The next function in the program is delay(). This command is used to let Arduino wait a specified amount of time. In this instance, it is used to let it wait for one second while the LED is lighting up.

1
2
digitalWrite(13, LOW); //Setting the pin 13 output to LOW=0V
delay(1000); //1000 msec = wait 1 sec

The digitalWrite function is used again immediately after this to set the output to LOW(0V), and then delay is used again to let it wait for 1 sec while the LED is off.

Putting together the flow of the process looks as follows:

  1. loop() function starts the process
  2. Set pin 13 output to 5V: Turns on the LED – digitalWrite(13,HIGH);
  3. Wait 1 sec: delay(1000);
  4. Set pin 13 output to 0V: Turns off the LED – digitalWrite(13,LOW);
  5. Wait 1 sec: delay(1000);
  6. Process continues by going back to the start of the loop()

So, this would be the program to let a LED switch on and off alternatively for 1 second.

Adding a Little Bit to This

So, now you understand the flow of a program, let us try to make some alterations to both the program and the electric circuit. Until now, we’ve been using a single LED, but let’s try using two now. Let’s first make the LEDs light up at the same time, and then think about how we can make them light up in an alternating way. Before we go into the explanation, I would recommend you to try thinking about it once for yourself.

Lighting up Two LEDs Simultaneously

Arduino lighting 2 leds

There are various methods you could think of in order to light up two LEDs simultaneously, but if we were to adjust the program and circuit we’ve been using so far, you could do it by adjusting the circuit as follows.

Arduino

How would we go about having them light up in an alternating way? For this, let’s try using an additional pin in addition to pin 13 as output.

Arduino

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//setup - The very first process which is called when Arduino has started up
void setup() {
//Telling Arduino to use pin 12,13 for output
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
//loop - after setup has been processed, the process contained in the loop will be repeated until power is disconnected
void loop() {
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
delay(1000);
}

Have you gotten used to adjusting the electrical circuit as you make changes to the program? Now, to conclude, let us try lighting up a row of 4 LEDs in order. While you’re at it, you can experiment by making the value of the delay longer or shorter.

Arduino

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//setup - The very first process which is called when Arduino has started up
void setup() {
//Telling Arduino to use pin 2,5,8,13 for output
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
pinMode(8, OUTPUT);
pinMode(13, OUTPUT);
}
//loop - after setup has been processed, the process contained in the loop will be repeated until power is disconnected
void loop() {
digitalWrite(13, LOW);
digitalWrite(2, HIGH);
delay(150);
digitalWrite(2, LOW);
digitalWrite(5, HIGH);
delay(150);
digitalWrite(5, LOW);
digitalWrite(8, HIGH);
delay(150);
digitalWrite(8, LOW);
digitalWrite(13, HIGH);
delay(150);
}

Starting from the next article, we’ll create an actual functional product as we use various electrical components.

Device Plus Editorial Team
Device Plus Editorial Team
Device Plus is for everyone who loves electronics and mechatronics.

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • What Is Current, Voltage, Resistors, and Ohm’s Law?What Is Current, Voltage, Resistors, and Ohm’s Law?
  • How to Read Your Arduino’s MindHow to Read Your Arduino’s Mind
  • How To Use ArduinoHow To Use Arduino
  • Create Your First Sketch and Learn More About the Programming LanguageCreate Your First Sketch and Learn More About the Programming Language
  • Let’s use Arduino with a light sensor!Let’s use Arduino with a light sensor!
  • Using LEDs and Motors on Raspberry Pi: How to Handle Larger CurrentsUsing LEDs and Motors on Raspberry Pi: How to Handle Larger Currents
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort