Monday, December 28, 2009

Byte vs int

I've just checked the difference between declaring pin numbers as byte or int.  There is no difference to the size of the compiled code, so there is not likely to be a performance difference.  Since it probably saves one byte per constant I'll try to use byte not int.

Now the code

I created a simple piece of code to test the schematic and my understanding of the way things work, which is below.  The best thing about Arduino is that the code is C, not assembler.  I have been reading the examples and the reference stuff on the web site  then I wrote the code below in one go.  It worked first time after only one failed compile for a missing semicolon.  In PIC assembler the chance of me writing code like that and getting it to work at the first attempt is zero. 

There are four sections to the code.  The first is the declarations section and defines the names of the pins to use.  The second is the setup routine, that is stuff that only happens once.  The third section is the loop which repeats for ever while there is power.  Lastly there are subroutines.

In the declarations I use const for pin names because they won't change.  I use int because all of the examples do, but really I feel we should use byte.  byte is only one byte, (int is two) and byte can only be positive.  These suit the pins perfectly and would save one byte per pin - a few bytes might be important on these small controllers, but not in this case.  There could be a down side if conversions take place - I'll experiment later.  Setup includes a call to a routine which blinks the on-board LED.  If the board resets for any reason the blinks will give it away.

The loop sets up the motor in one direction and steps up the speed to full speed in the for loop.  It then stops the motor, giving it time to stop, reverses the motor and steps it up to full speed again.  Then it stops the motor before the loop repeats.

The subroutines only have the blink routine, used to blink the on-board LED.

const int mot1Pin = 7;
const int mot2Pin = 8;
const int enablePin = 9;
const int ledPin =  13;

void setup() {
  pinMode(mot1Pin,OUTPUT);
  pinMode(mot2Pin,OUTPUT);
  pinMode(enablePin,OUTPUT);
  pinMode(ledPin, OUTPUT);

  //demonstrate a reset by flashing the led
  blink(ledPin,3,300);
 
  //disable the motor
  digitalWrite(enablePin, LOW);
}

void loop()
{
  // simple test to change a motor speed bit by bit
 
  // first set the motor in one direction
  digitalWrite(mot1Pin, LOW);
  digitalWrite(mot2Pin, HIGH);
 
  //run the motor in stages up to full speed
  for ( int i=0;i<255; i+=60) {
    analogWrite(enablePin, i);
    delay(1000);
  }
  analogWrite(enablePin, 255);
  delay(1000);
 
  //stop the motor
  analogWrite(enablePin, 0);
  digitalWrite(mot1Pin, LOW);
  digitalWrite(mot2Pin, LOW);
 
  // pause to allow stop
  delay(2000);
 
  //reverse
  digitalWrite(mot1Pin, HIGH);
  digitalWrite(mot2Pin, LOW);
 
  //run the motor in stages up to full speed
  for ( int i=0;i<255; i+=60) {
    analogWrite(enablePin, i);
    delay(1000);
  }
  analogWrite(enablePin, 255);
  delay(1000);
 
  //stop the motor
  analogWrite(enablePin, 0);
  digitalWrite(mot1Pin, LOW);
  digitalWrite(mot2Pin, LOW);
 
  // pause to allow stop
  delay(2000);
}

// blink an LED
void blink(int pin, int num, int pause) {
  int p=pause/2;
  for ( int i=0;i
    digitalWrite(pin, HIGH);
    delay(p);
    digitalWrite(pin, LOW);
    delay(p);
  }
}

Saturday, December 26, 2009

Schematic





Working from a diagram helps when you're building something, but what if you don't have one?  No problem, create your own.  I have just had a go with Cadsoft Eagle and produced a schematic for driving a motor from an Arduino board. I don't think my design standards are too good, but it does the job.  There wasn't a schematic for a motor in the library, but since the software is aimed at creating a board and motors are not usually soldered to a PCB that probably makes sense.

If you're thinking of using this schematic, and you are free to do so, then I suggest you wait until I've built it and tested it.  The next stage is to write the software.

Saturday, December 19, 2009

L293D

The motors arrived today. Yesterday I bought some more rechargeable batteries and charged them overnight.  I had a few things to do today, so only time to take a look at the datasheet for the L293D motor driver.  It would work with low voltage motors, so I needn't have waited.  The L293D will drive two motors.  It has a two voltage supplies, one for its control circuits and one to drive the motors.  It can supply over 1A to each motor, which is fine for some tests.

One thing I am short of is a decent power supply.  Batteries are great for remote stuff but while testing things on breadboard a decent power supply is very useful.  I might find one locally, buying from T'Internet just before Christmas can lead to delays.

I'll try to knock up a breadboard circuit to drive a motor in different ways next.

Friday, December 18, 2009

Serial comms

One thing about Arduino that I liked the sound of is the option of communicating directly with a PC via USB.  I have looked at how it's done, the simplicity is great, these two lines of code send the text to the PC.

Serial.begin(9600);
Serial.println("Hello world!");

There's also the means to send data to the controller.  I have looked at this a bit more and it turns out that there are two pins that act as the send and receive and run at TTL levels, as you might expect.  The clever bit is in two parts: first the interface to USB and second the language support.  The interface is provided by FTDI hardware.  This is available separately and could easily be used for other microcontrollers.  The language support is what really makes it easy.  The Serial object means comms to a PC, or to other serial devices is a breeze.

Thursday, December 17, 2009

Motors

I want to have a fiddle with some motors.  I have some 1½ - 3v motors but the half bridge driver I have will only work for motors 4½v or more.  I've ordered some 3-6v motors from MUTR so I can be thinking about what to do in the meantime.

The controller can produce PWM directly from some pins, which is a good way to control the speed of the motor, so I'll look a that.

While I'm waiting I want to try sending a message back to the PC.  Feedback like that should be very useful.

Wednesday, December 16, 2009

What's Arduino

According to the Arduino website
"Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments."

I have used Microchip PIC microcontrollers for fun projects in the past.  They are cheap and there are lots of options for different roles.  There are drawbacks though.  First is the language.  You have to write code in PIC assembler, and life is too short.  The programming boards expect to connect with a serial port, not USB and the port is one-way to the controller, so feedback is poor. I have a PC, with a lovely screen and keyboard, so why not use that for debugging?  The programming board also needs a separate power supply.

Arduino is more modern.  The board is small and neat. It has a USB connection, which is two way and can supply the power for programming the processor.  The language to program the controller is C - not the friendliest language usually, but so much better than assembler.

I think there could easily be crossover projects where each controller could do a specific task.  There are communication protocols in both families so they should talk to each other, but that is some way away yet.


I bought a Duemilanove 328.  It is all based around an Atmel ATmega328 controller, with the Arduino bootstrap pre loaded.  The chip is available separately of course, so the board is a programming environment and a test environment too.

I'm looking forward to working through the examples.

Arduino

My Arduino Duemilanove arrived today. I added the bits to Ubuntu as described on the web site, downloaded the IDE and it loaded. I am using Java 6, not 1.5 as described and all seems well. I checked the board type was right and set the USB port to use. Loaded the blink program, which must be the electronics "Hello World" equivalent and plugged in the board to a USB cable. Clicked download, the board reset and the LED started to blink. I changed the space and mark length, uploaded it and the LED responded. This is so easy! Writing simple C code rather than PIC assembler is a doddle.

What's next?