Say hello to my little friend!

Now we have everything we need to know to start building our project. Let's start by writing the code:

#include <NewPing.h> 
  
int ledPin=10; 
int trigPin=8; 
int echoPin=9; 
 
NewPing sonar(trigPin, echoPin); 
  
 
void setup() { 
 
pinMode(ledPin,OUTPUT); 
 
} 
 
void loop() { 
  delay(50); 
  int dist = sonar.ping_cm(); 
  if (dist < 15) 
digitalWrite(ledPin,HIGH); 
    else 
         digitalWrite(ledPin,LOW); 
          
} 

Like before, we start by storing the pin numbers in variables, so that it doesn't become confusing, and then initialize our LED pins for output. The code consists of a simple if statement that checks if an object is closer than 15cm, and if it is, lights up our LED's. If not, it turns our LED's off.

Now that our code is ready, let's build our project!

We begin by cutting out a head shape out of cardboard:

Say hello to my little friend!

We pierce LED's through the eyes, that we have made on the face:

Say hello to my little friend!

We connect the circuit according to the following diagram. Note that the LED's are placed in series, where the positive of one LED is connected to the negative of the other:

Say hello to my little friend!

We also mount the head on a clay body we have made:

Say hello to my little friend!

Connect and place the proximity sensor appropriately along with your new friend. If everything is connected properly, and the Arduino is powered up, when you bring your hand close to the Proximity sensor, the eyes should glow!

Say hello to my little friend!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.147.190