Arduino Challenge

Building and programming Arduino microcontroller projects.
Post Reply
Joe
Posts: 4
Joined: Wed Nov 01, 2017 10:13 am

Arduino Challenge

Post by Joe » Wed Nov 01, 2017 1:20 pm

To welcome three new schools to SARCNET and to get to know each other better we are having an Arduino challenge. The idea is to start with an already working program and to hack it to do something really cool. First, get the Arduino IDE running on your PC and attach your favourite Arduino board. Connect an LED, in series with a 470 ohm resistor, between any numbered pin (say pin 9) and GND. Open a new sketch, copy and run the following program:

Code: Select all

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

void loop() {
	digitalWrite(9, HIGH); 
	delay(100);
	digitalWrite(9, LOW);
	delay(100);
}
Image

Now, to create your own project: Add more LEDs, if you like, and change the code to make the LEDs flash in a really cool sequence. Build the Arduino and LEDs into a cardboard model. Take a video of your working project so that everyone can see what you have done. Note: To keep it simple we are only using LEDs and the functions shown above. Here are some project suggestions. It doesn't matter if you choose the same project as someone else as long as you do it yourself.
  1. Emergency flasher beacon (On-Off or SOS. 1 LED). Bike light.
  2. Hypnotic OWL (2 LED eyes) – Substitute your own cardboard animal or robot.
  3. Traffic lights (3, 6 or 12 LEDs)
  4. Airport runway approach lights (white) or port channel markers (red/green) (5 LEDs)
  5. Arrow sign (8 LEDs)
  6. Wearable Electronics: Name Tag (Morse code), brooch, pendant, earrings or necklace
  7. Roulette wheel or Dice (may need help to randomize it)
  8. 7-Segment LED Timer or Bar-graph LED Display
  9. Christmas tree lights
  10. Modern art or home decoration
Post your own project details, code snippets, problems and solutions here.

Joe
Posts: 4
Joined: Wed Nov 01, 2017 10:13 am

Re: Arduino Challenge

Post by Joe » Thu Nov 16, 2017 11:11 pm

While doing the Arduino challenge some students have noticed that they have to repeat the same instructions over and over again. They asked if there is a way to make it easier. For example, Ethan is writing a program to make a 7-Segment LED display count from 0 to 9. After each digit, he needs to turn off all seven LED segments. The code is quite repetitive. Well, Ethan discovered a way to reuse blocks of code. It is called “functions”. In fact you have been using functions probably without noticing it: Remember our Arduino mantra for creating a new program:

“Void setup round-brackets curly-brackets. Void loop round-brackets curly-brackets” Here is what that looks like:

Code: Select all

void setup() {
}

void loop() {
}
The setup and loop blocks above are actually “function declarations”. The Arduino must have them to setup and run your program. You already know to put your own code in-between the curly brackets of these functions. Well did you know that you can create and use your own functions? Since Ethan wants to turn off all his LED segments, connected to pins 2 to 8, he created a function called “turnOff”, which did just that. Here is what it looks like:

Code: Select all

void turnOff() {
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        digitalWrite(6, LOW);
        digitalWrite(7, LOW);
        digitalWrite(8, LOW);
}
He put this code right at the start of his program so that it can be used anywhere within the setup and loop functions. Now instead of having to type out all seven lines of code, again and again, he just inserts the following line in his code whenever he needs it:

Code: Select all

turnOff();
Pretty clever, eh? Well done Ethan.

Joe
Posts: 4
Joined: Wed Nov 01, 2017 10:13 am

Re: Arduino Challenge

Post by Joe » Sat Nov 18, 2017 7:03 am

Continuing on with our discussion of functions - arguably the most powerful part of any programming language - we saw how functions can replace a whole block of instructions, just by typing their name. But what if you wanted the function to do something a little different each time you called it? For example, suppose you connected three LEDs to our Arduino (pins 2, 3 and 4) and you wanted it to flash each one on for 1, 2 and 3 seconds. You would normally have to write the following instructions:

Code: Select all

digitalWrite(2,HIGH);
delay(1000);
digitalWrite(2, LOW);
digitalWrite(3,HIGH);
delay(2000);
digitalWrite(3, LOW);
digitalWrite(4,HIGH);
delay(3000);
digitalWrite(4, LOW);
Looking at the code above, it is hard to tell what it is doing and it would be hard to change the pin numbers and the delay periods later on. Wouldn’t it be cool if you could just write:

Code: Select all

flash(2,1000);
flash(3,2000);
flash(4,3000);
Yep! So, to do that you just create a function, like this, and put it at the top of your program:

Code: Select all

void flash(int pin, int period) {
	digitalWrite(pin,HIGH);
	delay(period);
	digitalWrite(pin, LOW);
}
Now if you write “flash(2,1000);” in your code, the flash function above is called with the numbers 2 and 1000 passed to it. We call these numbers "parameters" and their order is important. Inside the function, the first parameter is stored in an integer variable called “pin” and the second parameter is stored in an integer variable called “period”. So the function writes to the pin and delays for the period that you specify.

More on functions later...

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest