Tuesday 29 September 2015

Create an EXE in Visual Basic 6.0 To Control Arduino Projects



Create an EXE Visual Basic 6.0 
To
 Control Arduino


Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:
twitter.com/zakimuslim_

Theres have a little bit different between Visual Basic 6.0 and Visual Basic 2008. If you want to learn how to use serial communication and create an EXE in Visual Basic 6.0 here is video that i've make to help you in coding and how to find serial comm component in VB 6.0.

If you still dont have Visual Basic 6.0 software, you can download here for free!!
VB 6.0 Software:
https://drive.google.com/open?id=0B3c_VPBO9Qq3cHROTW5ydkVXOU0


How to find CD key!!










You need to watch on youtube because i've some problem during uploading this video on my blog :(
Tutorial Video!


VIDEO

VB 6.0 tutorial:
https://www.youtube.com/watch?v=uoyCcGM9nDA&feature=youtu.be



Arduino Code

You can copy and paste this code or directly download HERE ...
Modify this code if needed!



int led = 13;
String incoming;

void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop()
{
  if(Serial.available() > 0)
  {
    incoming = Serial.readString();
    if(incoming == "Led ON")
    {
      digitalWrite(led, HIGH);
      Serial.println("LED is ON....");
    }
    else if(incoming == "Led OFF")
    {
      digitalWrite(led, HIGH);
      Serial.println("LED turning OFF.......");
    }
  }
}


Code Explaination

String incoming;
-string that vb send to arduino.

int led = 13;
-tell arduino i attach led on pin 13

Serial.begin(9600);
-Baud rate. Communication speed between Arduino and PC

pinMode(led, OUTPUT);
-set led as output

if(Serial.available() > 0)
-when serial is available. If Arduino read any byte

incoming = Serial.readString();
-read serial in string.

if(incoming == "TURN ON")
-if incoming string that arduino read is "TURN ON"

digitalWrite(led, HIGH);
-Arduino will turn on led on pin13





//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


Visual Basic 2008



You also can learn how to use serial comm between Arduino and VB 2008 here. I also post my Arduino basic code to blink a LEDs using VB 2008.
VB 2008 tutorial:
http://ohhmyproject.blogspot.my/2015/09/control-arduino-using-visual-basic-2008.html 




Download Link

Arduino Code VB 6.0:
https://drive.google.com/open?id=0B3c_VPBO9Qq3VG0yY3hUbldScGc 


Visual Basic 6.0 software:
https://drive.google.com/open?id=0B3c_VPBO9Qq3cHROTW5ydkVXOU0

Sunday 27 September 2015

Control Arduino Using Visual Basic 2008


Visual Basic 2008 With Arduino

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:

i recommend watch this video on youtube for better quality
https://www.youtube.com/watch?v=OBJdnFVBfvY





Arduino Code



String incoming;
int led = 8;

void setup()
{
  Serial.begin(9600);
  pinMode(led, OUTPUT);
}

void loop()
{

  if(Serial.available() > 0)
  {
    incoming = Serial.readString();  //<<---this is the most important
    if(incoming == "TURN ON")
    {
      digitalWrite(led, HIGH);
    }
    else if(incoming == "TURN OFF")
    {
      digitalWrite(led, LOW);
    }
  }
}



Code Explaination

String incoming;
-string that vb send to arduino.

int led = 8;
-tell arduino i attach led on pin 8

Serial.begin(9600);
-Baud rate.

pinMode(led, OUTPUT);
-set led as output

if(Serial.available() > 0)
-when serial is available. If Arduino read any byte

incoming = Serial.readString();
-read serial in string.

if(incoming == "TURN ON")
-if incoming string that arduino read is "TURN ON"

digitalWrite(led, HIGH);
-Arduino will turn on led on pin8




Download link

Arduino Code:
VB 2008 Code:

Sunday 20 September 2015

Create Android Apps to Control Arduino

HOW TO CREATE ANDROID APP TO
CONTROL ARDUINO PROJECTS





In this tutorial i use:
-Arduino board
-HC06 Bluetooth Receiver
-LED
-Jumper cables

In this video i use Google Chrome browser to create the APK.
Before you start creating an application for android you need to go this website
http://appinventor.mit.edu/explore/

Click on the "Create apps!" at top  right.

Start Create App Now!




and log in with your gmail to create an account.
Then watch this video for the next step.

i recommend watch this video on youtube for better quality:









After you finished create and install the apps on your android, wire up HC06, LED, and Arduino like this.

