Hackers – Distance Sensor

Some of our Hackers have projects of their own that they are working on, to possibly submit to BT Young Scientists or elsewhere. Last Saturday, those people were focused on working on their own project, with occasional help from peers or mentors where needed.

Those who were not working on their own projects extended last week’s Arduino project to add an ultrasonic distance sensor, replacing the variable resistor that they used last week.

Ultrasonic distance sensors are interesting: like sonar in a submarine or how bats navigate, they send out a short sound pulse (ultrasonic – too high for humans to hear) and then see how long it takes for an echo to come back. Since the speed of sound in air is known, we can calculate the distance to the nearest object based on the time for the round trip.

Here is a good tutorial on how it works: https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

Above is a circuit designed by mentor Kevin for an ultrasonic distance sensor and a buzzer, to work like a car parking sensor that beeps faster as you get closer to an obstacle.

Below is Kevin’s Arduino program to control the distance sensor and print out the distance. Some people in the group modified this to use buzzers, others turned on 1, 2 or 3 LEDs depending on distance. #

const int triggerPin = 12;
const int echoPin = 10;

// The speed of sound in air at standard temperature and pressure is 343m/s.
// The range of the sensor is 4m.  It takes 2*4/343 seconds for an ultrasonic
// pulse to travel that far and back.
// We'll use that as a timeout later.  There's no point in waiting any longer
// than the time it takes to read an object at the maximum range of the sensor.
unsigned long echo_timeout = 2*4000000/343;

void setup() {
 Serial.begin(9600);
 pinMode(triggerPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  unsigned long duration;
  float distance;

  // Begin by resetting the distance sensor 
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  // Write out a short pulse for 10 microseconds
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  
  // pulseIn will wait for the input on echoPin to go HIGH.  Then it will
  // time how long it takes to go LOW.
  // The duration in microseconds is returned.
  // We'll wait, at most, echo_timeout microseconds for a pulse. 
  duration = pulseIn(echoPin, HIGH, echo_timeout);
  Serial.print("duration = ");
  Serial.print(duration);
  Serial.print(" microseconds;  ");
  
  // Convert duration to distance. Note decimal point here, needed to get floating point calculation.
  distance = duration * 343.0 / 1000 / 2;
  Serial.print("distance = ");
  Serial.print(distance);
  Serial.println(" mm");
}

Modellers – Week 5

Hi folks, thanks again for another fun session.

This week we continued our sword model. We finished the blade and started work on the guard.

We didn’t introduce any new concepts this week, but we did make our first practical use of the mirror modifier to allow us to create one side of the sword guard and have the other side created automatically.

Here are the video instructions from this week:

 

The matching model file can be downloaded from here.

Hackers – Basic Arduino inputs and outputs

Rheostat_bb

This week in the Hackers group at CoderDojo Athenry, we built on last week’s work on blinky lights, in which we made a simple circuit involving LEDs and resistors connected to an Arduino, and wrote code to get the LEDs to blink.

An LED is an example of an output from our microcontroller. We would also like to have inputs. Examples of circuit inputs are:

  • Switches
  • Dials
  • Sensors that measure something

We focused on dials, specifically a variable resistor or rheostat. This is the kind of knob or dial you find on dimmer switches, volume controls on old radios, electric guitars, and many others.

A variable resistor has 3 connectors: the two outer ones connect across a voltage source (e.g. 5V and ground pins on the Ardiuno) and the voltage at the middle pin can be adjusted from 0 to 5V by turning the knob.

We connected the middle pin of the variable resistor to Analog input 2 of the Arduino. The connections are shown above.

Then, the code to read its value is:

potValue = analogRead(potPin);

where potValue and potPin are ints that were defined already.

The value that you get is in the range 0-1024, and changes as you turn the dial.

Here is a full Arduino program to read a value and display it on the Serial Monitor window if you have a computer connected to your Arduino:

int potPin = 2;    // select the input pin for the potentiometer
int potValue = 0;  // value to read from potentiometer

void setup() {
  Serial.begin(9600);  // need for print commands later
}

void loop() {
  potValue = analogRead(potPin);    // read the value from the sensor
  Serial.println(potValue);
}

In the group, we used this as the basis to improve last week’s program. This time, the speed at which the LED blinks is controlled by turning the potentiometer dial.

The previous code to control how long the LED blinks for was:

delay(one_second);

We changed this to:

delay(potValue);

Of course, we also had to add the code to read the potentiometer value at the top of the loop() function.

Explorers Week 05 – How fast can you type?

Great to see you all on Saturday.

Last week I showed you how to control and move a Sprite, this week we didn’t use any Sprites, everything happened on the Background, but we were still able to create movement.

We created a Timer, this is a piece of code that can be added to any game and is a great one to know.

Every time a the correct key was pressed, the next Background would appear.

For the first time this year we did a Broadcast. This is useful when you want one part of the code to communicate with another part of the code. We used this so that when the last letter was typed, a message was broadcast to stop the timer and display it.

This coming week is the last one before our midterm break. I am going to show you a couple of techniques for showing movement. We will use costumes for the Sprites and it will be Halloween based.

Then everyone is going to work on their own game, based on the Boo Challenge. You do not have to enter the competition if you do not want, but you will use it as your idea for you game.

Here are the notes from last week in PDF CDA-S8-Week 5-How fast can you Type.pdf

Martha

Julie, Iseult, Ruaidhrí and Eoin

Modellers – Week 4

Hi Folks, this week we started to model a sword using the sword found here as inspiration.

This what the sword should look like when we’re finished

We got 85% of the way through modelling the blade this week. Next week we’ll finish the blade and construct the guard, handle and pommel. These should prove considerably easier than the blade which is the most complex part.

I’ve made a video version of the building of the blade, in so far as we got it to this week:

If anyone would like my Blender file with the part-completed blade, it can be found here.

Explorers Week 04 – Dino Finder!

Hello everyone,

Thank you again to you all for coming on Saturday and I hope you all had fun, which is what Coderdojo Athenry is all about!

We had some great new code to learn and to use all our coding concepts we have learned so far.

For the first time we wrote code to control our sprite, rather than it moving randomly as it did last week. For this we took a look at X,Y, axis and used this rather than the steps.

We also used our sensing blocks for the first time as we wanted something to happen when our character went off the path.

Here are the notes from this weeks session in PDF  CDA-S8-Week_04-DinoHunter.PDF

See you next week.

Martha

Julie, Iseult, Eoin and Ruaidhrí

Hackers – blinky lights

Today we built our first circuits on a breadboard.

A breadboard is used to try out electronic circuits and figure out what works.  Once we are happy with a circuit we can make a permanent version on something like strip-board.

Breadboards have blue and red lines across the top and bottom.  A wire strip runs across the board under each line.  If we connect a power source to any point on the lines, all of the points (holes) on the line receive the power.

The middle area of the board has wire strips running vertically.  A wire strip runs down the board under each line of holes.  If we connect a power source to any point on the lines, all of the points (holes) above or below receive power.

The first circuit was one to power a single LED:

circuit01_bb.png
We used the Arduino here as a simple 5V battery.  Current flows from the 5V pin on the Arduino, through the resistor, then the LED, and on to the GND pin on the Arduino.  We should always used a resistor with an LED to protect it.  It doesn’t take much current to damage an LED.

This has the LED on permanently – not very exciting, but good to get started with.  It’s a good idea to start with a minimal circuit and build up to the more complicated circuit we actually want.  That way, we can test and verify as we go, so that we don’t have too much testing and backtracking to do if something goes wrong.

It took a while to get everyone’s circuit working.  Some of the LEDs had legs that were the same length, and there was no obvious way to see which one was the anode or cathode, so we had to guess and test.
Some of the resistors we took at random from the box were too high:  200 kΩ, for example, so the LEDs wouldn’t light up.  We found some 220 Ω (red-red-brown) and 330Ω (orange-orange-brown) resistors to get us going.
Some of the LEDs were very dim.  180 Ω resistors (brown-grey-brown) helped there.
We did a quick calculation to see what value of resistor we really needed:

According to the spec sheet, our LEDs had a forward voltage of 2.2 V and a forward current of 20 mA. In other words, the voltage drop across the LED is 2.2 V, and they can draw up to 0.02 A without burning out.
If our supply is 5 V, then the voltage across the resistor is 5 – 2.2 = 2.8 V.  This is Kirchoff’s Voltage Law.
Applying Ohm’s Law (V = RI), 2.8 = R*0.02, so R = 2.8/0.02 = 140 Ω.
A 140 Ω resistor will make our LED as bright as possible. Less than that will burn out the LED. More than that will produce a dimmer light. So, 180, 220, and 330 Ω are all fine.
Different LEDs have different forward voltages and currents, so it’s important to keep and consult the specification sheets before using them.

The next step in our circuit building was to connect the Arduino’s ground pin to the ground rail on the breadboard.  Then we connected the negative (cathode) pin of the LED to the ground rail.  This is an important step in circuit building:  using a common ground for all our circuits on one board.  It allows us to only have one wire running back to the Arduino ground, instead of one for each circuit.  We would run out of space on the Arduino very quickly otherwise.
Next we did the same on the positive side. This isn’t necessarily as useful as the common ground as we’ll see later.

Once we were happy that we all had a working circuit, we moved on to use the Arduino to control the LED.
We did this by taking the wire that went to the 5V pin on the Arduino and plugging it into digital pin 12 instead.  We went back to our Arduino sketches and defined a new integer variable, ledPin, and gave it a value of 12 to match where we plugged it in.
Then we changed every occurrence of LED_BUILTIN in our sketches from last week to ledPin instead.  The Find (Replace with) function on the Edit menu of the IDE was helpful here.

Once we compiled the sketch and uploaded it to the Arduino, we had the LED on
the breadboard blinking our messages, just like the internal LED was last week.

Next, we added a second LED and resistor to the board, and wired it up the same as the first.  The positive side of the resistor was connected to the positive power rail, and from there on to the Arduino. Now both LEDs blinked together.
To get them to blink separately, we took the wire from our second LED going to the positive rail and plugged it into digital pin 8 on the Arduino.

circuit06_bb.png

Then we created a new variable in our sketch, led2Pin, with a value of 8.
We added setup code for pin 8.  Finally, we added two more lines to the loop, to have the output to LED 2 go low after LED 1 was set high, and vice versa.
The outcome was LED 2 was on when LED 1 was off and LED 2 was off when LED 1 was on.
Some demonstration code is shown below.

// Blink 2 LEDs: one on, one off
// Demonstration for CoderDojo Athenry

int led1Pin = 12;
int led2Pin = 8;

int one_second = 1000;  // delay() function expects milliseconds

void setup() {
  pinMode(led1Pin, OUTPUT);
  pinMode(led1Pin, OUTPUT);
}

void loop() {
  digitalWrite(led1Pin, HIGH);  // on
  digitalWrite(led2Pin, LOW);
  delay(one_second);
  digitalWrite(led1Pin, LOW);  // off
  digitalWrite(led2Pin, HIGH);
  delay(one_second);
}

Modellers – Week 3

This week we quickly introduced a number of techniques and then had an open session where people were free to create their own projects. Our lead mentor Declan got some video showing a few of the projects in progress:

Super work from everyone!

 

CoderDojo Boo

If anyone is interested in entering the CoderDojo Boo Challenge with one of their Halloween themed Blender creations, the link to enter can be found is here:

https://coderdojo.com/2019/10/01/show-us-what-boo-can-do-its-the-coderdojo-boo-challenge/

Best of luck!

 

Mirror Modifiers

The mirror modifier saves us time when working on a symmetrical model (that is, a model that is a mirror image of itself across the X, Y or Z axis).

 

3D Cursor and Adding New Meshes in Edit Mode

The 3D cursor is the point in our scene where new content is added. We can move the 3D cursor easily by selecting one or more parts of the model, pressing SHIFT-S and then choosing “Cursor to Selection”. If more than one thing is selected, the cursor will be in the middle of the selection.

 

Adding a Reference Image

A reference image can be very useful when modelling. One can be inserted from the Add menu (look for Image|Reference). It is a good idea to untick “Align to View” most of the time. You can rotate the image to the orientation you want, or move it and/or scale it once it’s imported.

 

Hackers – From Programming in Scratch to Programming in C

CoderDojo-Hackers-IntroToC

Maybe you can already program in a language like C, you just don’t know that you know it yet?

This week at Hackers, we had some wide-ranging discussion about:

  • Turing Equivalence (the idea that different programming languages are can do the same job, if they have some key features)
  • Alan Turing (computer scientist and code-breaker, after whom Turing equivalence is named)
  • The Turing Test (Alan Turing’s test for whether an AI system can be considered to be intelligent)

Our main focus, however, was on relating a programming language that everyone is familiar with, Scratch, with the language that is used to program Arduino, which is based on C/C++. C and C++ are professional programming languages, text based, that don’t look much like Scratch.

However, it turns out that they share important key features that make them Turing equivalent, and these are the basis for basically all major programming languages:

  • Variables and operators
  • Loops
  • Decisions

Therefore, if you can come up with an idea of out how to write a program in Scratch, you can probably translate that idea into a language like C.

Here are the notes in PDF: CoderDojo-Hackers-IntroToC

We also spent a bit of time lighting an LED by connecting it to a battery in series with a resistor. Next week, we will use this as the starting point for making an Arduino-controlled electrical circuit.

led+resistor+battery

 

 

Explorers Week 03 – Our first Game – Balloon Ninja!

Hi everyone,

Great to see you all yesterday and I hope you enjoyed creating your first game in scratch.

its important to take a moment to make a plan, in the long term it will save you time if you have a clear idea of what you want to create.

I didn’t make it easy on you and we went straight into learning about variables to create our score and lives.

I saw some fantastic games around the room with Dinosaurs and Doughnuts and Unicorns, much better ideas than I could ever come up with.

I gave you the option of whether you wanted to add a time or not. It is a piece of could yo can add to any of your games as it can add a sense of tension or urgency to a game.

Here are the notes from this week in PDF CDA-S8 Week_03_BallonNinja.PDF

Thanks again for coming and see you next week.

Martha

Julie, Iseult, Eoin and Ruaidhrí