Search

Learning Arduino - Day 1

Now I've paid my tenner a month, I'm officially a member of Bristol Hackspace, the experimental group associated with Dorkbot Bristol, I feel emboldened to start playing with the Arduino microprocessor, a favourite device in the tech/arts scene.  I'm reassured  by the knowledge that I now have a group who can help me out when the going gets tough.

This is the first blog entry which I'll use as a note book of my learning.

Yesterday afternoon I went online to Cool Components and purchased the Arduino Uno. It arrived the next morning this morning and I've just got round to starting.

My guide this evening has been the Arduino home page http://arduino.cc/en/ . I just followed the getting started instructions and found them very clear. Since the tiny board can be powered off the USB port as well as 7-12v DC from a battery or transformer,  I just had to find a USB A to B lead, plug it in and, lo,  the green power light goes on. Good. But there's an orange LED blinking erraticly - is that OK?

For my sins I'm using Windows XP SP2 on an oldish Compaq laptop. Downloading the 85Mb Ardino environment took a little but when the application was installed and started, it had the reasuring style of the Processing environment on which it is modelled.

The instructions on installing the device driver were very clear and the board was soon connected on, for me, COM6.

The first example script which does anything is 'blink' , a simple loop which turns the onboard LED connected to pin13 on and off at the rate of once every second.  One click and it downloads to the board, the Rx/Tx lights blinking reasuringly. A short pause and the LED starts blinking slowly.

Now to modify this code.  Maybe a morse code flasher.  First a couple of functions to flash a dot and a longer dash. A dash is equal to three dots and there is a dot length gap between letters. I wrote a couple of functions called short and long (sic):

 void short () {
  digitalWrite(13, HIGH);   // set the LED on
  delay(500);              // wait for a  half a second
  digitalWrite(13, LOW);    // set the LED off
  delay(500);              // wait for a  half a second
}

void long () {

}

 void loop() {
   short();short();short();
   long();long();long();
   short();short();short();
}

Next tried to download the revised code -  lots of error messages of the kind:

  error: expected unqualified-id before ')' token

What?  Perhaps I've got the syntax for declaring functions wrong - what's it supposed to be?  I search the documentation and find nothing - are functions allowed?  A google search finds a functions page on the Arduino site but I would have found it faster using the search box on the site itself. Doh!  Functions look Ok and then it dawns - reserved words!  Not very impressed by the error message though, but that's the weak point of so many languages - it's a hard problem.

Changing to dot and dash, download and we have a board signalling SOS for help.

 void dot () {
  digitalWrite(13, HIGH);   // set the LED on
  delay(500);              // wait for a  half a second
  digitalWrite(13, LOW);    // set the LED off
  delay(500);              // wait for a  half a second
}

void dash () {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1500);              // wait for a  half a second
  digitalWrite(13, LOW);    // set the LED off
  delay(500);              // wait for a  half a second
}

Don't like those magic numbers:

  const int d = 500;
  const int led = 13;

and I need to represent inter-letter and inter-word gaps. Called them letter-space and word-space. Wrong -  hyphen not allowed in variable names but underscore is OK. That's better but too slow - changed d to 300.

So how about doing the text to morse decoding on the board.  I seem to remember that the light on Cabot Tower in Bristol flashes out "BRISTOL" in morse  - ah I google that apparently it flashes morse no longer - pity.

How to code the morse letters - a simple array providing a coded pattern SSS LLL which is interpreted to call the functions indexed by an offset ASCII value of the character would do  Maybe code dot as 0, dash as 1  - ah but  we have dot and dot dot and dot dot dot  which would all code as zero. But this route is just normal programming - and its been done - better get back to the hardware.

It seems I could control the brightness of the led - ah but not the on-board led which is connected to pin13 - on the Uno the pins which are analog-output using PWM (Pulse Width Modulation)  are 3, 5, 6, 9, 10, and 11 - I'll have to get my jumper leads and a breadboard for this.

The Uno is still quietly flashing for help as I work though this - rather strange but reassuring.

 

 

 

 

 

 

I'm sure you'll think of a use for it on the boat!
Hi Paul. I rather like the idea of mimicking the light characteristics of lighthouses, so I could have a little Eddystone light -inside- the boat!
You could of course mimic any lighthouse you wish, maybe as an aid to identification? Not that you would need it, I'm sure.

Or as a navigational aid for model yachts?