Tuesday, 29 May 2012

Spin Doctors

I have been collecting these for a while now. It seems the same patterns come up over and over. The specifics change, of course. And it isn't just politicians, you can find anyone using these from middle management to military dictators. There's probably a spin-doctor handbook somewhere that has the complete set with detailed instructions.

The report is out of date

This is when some report or finding appears condemning something they are responsible for. The response is to effectively claim they already fixed the problem, which might be true, or might not.

We're still in discussions

When a proposal comes to light that people are unhappy about. Often the people they are in discussions with are some large business who stands to gain heaps, but sometimes they are just talking to other politicians. This implication here is that we should not take this too seriously because it all might change and they are just playing with ideas. This is never the case, of course. If we stop yelling about it they will do the thing we fear. They might anyway. After this phase you often hear:

It's a done deal, we can't change it now

They don't say it like this, of course. They say it is a great deal that they have achieved by being such good leaders etc, but the deal is done. The documents have been signed and backing out of the deal would now cost an enormous amount of money to recompense other parties for broken promises.

There's been a cost blowout

Yes, even though they said the deal was done and we couldn't change it, we find that other parties involved can change what it costs. That means they can make us pay more (for this deal we never wanted in the first place). There is a variation on this one where they have to pay heaps (of our money) to sack some highly placed official because they are breaking a contract. The official is being dismissed for incompetence or worse, but we still have to pay a golden handshake for some reason.

I'm sorry you took offence

This is when they screwed up and can't quite wriggle out of something that looks like an apology. It isn't actually an apology, though. It just seems like one. To be fair I have heard at least one politician apologise properly saying something like "I screwed up. No, I can't blame anyone else, it was me and I'm sorry." But this is astonishingly rare. And that one I recall was preceded by an attempt to squirm out of it.

I will sue you for defamation

Sometimes they mean it, sometimes they don't. They seem to say it more than mean it though. And we have to wonder, when they don't follow through, if the defaming statement were true. There are other reasons not to sue (too busy, too expensive etc) but threatening it in the first place and not following through is the mistake.

I am absolutely and completely in support of motherhood and apple pie

Of course they are, we all are. But the question was: why are you cutting support for solo parents trying to feed their children? The trick here is switching or simplifying the argument so that the real point is obscured. We are supposed to nod in agreement because they've picked something we all support, so we agree with them, don't we? That's all fine then. There are endless variations of this. A recent local example was a politician who was responding to the suggestion someone had organised a discounted hotel stay for him (which could be construed as a bribe). He vehemently responded that he had paid for the hotel himself and it was on his credit card. Of course it was, we knew it would be. But that was not the accusation. It was, indeed, on his credit card and the rate was indeed discounted.
We can't afford it
No, we can't afford to do this as well as the other stuff we'd rather you didn't spend our money on. We probably can afford to do this if we stop doing something else.

Sunday, 13 May 2012

Playing a melody on ATTiny85

This is fairly trivial but I wanted to document the steps carefully.
The code is here and I flashed the ATTiny85 using the steps I referred to here.

The flash pins are still connected though only VCC abd GND are necessary to run the program because they supply power. I have 5V running across them which is coming from the Teensy which, in turn, gets it from the USB.

So save confusion I am going to refer to the ATTiny pin numbers with a #, so #4 is GND and Arduino Pin 3 is #2 etc.

So the speaker is connected across #3 and #4 (Arduino Pin 4 and GND). I tried several speakers ranging from 9 ohm to 47 ohm. Not a lot of volume but quite audible. There were no resistors or capacitors etc.

Once running the program plays a 7 note melody over and over until you get annoyed with it and turn it off.

But there is a critical step I missed the first time around. There are several implementations of the ATTiny core for Arduino. Not all of them support the tone() function because it is tougher to do on an ATTiny (so fair enough).  Anyway the one I used is here. That also lists a bunch of other implementations so there is a lot of choice. The readme tells you how to add it to your Arduino directory.

Once it is installed you do need to pick the right ATTiny85 option in Tools->Board. There are several and the one I found that sounded right was "ATTiny85 @ 1 MHz (Internal oscillator, BOD disabled)" The others are oscillating at different speeds which means the clock doesn't run at the speed you want and the melody is scrambled.

I have the melody working on #3 just fine although it is not documented in the diagram as a PWM pin. In fact #2, #3, #5-7 all work. This may be because the Arduino core I am using is not actually using PWM for the tone(). Not sure. It works anyway. The comments in the core code match the diagram above , ie #5 and #6 are PWM and no others.

Saturday, 12 May 2012

Flashing an ATTiny85 from a Teensy.

Here's the project: flash an ATTiny chip from a laptop using a Teensy board.

I've been using Teensy boards for several things lately and they are very good. But for one project it is overkill. Teensy boards can blink lights just fine, but so can ATTinys, and ATTinys are smaller and cheaper.

But flashing a Teensy is very simple, you just plug a USB cable into it and use the usual Arduino IDE to load your code directly from a desktop or laptop. Not so with an ATTiny because it isn't as clever. I could mention boot loaders and similar but I'd get out of my depth fairly soon.

To flash an ATTiny you need to use the SPI protocol and you won't get your laptop talking that through a USB cable, so you need something to act as a go-between, taking the serial data being sent down the USB cable, turning it into SPI and pushing it through to the ATTiny. Obviously this is complicated. The go-between is called an ISP (In-System Programmer).

People use a custom box for this such as a USBTinyISP or they use an Arduino board as an ISP like this. But my projects tend to be very compact which is why I tend to go for Teeny boards, the smaller of which is about the size of the end of my thumb. So I have various Teensys lying around but no Arduinos. Fortunately someone already figured out that you can use a Teensy as an ISP just like an Arduino (there are a lot of similarities between Teensy and Arduino, including using the same IDE).

So we're all good, except the technique didn't actually work.

Here is what I did to get it working:
  1. Follow the steps in this link (same as the one above), except I used a Teensy++ 2.0 and an ATTiny85 chip. These probably don't make much difference, but the pins have different numbers.
  2. I'm using ArduinoIDE 1.0 like he is, and the Teensyduino software. He's using the 64 bit version and I am using 32 bit version, again should be no difference.
  3. I used a modified copy of the ArduinoISP software which I've copied to here. I got this from here and changed the pins to work with the Teensy++ 2.0. So this version has the pins for the Teensy++ 2.0 (this is probably the big difference, ie what made it actually work)
  4. The program for the ATTiny is here. This is just a modification of the blinky example for Arduino. Again, just the pins are different from the Arduino standard.
  5. I did edit the arduino-1.0/hardware/arduino/programmers.txt to arduinoisp.speed to 9600. I forget where that was recommended. My modified ArduinoISP code has a similar change.
This is what my breadboard looks like:

 That's the Teensy on the left, with the USB on the extreme left. In the centre is the ATTiny85 and over on the right is a blinking LED. As you can see there are no resistors or capacitors, just simple connections (the two exceptions are the resistors needed on the LED). Some sites will tell you that you need other bits but I didn't for this.

The wires here aren't that easy to see but I've got GND on the top rail and VCC on the second rail. Near the VCC in the upper left of the Teensy we have MISO, MISI and SCLK across to the corresponding pins in the ATTiny. SS is connected to RESET on the ATTiny.

There is a green LED on pin 3 of the ATTiny85 because my test program will flash the LED attached to that pin. The blue LED flashes when the Teensy is flashing the ATTiny. I ought to add a red LED to indicate an error on pin 8 of the Teensy but I didn't. In fact both of these 'LEDs' are actually just one RGB LED.

I made the heartbeat flash the on-board LED on the Teensy so that I can tell the ISP code on the Teensy is running okay.
It is worth noting the options I used to upload the ISP code from the ArduinoIDE
The board selected is Teensy++ 2.0 and the Programmer is AVRISP MKII, which is just normal for Teensy programming.

But for the ATTiny we need this:
In this case the board is the ATTiny. There are lots of ATTiny options but any ATTiny85 does for this program. The Programmer is Arduino as ISP (and the Arduino IDE does not care that it is a Teensy instead).

This allows you to do everything from the Arduino IDE, as long as you set the options correctly. But ArduinoIDE actually uses avrdude to transfer the files to the Teensy ISP. This is the command it ends up using:

/home/roger/kits/arduino-1.0/hardware/tools/avrdude -C/home/roger/kits/arduino-1.0/hardware/tools/avrdude.conf -v -v -v -v -pattiny85 -cstk500v1 -P/dev/ttyUSB000 -b9600 -Uflash:w:/tmp/build4483866087774593940.tmp/BlinkAttiny85.cpp.hex:i

Your arduino directory will be different to mine but that is easy to tidy up. The reason this is interesting is if you want to use the gcc compiler to build your code instead of ArduinoIDE. You can then invoke avrdude manually using a similar command to this and the hex file will transfer down.

For a handy reference to the pins of the ATTiny and others use this.