Arduino Beginners Course – Lesson 4

Arduino Beginners Course – Lesson 4

Welcome to the lesson 4! You are going to know all the Arduino basics !!

This lesson will be your first Checkpoint! We will write a sketch using some of the modules that we already used in the previous lessons!

Arduino Basics in this Lesson

We will make a code to create a program that will say if you typed the right/wrong password. Then it open a door (servo movement) if the password is correct, or it will close it (servo movement) if it is wrong.

Moreover we will use also some other module like LEDs, Buzzers, Buttons.

We will use the Buzzer to make a beep if the password is wrong and we will use a button to change the password.

Let’s start!

Step 1 – Make the password code

First of all we have to make a code to insert the password. It will say if it is wrong or it is correct.

String password;
String input;

void setup() 
{
    Serial.begin(9600);
    password = "pass";
}
 
void loop()
{
  if(Serial.available())
  {
    input = Serial.readStringUntil('\n');
    Serial.print("You typed: " );
    Serial.println(input);

    if(input==password)
    {
      Serial.println("Correct Password");
    }
    else
    {
      Serial.println("Wrong Password");
    }   
}

So we used what we explained in lesson 2! How to send data in input to Arduino! (complete tutorial). Now you can insert the password in the serial monitor.

If it is correct you will read “Correct Password” if it is not correct you will read “Wrong Password“.

Cool, but not enough.

Step 2 – Add a Servo

We want to add that a Servo has to make a movement if the password is correct, and another movement if it is wrong.

So now we have to had a servo to our circuit.

I don’t share the schematics because you have to try to make yourself, if you don’t know how to make it, just read the lesson in which we explain the module that you are going to add to the circuit, you will find there the schematic!

#include <Servo.h>

String password;
String input;

const int servoPin = 9;

Servo myServo;
 
void setup() 
{
    Serial.begin(9600);
    
    myServo.attach(servoPin);
    myServo.write(0);

    password = "pass";
}
 
void loop()
{
  if(Serial.available())
  {
    input = Serial.readStringUntil('\n');
    Serial.print("You typed: " );
    Serial.println(input);

    if(input==password)
    {
      Serial.println("Correct Password");
      myServo.write(90);  
    }
    else
    {
      Serial.println("Wrong Password");
      myServo.write(0);
    }
  } 
}

Now if you type a correct/wrong password, the servo will make a different movement.

Step 3 – Add a Button to change the password

Now we can add a button that will help us changing the password.

So we add to the code the button parts

#include <Servo.h>

String password;
String input;

const int buttonPin = 2;
const int servoPin = 9;

Servo myServo;
 
void setup() 
{
    Serial.begin(9600);
    pinMode(buttonPin, INPUT);
    
    myServo.attach(servoPin);
    
    password = "pass";
    myServo.write(0);
}
 
void loop()
{
  if(digitalRead(buttonPin))
  {
      if(Serial.available())
      {
        password = Serial.readStringUntil('\n');
        Serial.print("You typed: " );
        Serial.println(password);
      }   
  }

  if(Serial.available())
  {
    input = Serial.readStringUntil('\n');
    Serial.print("You typed: " );
    Serial.println(input);

    if(input==password)
    {
      Serial.println("Correct Password");
      myServo.write(90);  
    }
    else
    {
      Serial.println("Wrong Password");
      myServo.write(0);
    }
  } 
}

Now you can change the password by keeping the button pressed!

If you don’t keep it pressed, it won’t work.

Step 4 – Add the buzzer and LEDs

Now we had the Buzzer and the LEDs to make it a bit cooler with new effects.

Very easy to make. Now we will have a complete code.

#include <Servo.h>

String password;
String input;

const int buttonPin = 2;
const int servoPin = 9;
const int ledPin1 = 13;
const int ledPin2 = 3;
const int buzzerPin = 6;

Servo myServo;
 
void setup() 
{
    Serial.begin(9600);
    pinMode(buttonPin, INPUT);
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(buzzerPin, OUTPUT);
    
    myServo.attach(servoPin);
    
    password = "pass";
    myServo.write(0);
}
 
void loop()
{
  if(digitalRead(buttonPin))
  {
      if(Serial.available())
      {
        password = Serial.readStringUntil('\n');
        Serial.print("You typed: " );
        Serial.println(password);
        digitalWrite(ledPin1, HIGH);
        delay(1000);
        digitalWrite(ledPin1, LOW);
      }   
  }

  if(Serial.available())
  {
    input = Serial.readStringUntil('\n');
    Serial.print("You typed: " );
    Serial.println(input);

    if(input==password)
    {
      Serial.println("Correct Password");
      myServo.write(90);  
      digitalWrite(ledPin2, HIGH);
      delay(1000);
      digitalWrite(ledPin2, LOW);
    }
    else
    {
      Serial.println("Wrong Password");
      tone(buzzerPin, 150, 1000);
      myServo.write(0);
    }
  } 
}

If you want you can had an LCD, you can find how to had it in the first lesson -> read here!

See you in the Lesson 5! for other arduino basics !!

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: