Introduction: Chocolate Bar Dinosaur Watcher

First of all thanks to Mediatek and Instructables for sending me one LinkIt ONE. It's really a great board and I've had a lot of fun playing with it.

Now the project !!!

The main idea is to make this little toy dinosaur watch for your chocolates.

Step 1: Components Needed

For this project we'll need the next components. There are some explanations about what they do and some links with more information.

  1. Mediatek LinkIt ONE.
    The heart of the project. It will manage all the other components. It will wait for the change of state of the photoresistor and activate the motor inside the dinosaur through the L298.

    http://www.seeedstudio.com/wiki/LinkIt_ONE Here you can read how to set up your LinkIt ONE or you can follow the steps from another instructables.

  2. Keyes Photoresistor.

    It will detect if the chocolate bar is on place or not by detecting the amount of light.

  3. Keyes L298 Motor driver.
    It allow us start, stop and change the direction of the motor.
    A little battery to power the dinosaur it's also needed.

  4. A speaker and a power bank to power it.

  5. And finally a chocolate bar or something big enough to be on top of the photoresistor and cover it.

Step 2: Make the Connections

First, build your dinosaur solar toy and remove the solar panel. After that you can connect the toy to a little battery and you will see how its moves.

Later I will explain how to use the L298n motor drive to control it. In fact, the connections are very easy.

For the dinosaur growl sound I just downloaded from https://www.freesound.org/search/?q=dinosaur and set the little switch on the LinkIt ONE to MS (Massive Storage) instead of UART and just copied to the drive unit that appears on Windows when you plug it. That's it.

Of course, don't forget connect the speakers and their battery :P

Connect the photoresistor to the A0 analog input like that LinkIt ONE tutorial Light Sensor.

Connect the IN1 and IN2 from the L298n motor drive to the D8 and D9 from the LinkIt ONE and the ENA to the 5v. I don't know if it's a really good idea, because if you see the documentation of the L298n the ENA (Enable pin) allows you to control the speed of the motor with a PWM (Pulse With Modulation) signal, but works better than connecting directly to D3.

Done !!! Not big deal if you have already been playing with these things.

Next. Let's code.

Step 3: The Code

This code is very simple. Just three steps.

First a few setup.

#include < LAudio.h >

// These pins will control the L298
int IN1=8;
int IN2=9;

//int ENA=3; // It seems there's not supply power enough so I connect directly to 5V and it works better.

void setup() {
LAudio.begin();

pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);

}

//Then, in the main loop, we are reading the value of the photoresistor constantly and waiting for "light".
//When it comes then play the sound and start the dinosaur movement.

void loop()
{

int sensorValue = analogRead(A0);

if (sensorValue<300) {

LAudio.playFile( storageFlash,(char*)"dinosaur.wav");
LAudio.setVolume(6);

//analogWrite(ENA, 200); // motor speed
digitalWrite(IN1,LOW); // rotate forward
digitalWrite(IN2,HIGH);
delay(2000);
digitalWrite(IN1,HIGH); // rotate reverse
digitalWrite(IN2,LOW);
delay(2000);

}else

// Finally stop the dinosaur if there is no "light" in the sensor.

{
digitalWrite(IN1,LOW); // stop motor
digitalWrite(IN2,LOW); delay(2000);
}
}

// Just upload to the LinkIt ONE and It's done !!!

Whats next? Maybe connect it to the wifi and take the control of the dinosaur. There are some instructables here that sure will help. https://www.instructables.com/id/IoT-with-LinkIt-One-Relays/