Creators – Pattern

washer-machine-porous-mechanical-83852

This week we looked at two important concepts, loops and lists.

Loops

Start by imagining we wanted to draw four circles next to each other. We could write four calls the ellipse function, like this:

let size = 50;

ellipse(25, 25, size, size);
ellipse(75, 25, size, size);
ellipse(125, 25, size, size);
ellipse(175, 25, size, size);

Pretty easy. What if we had variables for the positions? We’d get this:

let size = 50;
let x = size / 2;
ley y = size / 2;

ellipse(x, y, size, size);
x = x + size;
ellipse(x, y, size, size);
x = x + size;
ellipse(x, y, size, size);
x = x + size;
ellipse(x, y, size, size);
x = x + size;

It’s longer than before, but notice how the same two lines now keep repeating. If we had a way to say “do these two lines four times” then this would get much shorter, and we do. We use the for statement:

let size = 50;
let x = size / 2;
ley y = size / 2;

for (let i = 0; i < 4; i++){
  ellipse(x, y, size, size);
  x = x + size;
}

The for statement is a bit complicated, so let’s break it down:

for (do first; check to see if we keep going; do every time 2) {
  do every time 1
}

Note the curly brackets (braces) containing the stuff we want repeated.

So in our case we:

  1. First create a new variable called i and give it the value 0
  2. Check to make sure that i is less than 4
  3. Draw our ellipse and make x bigger
  4. Increase i by 1
  5. Go back to step 2 and check if we can keep going, otherwise stop

This means that i will have the values 0, 1, 2 and 3 and our two lines will be run four times in total. Result! Our code can draw a row of circles. If we increase the value in the check, we can have as many as we like. We choose to have 8.

Nested Loops

Nesting a loop means putting one loop inside another. What’s the point of that? Well in our case we have a loop that can draw a single row of circles. If we put all of that inside another loop we could draw several rows. The row outside has a different variable j and also runs eight times. After we draw our row we do two things:

  1. We make y bigger move down the screen
  2. We move x back to the left to its starting position for the next row

The code now looks like this:

let size = 50;
let x = size / 2;
let y = size / 2;

for (let j = 0; j < 8; j++){
  for (let i = 0; i < 8; i++){
    ellipse(x, y, size, size);
    x = x + size;
  }
  y = y + size; // Move down and
  x = size / 2; // Back to the left
}

We now have 8 x 8 = 64 circles in a grid.

Changing the Colour of Some Circles Based on a Check

We then added code just before our call to ellipse() to change the colour based on some check:

if( /* some check here */ ){
  fill("red");
}
else {
  fill("white");
}

We experimented with some different checks to see what might happen. This check turns the upper-left of the grid red:

if (i + j < 8 ){ 
  :  :  :

This check, using the modulus operator, turns every third circle of the grid red:

if ((i + j) % 3 == 0){ 
  :  :  :

This check paints a red cross through the centre of the grid:

if (i == 3 || i == 4 || j == 3 || j == 4){ 
  :  :  :

Lists

We then jumped quickly into lists. Also called arrays, lists are like a variable that can store several values. We create them simply like this:

let a = []; // An empty list
let b = [1, 2, 4, 7]; // A list created with four entries

To get at the entries in a list you just use the list variable’s name and square brackets containing the number of the entry in the list you want to get out, noting that the first one is zero:

b[0] = 10; // Set the first entry in the list to ten
b[4] = 301; // Set the fifth entry in the list to three hundred and one

A list isn’t just for holding numbers though, it can hold anything. It can hold strings for example, or even other lists!

It’s this idea of holding lists that we use to define our pattern.

Defining our Pattern

We make a list with eight entries, each of which is a list also containing eight entries, all zero. We write it so it forms a neat block like this:

let pattern = [
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0]
];

Then down in our code, where we’re deciding what colour to make our circles, we change the check to read:

if (pattern[j][i] == 1){  
  : : :

What does this mean? Well, j is a counter that goes from 0 -> 7 as we go down our eight rows. Given that, pattern[j] means get entry from our list for that row. Since pattern[j] is a list too, we need to say which entry we want in. The variable i goes from 0 -> 7 as we go across each row. So, pattern[j][i] gets the list for the row and then picks out the number for that column.

Once it’s set up, we can then change zeros to one in our pattern and have our circles turn red to match (red in the text below to make them stand out):

let pattern = [
  [0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 1, 0, 0, 1, 0, 0],
  [0, 0, 1, 0, 1, 0, 0, 0],
  [0, 0, 1, 1, 0, 0, 0, 0],
  [0, 0, 1, 1, 0, 0, 0, 0],
  [0, 0, 1, 0, 1, 0, 0, 0],
  [0, 0, 1, 0, 0, 1, 0, 0],
  [0, 0, 0, 0, 0, 0, 0, 0]
];

Screenshot 2018-10-15 at 18.14.34

We have essentially built something that works like an image file! Our pattern is the image data and our program is the code that draws it.

As several smart people already noticed, you’re not limited to one and zero. By using more numbers, and expanding the if statement, you can have as many colours as you like.

Download

The files for this week are on our GitHub, as always.

Explorers Week 5 – Help Bo Peep find her sheep

Hi everyone,

Thanks for coming Saturday even though the weather was dreadful.

This week, I helped Bo Peep found her sheep! Some of you did the same and some used ideas like Minecraft Steve finding Iron Ore and Diamonds, Knights finding dragons or a Princess finding flowers.

Before we even started our game this week we talked a little bit about File Management and about the importance of keeping your files somewhere you can access them quickly and giving them a meaningful name.

So to this end, we all created a folder where we will be keeping our files in the future and within that we had a sub folder for this weeks files.

We started our game by drawing our background on our stage:

Unfortunately, due to internet problems we could not search the internet for images for our Sprites, but we could still use the sprites from the Scratch Library.

This week, we decided to move our main sprite using the arrow keys. For this we had to learn a small bit about the X and Y axis and I gave you a little tip on how to remember which is which!

Hope you all enjoyed this week, aee you next week when we will be starting a new game!

Here are the notes for this weeks session in PDF CDA-S7-Week_05-BoBeep.pdf

Martha

Julie, Ruaidhrí and Eoin

 

Hackers – Temperature Control, Part 2

Today we continued towards building a temperature controller.

Last week, we used an LM35 temperature sensor to read the temperature in the room and report it on the Arduino serial console.
We were able to get a reading for the temperature of the air in the room, and the temperature of a cup of coffee touching the sensor.  We couldn’t be sure, however, that the readings were correct.  So, we decided to test with a potentiometer and a voltmeter.

We supplied 5 V to the potentiometer and fed the output to the Arduino.  We used a voltmeter to measure the output from the pot and compared it to the reading from
the Arduino.  Once we got the code working, we got good agreement on voltage readings between the meter and the Arduino.

The sensor presents a voltage on its output pin that represents the temperature its measuring.  In its basic mode, the sensor reads temperatures between 0.2 and 150 degrees Celsius.  Every degree is 10 mV, so the output voltage ranges from 2 mV to 1500 mV.  Converting our voltage reading to a temperature is simple then:  divide the voltage reading in millivolts by 10.

We can use one of the analog I/O pins on the Arduino to read the voltage.  The analogRead() function returns a value between 0 and 1023 for voltages between 0 and 5 V.

We digressed into how to use a multimeter correctly.  The first thing to do is make sure that our meter is set up correctly.
We tried measuring a 9 V DC battery with the DC voltage setting and got 9.2 V.  We then tried measuring the same battery with the AC voltage setting.  This time we got 19.6 V.
There’s a lot of potential (sorry) to get confused, then.  Worse again, if we try measuring 220 VAC with the meter set to DC, we get a reading close to zero.  In other words, a live AC supply looks safe if we set our meter wrong.

Next, we start with the highest reading range and step down.  For a 1.5 V battery, we start with the 200 V reading, then step down to 20 V.  This is to protect the meter:  if the voltage is higher than we expected, we might damage the meter.

Voltage readings are taken in parallel with the circuit, and while it is live.

Resistance and continuity readings, on the other hand, are taken with the component disconnected from the circuit.
We worked out why with a simple circuit:

circuit1

F is a fuse.  W is a wire between the two ends.  We’re not sure if the fuse is blown.  We try testing for continuity across the fuse by putting our meter leads on A and B.
Even if the fuse is blown, we will get continuity because W provides a circuit.  We need to cut the wire to get a true reading.

Our first attempt at Arduino code for the voltage reading gave us a surprise.  Our meter displayed 2.5 V.  The Arduino displayed 2 V.  We used this formula to calculate the voltage:
v = 5 * analogRead_reading / 1023.
V was declared as float variable.  analogRead_reading was declared as an int.  The Arduino code multiplied two ints and divided the result by another int, truncated the result and stored the int as a float.  When we made the 5 and 1023 floating point numbers (5.0 and 1023.0), we got the right answer.

Once we were happy with the Arduino code, we replaced the potentiometer with an LM35.  Unfortunately, we didn’t notice the “bottom view” label on the datasheet drawing.  We connected the 5 V supply to the wrong side of the sensor.  It’s amazing how hot a temperature sensor can get!  It was too hot to touch.  And we couldn’t measure the temperature because…
After we disconnected the LM35, we made another discovery:  we continued to get random voltage readings displayed on the Arduino even though there was no voltage to read.  The analogRead() function happily outputs values from random noise.

Next week, we’ll use an LM35 connected the right way around, and try controlling a relay to switch power on and off to an external device.  We’ll build a temperature controlled soldering station carefully – we want to solder with the tip of the iron, not the temperature sensor.

Bodgers – Making With Trinket.io

Hello again everyone.

In the Bodgers group,  we’ve been working on code for the International Space Station. To do this we are using on online Sense Hat emulator, the Sense Hat is a special piece of hardware designed to be deployed with a Raspberry Pi on the ISS.

Capture15

The online emulator is available at https://trinket.io/sense-hat and there’s a good tutorial on the Raspberr Pi website here https://projects.raspberrypi.org/en/projects/astro-pi-mission-zero.

Here is our Trinket https://trinket.io/python/04b90b70cf.

You can play around with these and we will probably finish up with the Mission Zero Challenge for while after Saturday. My slides from last Saturday are here day 3.

See you all on Saturday.

Declan, Dave and Alaidh

Creators: Creative Coding with p5.js

What is P5?

This week we looked at p5.js – a library to make creative coding in javascript MUCH easier!  We first downloaded and copied p5.js onto our computers, and copied it into a new project folder.  We included p5.js in a html file, just like we included our own javascript file last week.

In fact, we looked at the file and realised that p5 is just another javascript file, just like the one we wrote – the code is long and looked a bit complicated, but it was basically the same kind of thing.

 

But really, what is P5?

p5 is a set of functions that make it easy to write programs for the most interesting parts of javascript!  Mainly, for drawing and animating things on a webpage, but also for sound, and other stuff.  If has lots of functions for:

Continue reading

Explorers – Week 4 – 2018, Improving Our Game!

Hi everyone,

Thank you do everyone you came along on such a lovely day on Saturday, hope you all got to go outside when you went home and enjoyed the weather.

This week, we improved on our game from last week.

Most games reward us for achievements (Gaining:Scores/Money/Health) and penalise us for making mistakes (Losing:Lives, Health, Money)

To this we had to create a Variable. Remember my box with the blocks. A variable holds information for you that you can use again. Remember to always give it a name that is meaningful….

Everything we have learned so far we will be using in ,most of our games we will make in future sessions. Don’t worry if you didn’t understand everything, we will be going over it again and again.

Have fun with the code you have done. Maybe try making another game!

Here are the notes from last week in PDF CDA-S7-Week_04-FirstGame-Part2.pdf

See you all next week

Martha & Julie, Ruaidhrí and Eoin

Hackers – Temperature Control, Part 1

IMG_20180929_131406

In Hackers, we started work on a short project for a temperature-controlled soldering station. As shown on the whiteboard above, the basic idea is:

  • A temperature sensor is connected to an Arduino (analog input)
  • A soldering iron, with its tip located near the sensor, is wired to the mains via a relay switch
  • The Arduino can control the relay switch to turn the soldering iron on/off
  • This is the basis for a simple bang-bang controller: we have a target temperature (e.g. 200 degrees) and a tolerance (e.g. ±10 degrees), then you turn it on if the temperature is below 190 degrees and turn it off if above 210 degrees.

We noted that since there will be space between the soldering iron tip and the temperature sensor, the temperature it will read will be lower than the tip temperature.

As the whiteboard shows, this project also involved discussion of: wiring for the thermocouple; how breadboards are wired; normally-open vs normally-closed relay switches.

One group used a LM35 temperature sensor – here are its specs, and the group found it interesting to see how detailed and useful these data sheets are: http://www.ti.com/product/LM35

The other group used a potentiometer to simulate temperature, so they could test it working over its full range, as shown here:

IMG_20180929_135713

Below is the code for reading and displaying the temperature. Next steps will be to integrate it with the relay.


// Michael Madden, CoderDojo Athenry
// Reading a temperature sensor.

// The temperature sensor is an LM35 - here are its specs: http://www.ti.com/product/LM35 
// THe temperature range is -55 to 150 degrees celcius.
// Its output voltage pin is connected to an analog input on the Arduino (A1),
// from which we read a value in the range 0-1023, so we convert them.

void setup() {
  // Just needed for print statements to work
  Serial.begin(9600);
}

void loop() {
  // Read the temperature sensor: get a value in range 0-1023
  int val = analogRead(A1);

  // From the analog input, we get a value in the range 0-1023
  // and we need to convert it to a temperature in the range -55 to 150 degrees.
  // The two lines below achieve this in differnet ways and give the same result.
  int temp1 = (val/1023.0)*205 - 55;
  int temp2 = map(val, 0, 1023, -55, 150);

  // Print out the raw value and converted temperature
  Serial.print("Sensor value = ");
  Serial.print(val);
  Serial.print("\tTemperature 1 = "); 
  Serial.print(temp1);
  Serial.print("\tTemperature 2 = ");
  Serial.println(temp2);

  // Wait 100 milliseconds before reading the sensor again
  delay(1000);
}

 

Bodgers – Making For The International Space Station

Hello again everybody.
This week in the Bodgers group we started working on our code for the Mission Zero Challenge.

mission-zero-logo

We began by writing a simple text message on the 8×8 full-colour LED display, then we changed the text and background colours. We then coded a picture by assigning a colour to each of the 64 LEDs on the display. We finished the session by taking a quick look at using the temperature sensor to read the temperature. Here are my slides from this week day 2.
Next week we will recap what we covered this week and we will start to personalise our code for the challenge.

In the meantime, here’s a couple of fun videos on how the Astro Pi computers got to the ISS.

See you all next Saturday

Declan, Dave and Alaidh

Explorers – Week 3 – 2018, Your First Game!

Hi everyone!

Wow! what a fantastic turnout on Saturday again, 79 ninjas in the room! plus a few adults!

I hope you had a good time, I know that I really enjoyed it. There was a really great atmosphere and energy in the room.

A special thank you to Mentor Julia for getting 14 loaner laptops working for us this week, so that everyone that wanted to participate, could. Another thank you to the other Mentors in the room, Eoin and Ruaidhrí, it makes my job easy when they can get around the room and help out when needed. You are doing a great job.

One of the first things we must do before we start anything is make a plan.


We quickly learn when we begin our coding that it is very important to test our code as we go along. Making mistakes is good, but we must learn from them by spotting them (testing) and fixing them.

I am attaching my notes in PDF for you. CDA-S7 Week_03-FeedingtheFish.pdf

We will continue to add to the game next week but don’t worry if you weren’t here this week, I will have a copy of the game so that you can join in.

See you all nest week!

Martha and the Explorers Mentors, Julia, Eoin and Ruaidhrí