Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Arduino for Kids

You're reading from   Arduino for Kids A cool guide to help kids develop robots and electronics

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781785884818
Length 218 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Authors (3):
Arrow left icon
Rishi Gaurav Bhatnagar Rishi Gaurav Bhatnagar
Author Profile Icon Rishi Gaurav Bhatnagar
Rishi Gaurav Bhatnagar
Priya Kuber Priya Kuber
Author Profile Icon Priya Kuber
Priya Kuber
Vijay Varada Vijay Varada
Author Profile Icon Vijay Varada
Vijay Varada
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. The World around Us FREE CHAPTER 2. Systems and Logic 3. Components and Connections 4. The Magic Wand 5. Hello World! 6. Safety Box 7. Make a Friend 8. Save Energy 9. High 5! 10. Plant, Meet Arduino!

High five!

Now you have everything you need to create your High-fiving robot!

We will use the ultrasonic proximity sensor we used in the previous chapter, along with conditional statements and relational operators to get everything working the way it should:

#include <NewPing.h> 
#include <Servo.h>  
  
 
NewPing sonar(2, 3); 
Servo myservo; 
  
void setup() { 
myservo.attach(9); 
} 
  
void loop() { 
  int dist = sonar.ping_cm(); 
  if (dist<15) 
  { 
    myservo.write(90); 
    delay(1000); 
  } 
  else  
  { 
    myservo.write(0); 
    delay(1000); 
  } 
} 

We begin by including the necessary libraries, our ultrasonic library, and the Servo library and initialize objects: sonar and myservo respectively. We attach our ultrasonic sensor to pins 2 and 3 of our Arduino and the servo to pin 9.

We then read the value of our distance sensor. To check if your hand is close, use an if conditional statement along with a lesser than (<) relational operator. Thus, if an object is...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image