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

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);
}

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

 

 

Modellers – Week 2

Hi folks, thanks for another great session on Saturday.

This week we looked at going beyond the object level and actually editing object meshes. With an object selected you can switch into edit mode by using the drop-down list on the upper left-hand side of the 3D viewport or, more commonly, by pressing the TAB key.

Meshes are made up of three parts:

  1. Vertices: I tend to also call them nodes, as I did in the diagram below. The represent a point in space
  2. Edges: A line between two vertices
  3. Faces: A flat solid area bounded by three or more edges (normally four in Blender)

Screenshot 2019-10-01 at 09.33.35.png

Faces are also known as polygons. Ones with three sides are called tris (short for triangles), ones with four sides are called quads and ones with more sides are called N-gons. The ends of Blender’s standard cylinder object are examples of N-gons. N-gons can cause strange shading effects when using smooth shading and we try to generally avoid or minimise them.

Editing at the mesh level

Once editing the mesh, all the usual tools that we had available at the object level (namely Move (or Grab), Rotate and Scale) can be used, but now on vertices, edges and faces.

You can work with vertices, edges or faces, as suits what you’re trying to do. To indicate which you want to use, use the buttons towards the top-left of the 3D viewport or the handy shortcut buttons 1, 2 and 3.

Adding geometry

The standard cube is too simple to start shaping We saw three ways to add geometry:

  • Adding edge loops: An edge loop is a cut that runs cleanly all the way around the object. They are easily added using CTRL-R and then using the scroll wheel to adjust the number of cuts and moving the mouse to indicate which edge you want them to go through.

  • Extruding. Select a face and hit the E key, you can then drag it and it will move while newly added faces connect it to its original location. The effect is like forcing Play-Doh through a hole.

  • Inseting: Select a face and hit the I key. Move the mouse and you will see a smaller face, the same proportion as the original, is created inside the original face and four more faces connect it to the original edges.

Exercise

For the second-half of the session, we took a cylinder shape and started to sculpt it into a candlestick shape. A few lessons from that were:

  1. We started with the simple cylinder and added loads of loop-cuts to allow us to shape the candlestick shape.
  2. We saw how proportional editing can be switched on to allow vertices connected to those we’re directing editing to be moved as well.
  3. We saw how the bottom and top of the cylinder are N-gons and look strangely domed when smooth shading is on. We resolved that by insetting the face to minimise the N-gon’s size and extruding it out of the way a bit.
  4. We saw how adding loop-cuts near an existing edge can restore a sharp edge to our model when smooth shading makes it seem too soft and round.
  5. We saw how the “Metallic” and “Smoothness” sliders can be used on a material to make it look like shiny metal.

Screenshot 2019-09-28 at 13.45.03.png

Next Week

Next week is intended as a free-form session, bring your best ideas for simple projects and we’ll try to help you achieve them. See you then!

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í

Hackers – Starting Programming Arduino

arduino

In our first week in the Hackers group, we began with an intro to what we do, which is to help people work on their own projects. In the first few weeks, we will focus on learning some useful technologies that people can then start applying to their own work.

Everyone introduced themselves and talked about what projects, if any, they planned to work on, and the mentors suggested some possible technologies that may be useful.

We decided to start by learning how to program the Arduino, which is a microcontroller that runs one program at a time – as soon as it is powered up, it runs the code in a function called setup(), and then it keeps running code in a function called loop().

Arduino is programmed in a version of the C Programming Language, which is a very well-known language.

To write a new program for Arduino, you connect it to a computer and use the Arduino IDE (interactive development environment), which we downloaded here: https://www.arduino.cc/en/Main/Software

Arduino is mainly used to control hardware, and we will see how to do that in future weeks. For this week, we did not control external hardware, but just a built-in LED.

We began by following this tutorial to write code to make an LED blink: https://www.arduino.cc/en/tutorial/blink

Then we started expanding it …

  • We decided to flash an SOS message in Morse Code
  • We found out that a dash is 3 times as long as a dot, and the interval is the same length as a dot
  • We found out that you need a short delay at the end of each letter (3 dots long) and a longer one at the end of each word (7 dots long)
  • We made functions for dot(), dash(), and letters such as S and O
  • We looked up Morse Code for the other letters, and wrote functions for them
  • We added a variable so we could control the speed of the flashes

Here is one version of the final Arduino program. Note that it is incomplete, it just has a few of the letters of the alphabet.

// Code by Michael from CoderDojo Athenry.
// A program to display messages in Morse Code by flashing an LED.
// This is not complete - just some examples.

int interval = 300; // this controls the speed of messages in milliseconds

void setup() {
  // Set up the LED as an output pin. The built-in LED is represented by LED_BUILTIN.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // Keep repeating our message over and over.
  A(); // Morse code for a letter
  B();
  S();
  endword(); // At the end of each word there is an extra delay
}

void dot() {
  digitalWrite(LED_BUILTIN, HIGH); // send 5 volts
  delay(interval);
  digitalWrite(LED_BUILTIN, LOW); // send 0 volts  
  delay(interval);
}

void dash() {
  digitalWrite(LED_BUILTIN, HIGH); // send 5 volts
  delay(3 * interval);
  digitalWrite(LED_BUILTIN, LOW); // send 0 volts 
  delay(interval);
}

void endword() {
  digitalWrite(LED_BUILTIN, LOW); // send 0 volts  
  delay(4 * interval);
}

void endletter() {
  digitalWrite(LED_BUILTIN, LOW); // send 0 volts  
  delay(2 * interval);
}

void A() {
  dot();
  dash();
  endletter(); 
}

void B() {
  dash();
  dot();
  dot();
  dot();
  endletter(); 
}

void S() {
  dot();
  dot();
  dot(); 
  endletter(); 
}

void O() {
  dash();
  dash();
  dash();  
  endletter(); 
}

Advancers – Spirals

This week Eoin led the Advancers group, we looked at drawing in Scratch, using the Pen blocks and some simple maths to draw some patterns.

As always, we started with a Plan:

A Plan

  1. A Button Sprite – to start the drawing.
  2. A Simple Sprite to do the drawing.
  3. Some maths to make it draw a Spiral

We started by drawing a square, to draw a square in Scratch we used the Pen down, Turn and Move blocks.

To get a spiral effect we had to make sure that we moved a little bit further each time we drew our square.

We also decided that we should be able to make spirals with different shape so we  needed some variables to help:

  • Shape – This would tell us how many sides the Spiral should have
    • We made this in to a Slider on the screen so it was adjustable.
    • Min value was 3 and Max value was 100
  • Degrees – This would tell us how much extra to turn, this made the patterns a lot more interesting.
    • Again, we made this one into a Slider so we could adjust it.
    • Min value was 0 and Max value was 360
  • Size – This was an internal variable, which we used to keep track of how many Steps to move each turn, we also added a little bit to it each turn to make the Spiral pattern.

The drawing Sprite

This was the Sprite that did all the work. To work out how far we should Turn each time, we divided 360 by the number of Sides, we then added the degrees value to get the strange effects working.

The code ended up looking like this:

 

The Button Sprite

We used this sprite to start drawing, we used a broadcast so out button sprite could “talk” to out drawing sprite.

Oliver will be back next Saturday.

See you all then Declan and Eoin

CoderDojo Athenry Information Session, Sept 2019

Thank you to everyone who came along for our information session yesterday Saturday 14-Sep-2019.info_session

Michael introduced us to the CoderDojo movement and then talked to us about CoderDojo Athenry and what we have planned for 2018/2019. Michael’s presentation is here:CoderDojoAthenry-InfoSession-2019-Sept .

Julie then spoke about our loaner laptops where we provide laptops for people who don’t have their own. Speak to Julie or any of the mentors (we’ll be wearing blue t-shirts) for more information.

Martha then spoke about Health & Safety and our shop, where we sell tea/coffee with biscuits for €2.00 or €1.50 if you bring your own cup, with all profits going towards equipment etc. for our CoderDojo. The H & S and Coffee Shop slides are here: HS-and-Coffee .

info_session1

This year, we have 4 different rooms with different topics in them, for different levels of experience and age.

Explorers- led by Martha for Beginners from around age seven. They use the Scratch programmimg language to build games, quizes and animations. Here are Martha’s slides: CDA-Explorers-S8-Information-Session .

Advancers- led by Oliver is for kids who have already been through Explorers. They tackle more complicated programming concepts, during the year the Avancers group will investigate various topics like gravity, music and even what happens when water boils using Scratch.

Modellers- led by Kieran is for older kids who have been through Explorers and Advancers. The Modellers group will concentrate on making 3D game resources using apps like Blender and Gimp. Here are Kieran’s slides: CoderDojoAthenry-Modellers-KickOff-Sept2019.

Hackers- led by Michael for older teenagers who have been through Creators and Bodgers. The Hackers group themselves usually decide what topics they will cover with many of them working on their own projects. Here are Michael’s slides: Hackers-Intro-Slides.

Kids are free to move between rooms until they find a topic that suits them.

To find our schedule for the 2019/2020 sessions click on the schedule button at the top of this page.

If you have any questions or if you need any help, please talk to me or any of the mentors.

See you all next Saturday.

Declan and the CoderDojo Mentors