## Unit 20 – My digital pet 2/2

Let’s program our digital pet. It’s still a way to go.

We have to teach the digital pet some things. It should blink with the eyes (LEDs). It should purr when it’s stroked. It should grumble when it is ignored. Let’s start with the blinking.

Blinking eyes is actually not that different from the blinking LED example we discussed in the very beginning of that course. The LEDs can be connected to two digital Arduino pins. That means we need the commands pinMode and digitalWrite twice. The code could look like that:

```int ledPin1 = 10;
int ledPin2 = 11;

void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}

void loop() {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(5000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(50);
}```

Now, both LED blink always in in the same speed. That is a bit boring, no? We should randomly blink the LEDs. Fortunately, there is a random-command.

### Info: random();

The random(maximum)-command creates a random number between 0 and the as argument given number (maximum). This number is never reached. If you write random(5) the random results are 0, 1, 2, 3 and 4.

Let’s generate a random number between 0 and 500. The eyes (LEDs) should switch off when the random number equals 1. That will create random intervals for eye blinking. Now, we put in a small delay at the end of the program. That will give us the needed break to see, that there is something really happening.

```int ledPin1 = 10;
int ledPin2 = 11;

void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}

void loop() {
// LEDs on
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);

if (random(500)==1){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(50);
}

delay(10);
}```

### Purring

The digital pet should purr when it is stroked. To detect if it is stroked, we will use a digital sensor inside. When you stroke it, your hand will cover up some light and the sensor can detect that.

But at first Arduino hast to measure the normal light when the pet is not stroked. It can do it when it is connected to the USB but we have to code it. Let’s store that value into the variable lightNormal: lightNormal=analogRead(sensorPin);

The variable sensorThreshold is used to calibrate the brightness difference when it should start to purr. That is done with if(analogRead(sensorPin)> lightNormal + sensorThreshold){…}. It may be that you have to change the value. Just try out other values if it doesn’t purr.

That is the code without blinking eyes:

```int sensorPin = 0;
int sensorThreshold = 20;

int lightNormal;

void setup() {
}

void loop() {
// Schnurren
tone(speakerPin,0.001);
} else {
noTone(speakerPin);
}

delay(10);
}```

### Grumble

But what if nobody plays with the pet? When it is ignored for to long, it will play a little melody to get attention. We do it like we did with he blinking eyes. We use a random command. Of course, the pet must not grumble while it is stroked. So we have to connect two conditions. We can do that in the if-statement using the double et-sign (&&).

```int sensorPin = 0;
int sensorThreshold = 20;

int speakerPin = 9;

int lightNormal;

void setup() {
}

void loop() {

// Meckern
tone(speakerPin,1200);
delay(100);
tone(speakerPin,800);
delay(200);
tone(speakerPin,1000);
delay(200);
tone(speakerPin,1200);
delay(100);
}

delay(10);
}```

If your digital pet should grumble or not is of course up to you ;-)

Here you can find the complete code for the digital pet:

```int ledPin1 = 10;
int ledPin2 = 11;

int sensorPin = 0;
int sensorThreshold = 20;

int speakerPin = 9;

int lightNormal;

void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);

}

void loop() {
// LEDs on
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);

if (random(500)==1){
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
delay(50);
}

// purring
tone(speakerPin,0.001);
} else {
noTone(speakerPin);
}

// grumbling
tone(speakerPin,1200);
delay(100);
tone(speakerPin,800);
delay(200);
tone(speakerPin,1000);
delay(200);
tone(speakerPin,1200);
delay(100);
}

delay(10);
}```

Wow! That was complicated. Time to tinker something!

Instruction

### 1

Print it on a a heavy paper or cardboard.

### 2

Take the first sheet and cut along the solid lines.

c

### 3

Fold along all dashed lines. That works better, when your carve it carefully before.

### 4

Cut out the second sheet.

### 5

Mind the cut out in the middle.

### 6

Now again, fold along the dashed lines.

### 7

Cut the holes for the eyes. Be careful.

### 8

Glue it together like shown on the picture. You can either use a glue stick or liquid paper adhesive.

### 9

Now, stick the backside to the frontside.

Close it.

### 11

The ears of the pet are a bit difficult to glue. Best, use a pencil to push against it from the inside.

### 12

Time to put in the electronics. Place the paper model on top of the breadboard. The wires can be bended a bit and put through the cut on the side.

### 13

Put the LEDs into the cut outs of the eyes. Bend the legs down. (If your calbes are to short, put something unter the breadboard to lift it up.)

### 14

Stick the legs onto the paper by using some tape.

### 15

Of course you should paint the pet in your taste. If you connect the Arduino to USB, the pet becomes alive!

### Congratulations!

You reached the end of that course! We hope it was fun and that you are now able to build your own electronic ideas. Come back soon, we will have new tutorials in the future!

It was great to meet you! Your StartHardware.org-Team

## 2 thoughts on “Unit 20 – My digital pet 2/2”

1. jenny sagt:

Hi,

I really want to build this project, but dont know what parts I need, could you please send me a list of what sensors etc I require?

Also, can you tell me how I can build with no breadboard?

Thanks

Jenny

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.