Dont forget to connect 5V and Gnd pin from Arduino to breadboard!







Then copy and paste this Arduino Code and upload to your Arduino board!
Or you can directly download the code here  <<<<<<<<<<<<<<<<Download Code
Its very simple code. Modify the code if you want!






Remember! Disconnect jumper cables at TX and RX pin on Arduino board when you upload the code!






Now you ready test the code and apps together!


First turn on your device's bluetooth.Pair your device's bluetooth connection with HC06. If it request the pin number, the pin is 1234. Your need to do this for the first you connect your android and HC06








Second, open the apps that you have installed on your android, and
click on "Select Bluetooth Client"



Now you can see the bluetooth client named "HC06"






Click on it to connect with your Arduino board






 Now your android connected to your Arduino board,
You can see the "Connected To Arduino" on your screen









How your app? running smooth?
You can try this apps that i've make to control the blender. You can download the APK and Arduino code. Go to the bottom of this page and you can see "Download Link". Its work perfectly on my ASUS T00K.








Circuit...






HIGH VOLTAGE!

To handle high voltage/current you need a RELAY to seperate them. Arduino just can handle 5v voltage.
With relay you can separate high current and high voltage. This can avoid your Arduino from toasted!

You need a relay 5V 220V.
Its stand for 5V input,  220V output.
The coil of relay need 5vdc to switch from normally close state(NC)  to  normally open(NO) state.


this type of relay that you can use:


In my case, i use this relay. It can triggered at 5vdc but need 9vdc power supply.
i bought this at Kytron, Seksyen15, Shah Alam, Malaysia.







Your circuit become easier if youre using relay module.

http://www.lelong.com.my/srd-5v-dc-5-pin-relay-mcs123-167817400-2016-09-Sale-P.htm

















Here my another simple projects that i've use MIT Inventor and HC06 Bluetooth Receiver.
I will make a tutorial for this project later :D




Another project. Tutorial already posted on my blog





Download Link

Arduino Code:

-LED Controller









Saturday 19 September 2015

Arduino + Robotis

Playing with 

Arduino + Robotis Parts

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:
twitter.com/zakimuslim_

i recommend watch this video on youtube for better quality:
https://www.youtube.com/watch?v=xmYYOmiEVDI







       Security Gates

In this post i just use serial monitor and keyboard.

This is really simple tutorial. Its take around 30minutes.
In this tutorial, i use:
 -Arduino Mega 2560                                
 -Robotis Play600 parts.
 -Motor Driver L293D
 -Red and Green LED.
 -Breadboard
 -Jumper Cables


Actually Robotis Play600 Pets looks so boring, then i just wanna make it become more interesting by using Arduino. I think this robotis play600 cheaper than other robotis model. I dont know how much because i got this for free. I got this during lucky draw Fira Roboworld Cup. I'm so lucky.  So i make this during my weekend......








1. Open the body and solder two female jumper cables at the battery casing.

     Like this!

    -Unscrew the screw and open this part





               -Solder the jumper cables to DC motor cables(at battery casing). I do this because i want to
                drive the DC motor inside using a motor driver.
                Maybe this image can help you to understand what i mean :)












After finish, assemble and screw them up!


2. Wiring between Arduino, Motor Driver, and Robotis

Connect the wiring like this.
In my video i use only 5V power supply to power up the motor. I'm not using any battery, i use from Arduino 5V pin.
Make sure your circuit is correct.

Dont forget to include the Green and Red LED in the circuit!






3. Assemble your Robotis Parts

  
 -Assemble them as creative as creative can.

                                                                      this is mine.




4. Arduino Code

Once you've finished, now you ready to upload the code!
This is my code. You can copy and paste or directly download here .



String password_input;
String pass = "zaki";
int gateup = 7;
int gatedown = 8;
int stopp = 9;
int go = 10;

void setup()
{
  Serial.begin(9600);     //baud rate
  pinMode(gateup, OUTPUT);     //pin for gate up
  pinMode(gatedown, OUTPUT);   //pin for gate going down
  pinMode(stopp, OUTPUT);      //red led pin
  pinMode(go, OUTPUT);         //green led
  Serial.println("Enter Password");
}
void loop()
{
  digitalWrite(stopp, HIGH);
  if (Serial.available() > 0)
  {
    password_input = Serial.readString();  //password key in by user
    if (password_input == pass)            //if password is correct
    {
      digitalWrite(stopp, LOW);           //red led OFF
      delay(500);
      digitalWrite(go, HIGH);             //green led is high
      Serial.print("Password Received: "); //print on serial monitor
      Serial.println(password_input);
      delay(500);
      analogWrite(gateup, 180);           //open the gate
      Serial.println("Gate Open");
      delay(2500);
      digitalWrite(gateup, LOW);         //gate open for 2 seconds
      Serial.println("delay 2 seconds......");
      delay(1500);
      digitalWrite(go, LOW);  //green led OFF
      delay(500);
      digitalWrite(stopp, HIGH);  //red led high
      delay(500);
      Serial.println("Gate Closed!");
      analogWrite(gatedown, 150);     //close the gate until get the correct password
      delay(650);
      digitalWrite(gatedown, LOW);
      delay(500);
      Serial.println("Please Enter Password"); //ask user to key in the password again
    }
    else 
    {
      Serial.print("Password Received: ");     //if the password is invalid
      Serial.println(password_input);
      Serial.println("Invalid Password!");
      Serial.println("Please enter the password again");
    }
  }
}




Code Explaination


String password_input;
-this string use for storing the password that user key in

String pass = "zaki";
-this string is the password. User need to key in "zaki" at serial monitor to open the gate.
-you can change the password with modify this string like this:
String pass = "the password you want";
example:
String pass = "vnsicxx34kjx";

int gateup = 7;
-pin on arduino that i use to open the gate.

int gatedown = 8;
-pin on arduino that i use to close the gate.

int stopp = 9;
-pin on arduino that i use to Red LED.

int go = 10;
-pin on arduino that i use to Green LED.

Serial.begin(9600);    
-baud rate

pinMode(gateup, OUTPUT);
-Set pin gateup as output

pinMode(gatedown, OUTPUT);
-Set pin gatedown as output

pinMode(stopp, OUTPUT);
-set red led pin as output

pinMode(go, OUTPUT);
-set green led pin as output

Serial.println("Enter Password");
-ask user to enter the password on serial monitor

digitalWrite(stopp, HIGH);
-Red LED always ON because gate always close

if (Serial.available() > 0)
-If serial communication with Arduino is available(check), all code ready to execute. If not, code
  never to execute

password_input = Serial.readString();
-read the password that key in by user in string

if (password_input == pass)
{
      digitalWrite(stopp, LOW);           //red led OFF
      delay(500);
      digitalWrite(go, HIGH);             //green led is high
      Serial.print("Password Received: "); //print on serial monitor
      Serial.println(password_input);
      delay(500);
      analogWrite(gateup, 180);           //open the gate
      Serial.println("Gate Open");
      delay(2500);
      digitalWrite(gateup, LOW);         //gate open for 2 seconds
      Serial.println("delay 2 seconds......");
      delay(1500);
      digitalWrite(go, LOW);  //green led OFF
      delay(500);
      digitalWrite(stopp, HIGH);  //red led high
      delay(500);
      Serial.println("Gate Closed!");
      analogWrite(gatedown, 150);     //close the gate until get the correct password
      delay(650);
      digitalWrite(gatedown, LOW);
      delay(500);
      Serial.println("Please Enter Password"); //ask user to key in the password again
    }

-All of this code will be execute if the user enter the correct password




else 
    {
      Serial.print("Password Received: ");     //if the password is invalid
      Serial.println(password_input);
      Serial.println("Invalid Password!");
      Serial.println("Please enter the password again");
    }

-this code will be executed if user enter the incorrect password
-print "Invalid Password" and "Please enter the password again"






If you have any problem with wiring, you can refer my older post about L293D and Blink More Than 1 LEDs 

or directly pm me on
twitter @zakimuslim_ 
facebook Mohammad Zaki 



Download Link

Wednesday 16 September 2015

Arduino + 7 Segment

Arduino + 7 Segment

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:

In this post i use :
-Arduino Mega 2560
-5101AS 7 segment
-Breadboard
-Jumper cables.





5101AS 7 segment








Okay before we start wire up the 7 segment, we should know which one pin is "common" pin.

In my case, i use 5101AS model. So when i check the datasheet, i get this....

Common(com) is at the middle. Labelled as "com".

okay now lets go the next step..


wire up your component like this.
remember! dot always at the downside





After finished, open your Arduino IDE and copy and paste this code or just download it here


void setup() {
  pinMode(2, OUTPUT);   //set all pin that i've use as output pin
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  
}

void loop() {
  one();               //tunjuk nombor 1
  delay(1000);    //display number 1 for 1000miliseconds(1second)
  two();
  delay(1000);
  three();
  delay(1000);
  four();              //display number 4
  delay(1000);    //display number 4 for 1second
  five();
  delay(1000);
  six();
  delay(1000);
  seven();
  delay(1000);
  eight();            //keluar nombor 8
  delay(1000);   //akan keluar nombor 8 selama 1 saat
  nine();
  delay(1000);

}



/* modify code here to display another number or characters*/

void one(void)                 //<------------void to display number 1
{
  digitalWrite(2, LOW);      //centre
  digitalWrite(3, HIGH);     //top left
  digitalWrite(4, LOW);      //top
  digitalWrite(5, LOW);      //top right
  digitalWrite(6, HIGH);     //bottom left
  digitalWrite(7, LOW);      //bottom
  digitalWrite(8, LOW);      //bottom right
  digitalWrite(9, HIGH);     //dot
}



void two(void)          //<---------------- void to display number 2
{
  digitalWrite(2, HIGH);      //centre
  digitalWrite(3, LOW);     //top left
  digitalWrite(4, HIGH);      //top
  digitalWrite(5, HIGH);      //top right
  digitalWrite(6, HIGH);     //bottom left
  digitalWrite(7, HIGH);      //bottom
  digitalWrite(8, LOW);      //bottom right
  digitalWrite(9, HIGH);     //dot
}


void three(void)    //<------- call this void to dsplay number 3
{
  digitalWrite(2, HIGH);      //centre
  digitalWrite(3, LOW);     //top left
  digitalWrite(4, HIGH);      //top
  digitalWrite(5, HIGH);      //top right
  digitalWrite(6, LOW);     //bottom left
  digitalWrite(7, HIGH);      //bottom
  digitalWrite(8, HIGH);      //bottom right
  digitalWrite(9, HIGH);     //dot
}


void four(void)      //<----diguna kan untuk keluar kan nombor 4. 
{
  digitalWrite(2, HIGH);      //centre
  digitalWrite(3, HIGH);     //top left
  digitalWrite(4, LOW);      //top
  digitalWrite(5, HIGH);      //top right
  digitalWrite(6, LOW);     //bottom left
  digitalWrite(7, LOW);      //bottom
  digitalWrite(8, HIGH);      //bottom right
  digitalWrite(9, HIGH);     //dot
}


void five(void)
{
  digitalWrite(2, HIGH);      //centre
  digitalWrite(3, HIGH);     //top left
  digitalWrite(4, HIGH);      //top
  digitalWrite(5, LOW);      //top right
  digitalWrite(6, LOW);     //bottom left
  digitalWrite(7, HIGH);      //bottom
  digitalWrite(8, HIGH);      //bottom right
  digitalWrite(9, HIGH);     //dot
}


void six(void)
{
  digitalWrite(2, HIGH);      //centre
  digitalWrite(3, HIGH);     //top left
  digitalWrite(4, HIGH);      //top
  digitalWrite(5, LOW);      //top right
  digitalWrite(6, HIGH);     //bottom left
  digitalWrite(7, HIGH);      //bottom
  digitalWrite(8, HIGH);      //bottom right
  digitalWrite(9, HIGH);     //dot
}


void seven(void)
{
  digitalWrite(2, LOW);      //centre
  digitalWrite(3, LOW);     //top left
  digitalWrite(4, HIGH);      //top
  digitalWrite(5, HIGH);      //top right
  digitalWrite(6, LOW);     //bottom left
  digitalWrite(7, LOW);      //bottom
  digitalWrite(8, HIGH);      //bottom right
  digitalWrite(9, HIGH);     //dot
}


void eight(void)
{
  digitalWrite(2, HIGH);      //centre
  digitalWrite(3, HIGH);     //top left
  digitalWrite(4, HIGH);      //top
  digitalWrite(5, HIGH);      //top right
  digitalWrite(6, HIGH);     //bottom left
  digitalWrite(7, HIGH);      //bottom
  digitalWrite(8, HIGH);      //bottom right
  digitalWrite(9, HIGH);     //dot
}


void nine(void)
{
  digitalWrite(2, HIGH);      //centre
  digitalWrite(3, HIGH);     //top left
  digitalWrite(4, HIGH);      //top
  digitalWrite(5, HIGH);      //top right
  digitalWrite(6, LOW);     //bottom left
  digitalWrite(7, HIGH);      //bottom
  digitalWrite(8, HIGH);      //bottom right
  digitalWrite(9, HIGH);     //dot
}





Code explaination


  pinMode(2, OUTPUT);   
- to set all pin that i've use as output pin

one();
-called void "one" to display number 1 on 7 segment.

five();
-called void "five" to display number 5 on 7 segment.

delay(1000);
-to display the number on 7 segment for 1 second

void one(void) 
{
  digitalWrite(2, LOW);      //centre
  digitalWrite(3, HIGH);     //top left
  digitalWrite(4, LOW);      //top
  digitalWrite(5, LOW);      //top right
  digitalWrite(6, HIGH);     //bottom left
  digitalWrite(7, LOW);      //bottom
  digitalWrite(8, LOW);      //bottom right
  digitalWrite(9, HIGH);     //dot
}
-this is void wheres i use it to create the number


for example;

digitalWrite(8, HIGH);      //bottom right

-on 7 segment will show this








Download Link

Arduino Code:
https://drive.google.com/open?id=0B3c_VPBO9Qq3aUNnMXVIbEc3N3M







Saturday 12 September 2015

Arduino With GPS SKM53

    GPS WITH ARDUINO MEGA 2560

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:


1. Where to buy this SKM53 GPS ?



Memang dari kedai beli SKM53 ni akan dapat cam ni je. Kaki dia tak ngam dengan breadboard & jumper cable. Kena beli jumper cable lain sikit dari biasa.










So, i solder this component with jumper cable.













This how to connect SKM53 to Arduino



VCC >  +5V
Gnd > Gnd
TX > digital pin 10
RX > digital pin 11

Lets go to the next steps.


Software

In this test i use TinyGPS and SoftwareSerial library. I've make a little bit changes with original code to make user easier to search their current position in Google Map.


Download Link

TinyGPS Library

Arduino Code






Arduino Code

Just copy and paste this code! Make sure you have added TinyGPS library first.
Check my older post (under ultrasonic sensor post) for tutorial adding library to Arduino IDE!


#include <SoftwareSerial.h>
#include <TinyGPS.h>   //include library

long lat, lon;

SoftwareSerial gpsSerial(10, 11);  //connect Tx and Rx pin on digital 10 and 11
TinyGPS gps;

void setup() {
  Serial.begin(9600);    //baud rate with serial monitor
  gpsSerial.begin(9600);  //GPS baud rate
}
void loop() {
  while (gpsSerial.available()) {      ///kalau GPS available, Arduino akan mula read
    if (gps.encode(gpsSerial.read()))  ///Encode signal dari GPS
    {
      gps.get_position(&lat, &lon);     ///lepas Arduino encode signal GPS, baru akan dapatkan global position
      float real_lat = (lat/1000000.000);   //divide by 1m to make user easier to get their current global postion
      float real_lon = (lon/1000000.000);
      Serial.print("google: ");
      Serial.print(real_lat);   //print latitude on Serial monitor (in two decimal places)
      Serial.print(", ");
      Serial.println(real_lon); //print longitude on serial monitor (in two decimal places)
      //////////////////////////////////////////////////////////////////////////////////////////////////
      Serial.print("Position: ");
      Serial.print("lat: ");
      Serial.print(lat);
      Serial.print(" ");
      Serial.print("lon: ");
      Serial.println(lon);
    }
  }
}




Code Explaination


#include <TinyGPS.h> 
-include TinyGPS library.

SoftwareSerial gpsSerial(10, 11);
-communication between arduino and skm53 on pin digital 10 and 11.

Serial.begin(9600);    
-baud rate with serial monitor

gpsSerial.begin(9600);
-GPS baud rate

while (gpsSerial.available())
-check if GPS available

if (gps.encode(gpsSerial.read()))
-encode GPS signal

gps.get_position(&lat, &lon);
-get the position

 float real_lat = (lat/1000000.000);
-convert latitude in two decimal places to make user easier search their curent global postion

 float real_lon = (lon/1000000.000);
-convert longitude in two decimal places to make user easier search their curent global postion

Serial.print(real_lat);
-print latitude on serial monitor

 Serial.println(real_lon);
-print longitude on serial monitor

Ultrasonic Sensor HC- SR04 And How To Add Library To Arduino

Ultrasonic Sensor(HC-SR04)
Measure Distance/Detect Any Motion

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:
twitter.com/zakimuslim_

Based on my experience, this sensor need a good reflector to get better result. Reflector must be:
-Flat
-Hard
And size of the reflector must be bigger than this sensor size. It help sensor receive reflected signal better.


This how Ultrasonic sensor looks

VIDEO

i recommend watch this video on youtube for better quality:
https://www.youtube.com/watch?v=hCmmrtMHlVw&feature=youtu.be




Hardware Setup


This how to connect HC-SR04 Ultrasonic sensor to Arduino






How to measure the distance(cm) using Ultrasonic sensor using Arduino.

1. First of all you need to download the library for this sensor first.
   You can download here;

 https://drive.google.com/open?id=0B3c_VPBO9Qq3eWIyNi1maWN6R1U

This library can help you to get more  accurate measurement and also can make your program easier. But you need to add this library first before you can upload the program.


2. How to add library to Arduino 

-download the library and unzip the zip item.









-copy & paste the unzip item to desktop







-open Arduino IDE and click on "include library", then click on "Add .Zip Library.."







-then select the folder that tou have move to dekstop.







-once you click open, you succesful to add the library to Arduino. You can check the library by click on Sketch > Inculde Library. Then you can see the folder "New Ping"








3. Now you ready to upload the program.

    Here link to download the program:




   or you can just copy and paste this code to your ide





#include <NewPing.h>

#define TRIGGER_PIN   12 //trigger pin kat arduino 12
#define ECHO_PIN      11 //echo pin kat arduino 11
#define MAX_DISTANCE 80 //set jarak maksimum

int led = 7;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

unsigned int pingSpeed = 50; //speed bacaan ping
unsigned long pingTimer;     

void setup() {
  Serial.begin(115200); 
  pingTimer = millis(); // Start now.
  pinMode(led, OUTPUT);
}

void loop() {

  if (millis() >= pingTimer) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer += pingSpeed;      // Set untuk ping seterusnya.
    sonar.ping_timer(echoCheck); // Keluarkan ping seterusnya, call balik function echoCheck
  }
  int reading = (sonar.ping_result / US_ROUNDTRIP_CM); //tukar nama kepada bacaan
  if(reading <= 21)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

void echoCheck() { //Check Timer2 kalau ada interrupt 
  
  if (sonar.check_timer()) { // check ping yang diterima setiap 24uS
    Serial.print("Ping: ");
    Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); //tukar bacaan US_ROUNDTRIP_CM kedalam centimeter
    Serial.println("cm");
    int reading = (sonar.ping_result / US_ROUNDTRIP_CM);
    
  }
}




4. Sensor Reading

- To see the sensor reading you can press ctrl + shift + m  or click on serial monitor icon at the top left.
- I've make a little modification to the code. You can see the distance in "cm" at serial monitor.



5. Code explaination


#include <NewPing.h>
-Include the library. Tell arduino that youre using "NewPing" library.


#define TRIGGER_PIN   12 //trigger pin kat arduino 12
-use pin12 as trigger pin

#define ECHO_PIN      11 //echo pin kat arduino 11
-tell arduino pin11 is echo pin

#define MAX_DISTANCE 80 //set jarak maksimum
-set the maximum distance that youre need. I set it at 80cm

  Serial.begin(115200); 
-set baud rate at 115200. I use Arduino Mega 2560 so the baud rate is 115200. If youre using Arduino Uno set it at 9600.
  pingTimer = millis(); // Start now.
-set timer in milisecond

  pinMode(led, OUTPUT);
-set led pin as output. I use LED as output in this test.

  if (millis() >= pingTimer) {   // pingSpeed milliseconds since last ping, do another ping.
    pingTimer += pingSpeed;      // Set untuk ping seterusnya.
    sonar.ping_timer(echoCheck); // Keluarkan ping seterusnya, call balik function echoCheck
  }
-dont make any change at this if dont know what is this.



  int reading = (sonar.ping_result / US_ROUNDTRIP_CM); //tukar nama kepada bacaan
  if(reading <= 21)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

-if distance between sensor and reflector equal and less than 21cm, led is ON. If distance more than 22cm, led will turn OFF.


void echoCheck() { //Check Timer2 kalau ada interrupt 
  
  if (sonar.check_timer()) { // check ping yang diterima setiap 24uS
    Serial.print("Ping: ");
    Serial.print(sonar.ping_result / US_ROUNDTRIP_CM); //tukar bacaan US_ROUNDTRIP_CM kedalam centimeter
    Serial.println("cm");
    int reading = (sonar.ping_result / US_ROUNDTRIP_CM);
    
  }
}

-void to check the echo.




5. Download Link