Instructables

Step 13: Programming

Picture of Programming
Now it's time to program the atmel. I used avr-gcc (a free compiler) to compile my code, and avrdude to burn it on to my chip.

Unfortunately, I left my code behind on the shop computer when I moved out to LA at the beginning of the summer, and there's a possibility that the code is lost and gone forever. Rather than try to rewrite it(although I'll probably find a copy when I get back to LA in a few days--I'm in frisco now), I'll try to walk you through programming techniques you can use for this baby. Wouldn't you rather learn to fish than eat fish, anyway?

Here' s the basic theory behind the code:
Drawing is an open-loop process. The only way we have of knowing how far the etch-a-sketch has drawn is by figuring out how long we've been turning the motor at a constant speed. The problem, of course, is that the motors vary their speed based on the battery voltage, which could change from day to day. I could regulate the motor voltage, but I find that idea abhorrent and unnecessary. Instead I'll sit back and listen to the song 'Frankly, Mr Shankly' by the Smiths with a smug expression on my face.

To get around this, I pretend I'm drawing vector images (i.e. images that are composed of scalable lines and curves). I make a global variable in my code called scale, and if I had a function such as drawLine(angle, length), I would call it with an arbitrary number for length, say 3. Inside the function, I have code that says: Draw a line at the proper angle for 3*scale seconds

Does that make sense? Good.

Here are some basic functions:

void drawRight(unsigned char distance)
{
PORTB=1;
wait(distance*scale);
PORTB=0;
}

void drawLeft(unsigned char distance)
{
PORTB=2;
wait(distance*scale);
PORTB=0;
}

void drawUp(unsigned char distance)
{
PORTB=4;
wait(distance*scale);
PORTB=0;
}

void drawDown(unsigned char distance)
{
PORTB=8;
wait(distance*scale);
PORTB=0;
}

for those of you who don't want to write it, here's the wait function:
void wait(int time)
{
int count, count2;
for(count=0;count<1000;count++) //we're going to burn lots of cycles here.
for(count2=0;count2<time;count2++)
;
}

OK, this ought to get you going. Play around with this code for a bit and draw lots of horizontal and vertical lines. Maybe a box. In the next section I'll talk to you about curves.

While you program, check out the indie label Kill Rock Stars. Next to Matador, it's one of my favorite labels. ok, cool.
 
Remove these adsRemove these ads by Signing Up