Tuesday, 25 May 2021

LAMBDA EXPRESSIONS

History behind lambda expressions.

In the mathematics there is one concept called as lambda calculus. This Lambda calculus made the big change in the mathematical world ,in 1930's the first time lambda calculus was introduced by Alonzo Church into the mathematical world. By using the lambda expressions very big and difficult mathematical problems where solved very easily because of benefits of lambda calculus the programmer also started the use of lambda expressions .LISP is first programming language to use lambda expressions.

What are Lambda expressions?

A lambda expression is a short block of code that takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method. Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces.

Lambda expressions are added in Java 8 and provide below functionalities.

  • Enable functional programming in java.
  • write readable, maintainable and concise code.
  • To use API's very easily and effectively.
  • To enable parallel processing.
  • Enable to treat functionality as a method argument, or code as data.
  • A function that can be created without belonging to any class.
  • A lambda expression can be passed around as if it was an object and executed on demand.

Requirements of lambda expressions:

  1. It should be Anonymous ( means the functions should not contain the name).
  2. There should not be any return type.
  3. There should not be any modifier.

Syntax :

                 lambda operator -> body

where lambda operator can be:
  1. Zero parameter:
() -> System.out.println("Zero parameter lambda");
2.One parameter:
(a) -> System.out.println("The value is: " + a);
3.Multiple parameters:
(a1, a2) -> System.out.println("Multiple parameters: " + a1 + ", " + a2);

Examples of lambda expression in java:

1.Lets see the normal code without using Lambda expression to print "HELLO WORLD" how much lines it takes and lets compare it with code using the lambda expression .
Normal method:
                         public void normal()
                         {
                              System.out.pritnln("HELLO WORLD");
                          }
This is normal method now to convert this code to  Lambda expression there should not be any name ,modifier, and return type . And use indication by using symbol -> which tell that this method is lambda expression. If there is only one statement then there is no need to use curly brackets. So our lambda expression will be as below:
 Lambda Expression:
                         ()->System.out.println("HELO WORLD");
2.Lets take another example to print multiplication of two integers.
Normal method:
                         public void normal(int a, int b)
                        {
                            System.out.println(a*b);
                        }
Lambda Expression:
                        (a,b)->System.out.println(a*b);
When we are using Lambda expression the complier automatically guess the type of parameter you pass automatically know the type whether it is integer ,float ,character or any other.
3.Now let know how to return any value .
Normal method:
                         public int normal(int a, int b)
                          {
                                   return a*b;
                          }
Lambda Expression:
                         (a,b)->a*b;
Here complier will automatically guess that user want to return the value. There is no need to write return here if you use then you should also use curly brackets even though there is only one statement.
4.Now write code to return cube of given number
Normal method:
                          public int normal(int a)
                          {
                               return a*a*a;
                           }
Lambda Expression:
                           a->a*a*a;
when there is only one argument then there is no any need to use parenthesis ().


Important points:

  • The body of a lambda expression can contain zero, one or more statements.
  • When there is a single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.
  • When there are more than one statements, then these must be enclosed in curly brackets (a code block) and the return type of the anonymous function is the same as the type of the value returned within the code block, or void if nothing is returned.
References:-
1)https://www.w3schools.com › java
Java Lambda Expressions - W3Schools
2)Understanding the Use of Lambda Expressions in Java
DAVOOD MAZINANIAN∗,Concordia University, Canada AMEYA KETKAR∗
,Oregon State University, USA NIKOLAOS TSANTALIS, Concordia University, Canada DANNY DIG, Oregon State University, USA
3)https://youtu.be/bnuQpz-DK3w

Monday, 22 March 2021

Electronics bell using ardunio

ELECTRONIC BELL USING ARDUINO
        We are living in the automation world, where everything in industrial and home automation is getting automated with the help of an advanced programmable controller. An automatic bell system for schools or institutions reduces the effort necessary to control an electric bell manually that gives alarm for certain intervals of time based on school or college timings. The proposed system uses a simple basic Arduino to make the product affordable. Generally conservative methods need a peon or bell operator to control the bell system for every class and intermission in schools and also institutions. Such systems require plenty human efforts to do so, and need progress in order to become automatic – the ones that reduce human efforts.
          A bell is a percussion instrument used in schools that tells the students when it is time to go to class in the morning and when it is time to change classes during the day. The bell is an important instrument in both primary and secondary schools and even in the industries and other businesses where the bell timer plays a critical role in running the day .Bells are also associated with clocks indicating the hour by ringing. In any school, the classes are organized in periods and beginning of a period or break is alerted to the students and teachers by ringing the school bell. Conventionally, the school bell is rang by a peon or multi-tasking assistant but the margin of error where more. To reduce the margin and efforts of human we created automatic bell using Arduino. Basically it includes Arduino UNO as a controller, RTC module which tells real time, Relay module to operate AC machines on DC signal which connects electric bell to AC mains and the electric bell as a output. We designed one program which includes all the time table of the school .As we connect the bell to the AC mains according to the program it will start ringing and after certain time of interval it will stop automatically .The time of start and stop are included in the program it will not require any human efforts to start or stop the bell it is fully automatic.  
The working of this Automatic bell system starts with Real time clock module DS3231. This module feeds Arduino with real time and keeps track of it. Through the process of polling Arduino will frequently get the time and date values from this chip. Also user can change the time, date, month and year simply by editing the code. The automatic bell timer is activated by means of a Relay. Adding a flywheel diode will act as a protection to the rest of circuit when the relay is turned OFF. The required libraries are imported like DS3231.h for handling RTC module. The baud rate for data transmission is set to 9600 bits per second using Serial.begin() function . The Arduino Board is programmed using the Arduino IDE software.
   
Code:-
#include <DS3231.h>

int Relay = 13;

DS3231 rtc(SDA, SCL);
Time t;

 int OnHour ;
 int OnMin ;
 int OnSec ;
 int OnDay;
 int OffHour ;
 int OffMin ;
int OffSec ;

void setup() 
{
  Serial.begin(9600);
  rtc.begin();
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, LOW);
}

void loop() 
{
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.print(t.sec);
  Serial.print(" sec(s), ");
   Serial.print(t.dow);
  Serial.print(" day(s), ");
  Serial.println(" ");
  delay (1000);
  if 
  (OnDay==MONDAY||TUESDAY||WEDNESDAY||THURSDAY||FRIDAY)
  { 
   OnHour=11;
 OnMin=35;
  OnSec=5;
 
  OffHour=11;
  OffMin=35;
  OffSec=15;
  
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }

OnHour=8;
  OnMin=0;
  OnSec=5;
  
  OffHour=8;
  OffMin=0;
  OffSec=15;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
OnHour=9;
  OnMin=0;
  OnSec=0;
  OffHour=9;
  OffMin=0;
  OffSec=10;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
    OnHour=10;
  OnMin=0;
  OnSec=0;
  OffHour=10;
  OffMin=0;
  OffSec=10;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
    OnHour=11;
  OnMin=0;
  OnSec=0;
  OffHour=11;
  OffMin=0;
  OffSec=20;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
    OnHour=11;
  OnMin=30;
  OnSec=0;
  OffHour=11;
  OffMin=30;
  OffSec=10;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
      OnHour=12;
  OnMin=30;
  OnSec=0;
  OffHour=12;
  OffMin=30;
  OffSec=10;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
     OnHour=13;
  OnMin=30;
  OnSec=0;
  OffHour=13;
  OffMin=30;
  OffSec=20;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
     OnHour=14;
  OnMin=00;
  OnSec=0;
  OffHour=14;
  OffMin=00;
  OffSec=10;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
    OnHour=15;
  OnMin=00;
  OnSec=0;
  OffHour=15;
  OffMin=00;
  OffSec=10;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
    
 OnHour=16;
  OnMin=00;
  OnSec=0;
  OffHour=16;
  OffMin=00;
  OffSec=15;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
    
OnHour=16;
  OnMin=15;
  OnSec=0;
  OffHour=16;
  OffMin=15;
  OffSec=10;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
    OnHour=17;
  OnMin=15;
  OnSec=0;
  OffHour=17;
  OffMin=15;
  OffSec=10;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
      OnHour=18;
  OnMin=15;
  OnSec=0;
  OffHour=18;
  OffMin=15;
  OffSec=20;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
  }
    else 
    (OnDay==(SATURDAY));
    {
     OnHour=23;
     OnMin=59;
  OnSec=0;
  OffHour=0;
  OffMin=0;
  OffSec=20;
if
  (t.hour == OnHour && t.min == OnMin && t.sec==OnSec){
    digitalWrite(Relay,HIGH);
    Serial.println("LIGHT ON");
    }
    
    else if(t.hour == OffHour && t.min == OffMin && t.sec==OffSec){
      digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
    }
    }
if
  (OnDay==SUNDAY)
  {
     digitalWrite(Relay,LOW);
      Serial.println("LIGHT OFF");
  }
}

LAMBDA EXPRESSIONS

History behind lambda expressions. In the mathematics there is one concept called as lambda calculus. This Lambda calculus made the big chan...