Arduino Beginners Course – Lesson 2

Arduino Beginners Course – Lesson 2

Hi! Welcome to the second Lesson of this Arduino Beginners Course!

In the previous lesson we explained some basic things, like how to install the Arduino IDE. We also explaind how to use an LCD with Arduino and we made an Hello World!

In this Lesson you are going to learn how to use some new modules!

Let’s start!

What are we going to explain?

  • Send data from the keyboard to Arduino
  • map( ) function
  • Servo
  • Joypad

Send data from the keyboard to Arduino

If you already read the previous Lesson, you know how to print messages on the Serial Monitor. But we only printed messages wrote in the code. So what have we to do if we want to give some input to Arduino from our keyboard? It’s easy and we will show you an example to do it!

We explained it very well in this Article, but we will post here an example!

We suggest to read the Article about How to send Input from Serial Monitor to Arduino because we explain each function there.

String input;
 
void setup() {
    Serial.begin(9600); 
    Serial.println("Type something!");
}
 
void loop()
{
    if(Serial.available())
    {
      input = Serial.readStringUntil("\n"); 
      int intInput= input.toInt();       
      Serial.print("You typed: " );
      Serial.println(intInput);
    }
}

With this code we will send as input a number as a String and Arduino will convert it in a Int.

map() function

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh.

map(value, fromLow, fromHigh, toLow, toHigh)

This means that we can change the initial range of values to a new range.

We can write a very easy example.

we set as inital value x = 10

Then we use tha map function, so the value will change.

String input;
 
void setup() {
    Serial.begin(9600); 
}
 
void loop()
{
  int x = 10;
  x = map(x, 0, 10, 0, 100);
  Serial.println(x);
  delay(1000);
}

If you make this simple example you will see in the Serial Monitor the value 100.

That’s bacause the map function changed the range for the x values.

Servo

First of all you have to read two articles.

  • Servo Tutorial
  • What is a PWM? (not really necessary, but if you want to know more about how a Servo in controlled…this article it’s interesting).

Now that you have read The Servo Tutorial, we want to make a simple example using something we explained before…

We will control a Servo from Serial Monitor.

#include <Servo.h>   //we are including the library
Servo myservo;       //we are creating an object for the class Servo
String angle;       //that's the variable that we will use to change the angle 
int intInput=0;

void setup()
{
  Serial.begin(9600); //to read what we write
  myservo.attach(9); // we are using a Servo's class method. With it we are stating that the pin to which is attached the servo is the pin 9
  myservo.write(0); //we initialize the servo's angle to 0
}
void loop()
{
  if(Serial.available())
  {
    angle = Serial.readStringUntil("\n"); 
    intInput= angle.toInt();       
    Serial.print("You typed: " );
    Serial.println(intInput);
  }

  myservo.write(intInput); //the servo will change the angle and won't do any other thing
}

Now you can type any number from 0 to 180 and the servo will change its angle!

Joypad

This is the last part of this second lesson of the Arduino beginners course. Now we will explain how to use a joypad module.

You can read a detailed article reading our article -> how to use a joypad with Arduino.

Once you have read the article about how to use a joypad with Arduino, we can show you another simple example in which we use the map() function that we explained before!

#define Xaxis A0
#define Yaxis A1
#define buttonPad 2

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPad, INPUT_PULLUP);
}

void loop()
{
  int x = analogRead(Xaxis);
  int y = analogRead(Yaxis);
  int button = !digitalRead(buttonPad);

  x= map(x, 0, 1023, 0, 15);
  y= map(y, 0, 1023, 0, 15);
  
  Serial.print("Value:");
  Serial.print("x: " + String(x));
  Serial.print(",\tY:" + String(y));
  Serial.println(",\tB: " + String(button));

  delay(100);
}

Now the values range will be from 0 to 15.

That’s all for this lesson of the Arduino beginnners course! Hope you liked it!

If have any questions or you want to suggest anything you can write it in the comments!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: