Book Image

Arduino for Kids

By : Rishi Gaurav Bhatnagar, Vijay Varada
Book Image

Arduino for Kids

By: Rishi Gaurav Bhatnagar, Vijay Varada

Overview of this book

The mission of this book is to integrate technology with the tools that children already use for crafts so that they feel that the technology is an extension of their playtime. We use coding, sensors, and micro-controllers integrated with art and craft supplies, origami, and Playdough. There are 10 fun-filled chapters that talk to children directly, and give clear instructions for non-technical parents too. We use Arduino as the controller of choice due to its easy availability and large community. By the end of the book, children will comfortably be able to set up their Arduino, read and understand code, manipulate code, and ultimately write their own code for projects. They will also be able to use basic sensors and know how components connect to each other. All the learning takes place with lots of colorful pictures and the circuits are neatly presented using wiring.
Table of Contents (18 chapters)
Arduino for Kids
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

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 detected...