# Bodgers – FreeCAD and Fritzing

Hello again everyone

This week we continued working on our Astro Pi entries and we also looked at FreeCAD and Fritzing which are tools that will help us with building our projects.

FreeCAD, available for download from here, is used for 3D modelling and allows us design very complicated things from simple 3D shapes such as cubes and cylinders. Here are a couple of quick videos to get you started.

Then we looked at Fritzing, download from here, an application for drawing very easy to understand circuits, here’s how to draw a simple circuit using it.

Dave will be leading next Saturday’s session and I will see you again on the ninth of Feb.

Declan, Dave and Alaidh.

# Creators: Shootah Part 2 – Sound and Bullets

This week we carried on our “Shootah” space shooting game  by adding some bullets with an appropriate sound effect.  This brings the game to the stage shown below:

Not very snazzy but it’s starting to show some promise!

As with last week, this will only run if served up from a web server, we had a few ninjas in that missed last week and had to install node.js and local-web-server application.

Bullets!

Our Bullets were a pretty simple object that just moves up constantly – this has the following code:

```class Bullet {
constructor(x, y){
this.x = x;
this.y = y;
this.speed = 10;
}

move(){
this.y = this.y - this.speed;
}

show(){
push();
fill("yellow");
ellipse(this.x, this.y, 10, 10);
pop();
}
}

```

We created an array of bullets and added a new one to the array each time the up arrow is pressed.  In addition, we played a “firing” sound which really adds to the feeling:

```function preload(){
}

function draw() {
// .... skipping a few lines
for(let i=0; i< bullets.length; i++){
bullets[i].move();
bullets[i].show();
}
// .... skipping a few lines
}

function keyPressed(){
if(keyCode==UP_ARROW){
bullets.push(new Bullet(player.x, player.y));
bulletSound.play();
}
}```

The sound was done very similarly to the image – we created a folder (sound) and added a sound file we found on the internet to the folder.  In the preload function we loaded the sound from the file into a variable (“bulletSound”).  In the keyPressed function, we check if the pressed key is UP ARROW and if so, we play the sound and add a new bullet to the list.. voila!

Problems to work on!

A great start but a lot left to be sorted with this game!  Here are some of the items:

• The bullets array grows forever – this really starts to slow down the game as we keep playing it.  We need to add some code to remove a bullet from the array when it is gone off the screen.
• The player has no edge detection – we need some way to keep it onscreen.
• We need some enemies!!
• We need to get the bullets to check when they are hitting the enemies!
• We need to keep and display a score
• We need a way to die.. perhaps the enemies can drop bombs?
• Other ideas:
• Maybe a background tune in a loop?
• Maybe a scrolling starfield in the background?
• Maybe multiple levels?
• Maybe different types of enemies?
• Maybe a shield?
• Perhaps some different weapons or weapons you can pick up?

We have gotten this far so we might as well push forward and make this a fully working game so we’ll get some of the above sorted next week!  The code is as usual up on our github.  https://github.com/coderdojoathenry/creators2018

# Hackers – Controlling Motors with an Arduino and H-Bridge

Previously in Hackers we have studied how transistors work, and made a transistor-based circuit to control a motor from an Arduino: Hackers – a Joule Thief and Controlling Motors.

When you write to a pin on the Arduino, it outputs a voltage. However, you can’t use this directly to drive an electric motor, because they require too much current, and it would damage the Arduino. The solution is to use a 6V battery as an external power supply, and connect it to the motor via a transistor circuit. When you apply a signal with small current to the middle leg of the transistor, a much larger current can flow from the battery to the motor.

While this works, a more elaborate circuit is needed if you want to be able to control two motors, and make them go backwards and forwards. This circuit is called a Dual H-Bridge. The Wikipedia page has technical details: https://en.wikipedia.org/wiki/H_bridge

We are using a pre-built integrated circuit for our H-Bridge, as they are low-cost, small, and work well. Here is the one we are using:

It has several connectors:

• [+] and [-] are where the external battery is connected
• [IN1] and [IN2] control Motor A (details below)
• [IN3] and [IN4] control Motor B
• [Motor A] and [Motor B] each have two pins that are connected directly to motors

To control Motor A, connect [IN1] and [IN2] to two pins of the Arduino, such as 6 and 7:

• [IN1] HIGH and [IN2] LOW: Motor A goes forward full speed
• [IN1] LOW and [IN2] HIGH: Motor A goes backward full speed
• Both LOW: Motor A does not turn (no power, it coasts)
• Both HIGH: Motor A does not turn (is braked)
• To control speed, use a value for the pins connected to [IN1] or [IN2] in the range 0-255 (0=LOW, 255=HIGH)

Here is Arduino code to control a motor with a H-Bridge, written by Luke, one of our Hackers:

```// Luke Madden, CoderDojo Athenry
// Control motors using a H Bridge

// The H bridge has 4 input pins: in1-in4
#define in1 6
#define in2 7

int fast = 100;// range 1-255
int slow = 50;// slower speed
int hyperspeed = 255;// hits the hyperdrive

void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
Serial.begin(9600);
}

void loop() {
drive();
}

void drive() {
// Test the functions
Serial.println("move forward");
forward();
delay(2000);

Serial.println("hit the hyperdrive");
hyperdrive();
delay(2000);

Serial.println("go backwards");
backwards();
delay(2000);
}

void forward() {
//makes motor go forwards
analogWrite(in1, fast);
analogWrite(in2, 0);
}

void hyperdrive() {
//hits the hyperdrive
analogWrite(in1, hyperspeed);
analogWrite(in2, 0);
}

void backwards() {
//makes motor go backwards
analogWrite(in1, 0);
analogWrite(in2, slow);
}

void stopping(){
//makes it stop
analogWrite(in1, 0);
analogWrite(in2, 0);
}
```

# Hackers – Hide and Seek

We welcomed back Hackers Group members Luke and Sean, who had projects at the Young Scientists Exhibition last week.  Details of their projects are in another post: Congratulations to our members who presented at the BT Young Scientists 2019!

This week we divided the group into two teams to work on our hide-and-seek robots:  One team to build the hider robot, the other to build the seeker robot.

The two robots can use many of the same parts, but the control systems will be different.  The hider robot will be operated by a person with an RC transmitter.  The seeker robot will control itself.

Team Hider started resurrecting a radio-controlled robot that we built last year.  The robot was involved in a battle-bots battle at the end of the year, so it is in pretty poor shape.
Teem Seeker worked on a colour tracking system.  We took the code that we developed on a laptop before Christmas and tried it on two Raspberry Pis.  We found that the original model Raspberry Pi wasn’t fast enough to do the job we needed.  It took 6 seconds to acquire an image from a webcam.  We tried a Model 3 B instead.  This was fast enough for our needs.  We then started work on tuning the colour detection.  There’s some more experimentation to be done to find the best colour range to work with.

# Creators: Images and Web Servers

Up to this point in Creators we have been building all of our apps as sketches ourselves using the drawing functions in p5/javascript – ellipse, rect, etc.  This is great but a question we get all the time is “how can I load IMAGES and Sound into my games?”.  This week we got stuck into this, using a project of a very basic shooting game as an example.

However, before we got stuck into writing our game, we needed to install something on our computers called a web server.

## What is a web server and why do we need it?

A web server is a program that makes files available on the internet.  Mostly, the files are made available to programs that can understand the Hyper Text Transfer Protocol – or http for short.  “http” probably sounds familiar – this is because you are used to seeing it on your browser before internet links.

Up until this point, we simply opened files on our computer with explorer and if you looked at the browser link you would see something like this:

`file:///C:/Users/markdav/code/dojo/creators2018/Week11-shootah/index.html`

The “file” at the start means the browser is opening a file on the local computer.  This works great for a lot of things, however if you write a program in javascript which runs on the computer, javascript is not allowed to open files on the local computer.  Why? Because if javascript could open files on the computer of the person opening a website, it would be easy to write a webpage that steals information from people looking at it, without them knowing it.  This would be a BIG security problem!!

Javascript can only open files which are served out by a web server – so in order to load images or sound into a javascript program, we need to make sure that they are served out by a web server.

## Installing our Web Server – Node.js

There are lots and lots of web server programs out there but we decided to install one which is written in javascript – we are javascript nuts after all!  Node.js isn’t actually a web server, but is a set of programs kind of like p5 that make it easy to write or install programs in javascript for your computer – and especially ones like web servers.  Later in the year we will play some more with node.js and write our own programs in it to do some cool stuff.

We installed node.js from the node website here.

Once we installed node.js, we then installed a web server which is written in node.js – we installed one called “local web server”.  To install local web server – we opened a command terminal in the computer and typed the following:

`npm install -g local-web-server`

npm stands for “Node Package Manager” and it’s a program that makes it easy to install programs written in node on your computer.  Once this was done, we have a new command in our computer – the command is “ws” and what it does is to start a web server that puts the files from the current folder on the internet!

To try it out, we started a new p5 project called “shootah” and right clicked on the folder and selected “open in terminal”.  The command prompt appears directly in Visual Studio Code, which is handy.  We then typed “ws”

```markdav@markdav2017 MINGW64 ~/code/dojo/creators2018/Week11-shootah (master)
\$ ws
Serving at http://markdav2017:8000, http://192.168.71.1:8000, http://192.168.48.1:8000, http://192.168.1.20:8000, http://127.0.0.1:8000```

This started a web server in the folder which serves out the files – to open we used either one of the links given, or actually we kind of preferred to type “http://localhost:8000&#8221; which was another way of putting it.  When we did this, the files just showed in the browser!

## Developing Shootah

All this installing took us some time, but we were ready to get stuck into developing our space shooting game.  We go to the stage of getting a Spaceship which we could move left and right – but no bullets, sound or enemies, yet.

First we needed a spaceship picture, and we had a bit of fun rooting around on the internet for one.  Some people created their own images.  I ended up picking this picture (you can right-click this picture and save it if you like it too):

When we had the spaceship picture, we created a folder to store our pictures.  I created a folder called “images” and put the picture in there and called it big-spaceship.png.  Some others called their folder “assets” which is a term you often see used. To be honest my picture was a bit big – you are better off making the images just the right size, as it saves a little bit on memory for the computer when showing them.

We learned that there is another special function in p5 which is best to use when we are loading files like sounds and images into variables – it’s called “preload” and we wrote the code to load our images in there:

```let playerImage;

}```

Once we had the image loaded, we could draw it using the “image” function.   The show function on our player used this to paint the image:

```show(){
imageMode(CENTER);
image(this.img, this.x, height-100, 100, 100);
}```

Aside from that, the code was pretty similar to other sketches we have done.  We put some basic code in to allow the player to be moved left and right.  This is as far as we’ve gotten – next week we’ll work on it some more and see if we can get some enemies, bullets, sounds, maybe stars, scores, better movement, etc.  Code is on github as usual!

# Bodgers – Mission Zero & Projects

Hello again everyone.

This week we started getting our entries ready for the Astro Pi Mission Zero Challenge by looking at whose aboard the International Space Station now and what they’re doing. We followed this up by taking another look at programming the Trinket Astro Pi emulator.

We also had a small brainstorming session to come up with ideas for projects that we will be working on for the rest of the year.

See you all next Saturday.

Declan, Dave and Alaidh.

# Week 2 2019 – Explorers – Piano

Hello everyone,

Thank you all for coming again this week. Today we started looking at the Scratch 3. We had some small issues saving but hopefully all these little issues will get sorted with updates over the next couple of weeks.

This week we did a simple Piano so we could familiarise ourselves with it.

We only had to draw two keys, and then could duplicate these and change the names. The same applied to the code. The code is the same for each key apart from one small change so the note is the appropriate for the key.

REMEMBER! You need to make four changes each time you duplicate:

Change the name of the spite to the next Note

Change the name of the TWO costumes

Change the NOTE played

Here are notes from this week in PDF cda-s8-week-2-19-piano.pdf

