Friday, December 16, 2011
A stack on the Arduino
I have put together a collection of Python scripts which allow me to build proper C++ programs for the Arduino Uno and Mega 2560 from the command line, and upload them all in one go. At the moment, these are only working in Linux but I think I know enough now that in principle I could get this going on Windows, possibly within Dev-C++ - a project for after Christmas, I think.
Wednesday, December 14, 2011
Home Made
I used to be really, really terrible at soldering things. Thank goodness for instructional videos on YouTube. The board was programmed by the method described in the previous post.
Bypassing the IDE
A colleague has objected, justifiably, that the Arduino can't be used for teaching C/C++ as it stands because of the way the environment works (hiding the main function, and confusing issues of scope, global variables, etc). Judging by a conversation I had this afternoon there is probably no money in the equipment budget to buy fifty of the things for next year's programming course anyway, but it would be nice to try. There are various tutorials on the web dealing with using the avr-gcc compiler directly, mainly reliant on using modified makefiles. The Arduino 1.0 release seems to have broken these (some of the files are now in different directories, some have disappeared), so I tried looking at the output if you ask the Arduino environment to give detailed output on compilation and upload (in File / Preferences).
Friday, December 9, 2011
Arduino classes
Underneath the Arduino programming environment is C++, but I didn't think I'd be trying to find my copy of Leendert Ammeraal's C++ for Programmers this Friday afternoon (I have Stroustrup, of course, but it's not a book to find a quick and easy solution in). I hooked up four of the RGB LED units to the Arduino Mega's 12 PWM pins, and wanted to implement a little algorithm I had run with one unit earlier in the week - set a random "target" value for the RGB values, gradually increase (or decrease) the intensity to reach the target, and then set a new random target. The effect is to gradually change the colour and intensity of each unit, but in a pleasant drifting manner rather than jumping around all over the place. Once I had written the code, I looked for ways to make it simpler - first using a struct, but there are difficulties writing functions for the Arduino which take a struct as a parameter (as I understand it, the pre-processor shuffles the code around so that your function ends up before the struct declaration, which means the compiler doesn't recognise the struct when it comes to compile the function). The answer was to go straight to writing a C++ class, which turned out to be fairly simple (though I did try writing a proper constructor function, and was reminded what a complete bloody nightmare that can be especially on a Friday afternoon in a very warm office when you haven't written any O-O C++ code for about a decade - once you start dabbling in constructors, you need to think hard about copy constructors, destructors, deep and shallow copies, all that stuff).
Arduino Mega and ethernet shield
I bought an Arduino Mega 2560 to reward myself for doing well in a philosophy module. To connect it to the ethernet shield, the solution in the Arduino forum works - connect pins 11, 12 and 13 on the ethernet shield to 51, 50 and 52 on the Mega (in that order, of course).
Edit: the Mega sorted out the problems I'd been having with getting a web server to read off the SD card - with the Arduino Uno it kept falling over and resetting the board, with the same code on the Mega I was able to run a slow, but quite usable, server.
Edit: the Mega sorted out the problems I'd been having with getting a web server to read off the SD card - with the Arduino Uno it kept falling over and resetting the board, with the same code on the Mega I was able to run a slow, but quite usable, server.
Thursday, December 8, 2011
Scrolling text with shift registers
Using a couple of 74HC595 shift registers, I have done away with most of the wires leading from the Arduino to the breadboard. This also simplifies the code - simply send a couple of bytes to the first shift register, and the data sorts itself out (the first byte is displaced by the second one and flows into the second shift register - I will probably add a third to the chain, to control the green LEDs). The text is now proportionally-spaced, rather than using 8 pixels per character.
Edit: adding a third shift register to the chain worked, but at the price of making the display rather dim (because of the time spent transferring all the data, I suspect). The green LEDs aren't as bright as the red ones, which made the effect worse - the red ones were just about acceptable.
Edit: adding a third shift register to the chain worked, but at the price of making the display rather dim (because of the time spent transferring all the data, I suspect). The green LEDs aren't as bright as the red ones, which made the effect worse - the red ones were just about acceptable.
Wednesday, December 7, 2011
Space invaders!
This is heavily adapted from the code on the Oomlout web site. I'm defining "sprites" called invader1, invader2 and blank, then in the loop() function I have an array of pointers to these sprites. There is an array led_state which stores the current state of the LED array, and at each cycle I am shifting every row rightwards and overwriting with the relevant portion of the next sprite. There is a lot of bit-masking and shifting going on. The next project is to replace the unwieldy wiring by using the shift register chips which are visible on the bottom right of the video - as described on the Arduino website.
Subscribe to:
Posts (Atom)