Arduino Nano: turn on the arduino led as many times as you want from 1 to 9.

in utopian-io •  7 years ago  (edited)

Screenshot.jpg
photocredits

for this activity you must remember the concepts shown below.

If Statement

The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false.

for statement

A for statement is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. The for loop contains three parts: an initialization, a continuation condition, and step.

serial communication

Serial ports are the main way to communicate an Arduino board with a computer. Thanks to the serial port we can, for example, move the mouse

There are endless possibilities in which the use of the serial port is required. Therefore, the serial port is a fundamental component of a large number of Arduino projects, and it is one of the basic elements that we must learn in order to take full advantage of Arduino.

Serial ports

A port is the generic name with which we name interfaces, physical or virtual, that allow communication between two computers or devices.

A serial port sends the information through a sequence of bits. This requires at least two connectors to perform data communication, RX (reception) and TX (transmission). Sometimes you will see refer to the serial ports as UART.

Arduino and the Serial Port

Virtually all Arduino boards have at least one UART unit. Many models of Arduino boards have a USB or Micro USB connector connected to one of the serial ports, which simplifies the process of connecting to a computer. However some boards, such as the Mini Pro, do not have this connector so the only way to connect to them is directly through the corresponding pins.
we will connect using the USB cable.

Here we are going to see a photo of the arduino nano:

Screenshot002.jpg
PhotoCredits

Activity

we will turn on the Arduino LED by typing a number on the keyboard. if we type the number 4 the LED will blink 4 times.

difficulty

This tutorial is for those who know the basics of Arduino.

Requirement

  • Arduino nano with USB Cable
  • Laptop with Arduino IDE

Steps to follow

  • Prepare the equipment needed for the activity.
  • Connect the arduino to the Laptop using the usb cable
  • Open Arduino IDE

now we are going to open a new project in arduino:

Sin título.png

you will see this window:

Sin título1.png

now we'll see the code to turn on the LED as many times as we want. Remember that you must have basic arduino notions.

Program

now you must write the following code that I have explained:

foto1.jpg

This is the text code:

int key=0;
void setup(){
// put your setup code here, to run once:
Serial.begin(9600); //sets the baud rate of the UART for serial transmission
pinMode(13 , OUTPUT); // We declare that we use pin 13 as output
}
void loop(){
// put your main code here, to run repeatedly:
//Get the number of bytes (characters) available for reading from the serial
//port if the number of bytes available to read is greater than 0 we continue
if (Serial.available()>0){
//Reads incoming serial data and
//returns the first byte of incoming serial data available.
//we read one value at a time
int key = Serial.read();
//if the value read is between 1 and 9 the condition is true
if (key >= '1' && key <= '9')
{
//subtract the value' 0' to obtain the number sent from the keyboard
option -= '0';
//we use the for statement to turn on the led according to the number we read.
for(int i=0;i<key;i++){
digitalWrite(13 , HIGH); //we turn on the led
delay(150); //wait 150ms
digitalWrite(13 , LOW); //we turn off the led
delay(150); //wait 150ms
}
}
}


The serial port monitor is a small utility integrated within IDE Standard that allows us to easily send and receive information through the serial port. In the image we see how to open the serial monitor:

Sin título.png

Using the serial monitor is very simple, and has two zones, one that shows the data received, and another to send them. You should check that the baud is 9600 as you configured it with the Serial.begin(9600) function.

Sin título1.png

Run the code. when we press a number from 1 to 9 the led turn on as many times as we indicate, otherwise it doesn't turn on. for example if we write 1 and we press SEND the LED turn on only once, if we press 4 the LED turn on 4 times

Sin título2.png

try to run the code and start to learn.

Collaborations



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.

You can contact us on Discord.
[utopian-moderator]

Hi! I am a robot. I just upvoted you! Readers might be interested in similar content by the same author:
https://steemit.com/utopian-io/@luisrod/arduino-nano