Martha

Julie, Ruaidhrí and Eoin

# Introduction

This week we built a Mad House with different rooms that you could go into and if you found the right place to click something would happen.

The coding for this project shows how you can re-use code, this has 2 benefits, it makes coding quicker and it also makes all the code consistent, so it all looks the same.

## The Plan

As always, it’s best to have a little bit of a plan.

We decided on the following:

• 4 Rooms – each room would have 2 Sprites
• Clicking a room would make it large
• Clicking the Space key would make the room small again
• We would build one room first so we could copy the code.

## The Code

The first room we created was the Kitchen, the plan was when you clicked on the saucepan, it would boil over.

The Sprites need to fill the whole screen and also need a coloured background to ensure that the Mouse Click is recognised by the code.

These are the 2 Sprites that I built:

It’s not easy to see, but the “On” Sprite has some flames coming out of the pot.

Because we are having 4 Sprites, when the program starts we need to make the Sprite a quarter of it’s full size and move it to one of the corners of the Screen. We also discovered that we would need a variable (a flag) to indicate if the room was Large (1) or Small (0). We did this in the GreenFlag code like this:

We moved to top left corner.
we set the size to 50% (this makes it fill a quarter of the screen
We set the variable to 0 (0 = small, 1 = large)
We set the start costume to the Off costume.

Next step was to write the code for when the Sprite was clicked, this would make the Sprite fill the screen, slowly! It would also set the flag to say that the Sprite was large.

The biggest piece of the code was making the Sprite grow slowly, we did it in 50 steps, changing the size was easy as it just changed by 1 each step, but the X and Y positions were a little trickier as we needed to make sure they were going in the right direction and also changing by the right amount to reach the full size in 50 steps.

Only run this code if we are small (0)
Make we can’t run again set the flag to 1.
Make sure we are in front of the other Sprites

X needed to go from -120 to 0, 2.4 each time
Y needed to go from 90 to 0, so -1.8 each time
Size was simple, 1 each time
And to make it nice and smooth, we wait for .1 of a second in each step.

We then moved on to the code that should run when the Space key is pressed, this would reverse what happened when the Sprite was clicked.

Again, we only run if we are large (1)

The X and Y changes are the reverse of the ones above.

And finally we set the size back to small again (0)

Now we can move on to the code that changes the costume if the user clicked on a certain spot on the screen.

We decided that the user should click on the pan for the costume to change, so first we needed to work out where the pan is on the screen (It’s X and Y positions). This is easy in Scratch 2, when you move the mouse across the Stage the X and Y position is shown at the bottom of the screen, so you just need to place the Mouse on the 4 sides of the Pan and make a note of the X and Y values. X values for the min and max left/right positions, Y values for the min and max up/down positions. Hopefully this picture will help:

\$

We needed to place the Mouse on each of the positions indicated, which then gave use the correct values to place in the code.

The code then looked like this:

We loop forever
Only check X and Y if we are large (1) and the Mouse is clicked. Then check where the Mouse is. This where we use the values we got when checking where the Pan was on the screen.

And that is it for the first Room.

## The Second Room.

Now that all the code is written for the first Room, the second Room becomes a lot easier, we simply duplicate the first Room Sprite and then make the following changes to the code:

• Change the 2 Costumes to something else for this new Room.
• Add a new Variable for this Room
• Update all the places where the Variable is set to this new one.
• Update the starting X and Y positions to one of the other corners of the Stage.
• Update the X and Y changes when the Room grows and shrinks to make sure it is going to the centre of the screen correctly and also back to it’s own corner.
• Update the X and Y values to a new location on the Screen, depending on what you have drawn on your new Costumes.

## The Finished Program

I only got 3 Rooms finished and this what it looked like at the end:

## Next Steps

If you want to improve the program you can look at adding the following:

• The final Room to make it 4 rooms.
• Extra code change the Costume back to normal if the item is clicked again, this would check what Costume is currently, this can be done using the “costume #” from the Looks blocks.

# Notes

The one I did in the class is available on the Scratch Web Site: