Show Posts
Pages: [1] 2 3 ... 5
1  Forum 2005-2010 (read only) / Syntax & Programs / Re: phase-shifted PWM on: April 08, 2008, 08:35:09 pm
// stdint.h has types such as uint8_t and uint16_t
#include <stdint.h>

// inherent wrapping of limited data type does all the phase stuff you need during a simple add operation
// note that (255 + 1) & 255 = 0, which is exactly what happens when we work with 8 bit variables
uint8_t var, phase;

#define INITIAL_PHASE 30

// we recalculate the phase angle for 128 = 180 degrees, 256 = 360 degrees so simply adding does everything we need
// 30 degrees comes to about 21
for (var=0, phase=255 * INITIAL_PHASE / 360;;var++) {
  doSomethingWith(var);
  doSomethingWith(var + phase);
}
2  Forum 2005-2010 (read only) / Syntax & Programs / Re: desired result not being achieved with button on: April 02, 2008, 10:09:43 pm
Whenever I've needed to do delays in this sort of loop, I simply wrote the next value out just after reading the columns rather than just before.
3  Forum 2005-2010 (read only) / Syntax & Programs / Re: desired result not being achieved with button on: April 02, 2008, 08:42:57 pm
Quote
yeppers, i don't have the exact values on hand, but i have, so here are the colors:

brown black orange resistor connected to ground
connected to the brown black orange is both the button output, and a brown black brown resistor, which is connected to the pin.

10k to ground and 100r to input, sounds good. input resistor isn't really needed but shouldn't hurt either.

btw, how much time are you leaving between setting the outputs and reading the inputs? keypads and their connecting wires have enough capacitance that if you do one then immediately the other, you may get bogus readings. a couple dozen microseconds should do it, maybe count to 32 or something.
4  Forum 2005-2010 (read only) / Syntax & Programs / Re: desired result not being achieved with button on: April 02, 2008, 07:07:03 pm
do you have resistors pulling the columns low when nothing is being pressed? Without that, the inputs will simply go high and stay there as soon as you press a button, and their initial state will be subject to random fluctuations in the electric fields around the project.
5  Forum 2005-2010 (read only) / Syntax & Programs / Re: TTYUSB Problem on: April 03, 2008, 02:32:02 am
try stty 9600 raw ignbrk < /dev/ttyUSB0

add -hup if you don't want the arduino to reset when you open the port, otherwise wait a second or two after opening before trying to communicate.

Also note that the bootloader runs at 19200 baud.
6  Forum 2005-2010 (read only) / Syntax & Programs / Re: 10 bit pwm, How? on: March 31, 2008, 08:37:01 pm
see table 15-4 on page 133. You want to

TCCR1A |= (1<<WGM11) | (1<<WGM10);
TCCR1B |= (1<<WGM12);
TCCR1B &= ~(1<<WGM13);

which will set WGM1[3..0] to 0111 which according to table 15-4 is 10-bit fast pwm. If you want phase correct PWM, move WGM12 to the &= ~ line so it's set to zero instead of 1.
7  Forum 2005-2010 (read only) / Syntax & Programs / Re: can't make a fade effect on: March 28, 2008, 12:43:54 am
more like:

int direction, counter;

void c_init() {
  counter=0;
  direction=1;
}

int c_iterate() {
  counter+=direction
  if (counter >= 255) direction=-1;
  if (counter <= 0) direction=1;
  return counter;
}

for (c_init();smiley-wink
  doStuffWith(c_iterate()); // or whatever works for your program
8  Forum 2005-2010 (read only) / Interfacing / Re: Simple Perl code for interfacing with Arduino on: May 04, 2008, 10:27:41 am
stty raw is your friend

Also, if you stty -hup, it shouldn't reset next time the port is opened.

Check out http://hc11-ide.funkmunch.net/bart-v3/sender.pl for an example of a perl serial I/O utility I use for a different microcontroller.
9  Forum 2005-2010 (read only) / Interfacing / Re: HOWTO avrdude+linux+diecimila on: April 20, 2008, 05:27:29 am
Just thought I'd add that I successfully reprogrammed the FT232RL on my diecimila to request 500mA from my USB port. See MProg tool from ftdichip.com. Unfortunately, I had to run it in virtualbox, ftdi_eeprom appariently doesn't support ft232R yet due to insufficient documentation from ftdi smiley-sad
10  Forum 2005-2010 (read only) / Interfacing / HOWTO avrdude+linux+diecimila on: March 27, 2008, 09:53:27 am
This short howto covers uploading from a terminal without using the arduino software at all.

1) howto reset diecimila, then upload with avrdude just like arduino software

stty 19200 raw ignbrk hup < /dev/arduino && avrdude -cstk500 -b19200 -pm168 -P/dev/arduino

The key here is the 'hup' attribute which tells linux to toggle the DTR pin when the port is opened and closed. The DTR pin going low when the port is opened is what causes the diecimila to reset, so this is perfect.

NOTE: some linux kernels in the 2.6.22+ range had a bug in the ft232 driver where DTR is never de-asserted on close. Symptoms are, even with above code, diecimila only resets the first time you program after plugging in. patch is available at http://lkml.org/lkml/2007/10/6/14 if you are affected.

I put both of the above in a script for easy reference. Replace /dev/arduino with /dev/ttyUSB0 or whatever it is on your system, or continue reading

2) howto ask udev to create a /dev/arduino symlink for you

I added

SUBSYSTEMS=="usb", ATTRS{product}=="FT232R USB UART", ATTRS{idProduct}=="6001", ATTRS{idVendor}=="0403", NAME="%k", SYMLINK+="arduino", MODE="0660"

to /etc/udev/rules.d/10-local.rules

If you'd like it to create particular symlinks for particular arduinos, run

udevinfo -a -p /sys/bus/usb/drivers/ftdi_sio/* | grep serial

and add the first line to the above udev rule with appropriate commas. The rule might look like:
SUBSYSTEMS=="usb", ATTRS{product}=="FT232R USB UART", ATTRS{serial}=="A1001Ncz", NAME="%k", SYMLINK+="arduino_1", MODE="0660"

which will create a /dev/arduino_1 symlink only for ft232R chips with that serial number.
11  Forum 2005-2010 (read only) / Development / Re: Do unused functions in a library use memory? on: March 31, 2008, 05:34:44 pm
I haven't played with .a/.la much, but .so is easy; just gcc -o libsomething.so --shared source1.o source2.o source3.o
12  Forum 2005-2010 (read only) / Development / Re: Do unused functions in a library use memory? on: March 27, 2008, 04:23:58 pm
Not sure if sketch uses gcc, but if it does, gcc and ld have options to leave out functions that are never used. You want to either make separate object files for each section you want to be optional, or find the gcc option that puts each function in a separate section. Then, you want to find ld's section garbage collection option which removes unreferenced sections.

I believe avr-libc does this already, or even simple programs would be enormous.
13  Forum 2005-2010 (read only) / Development / Re: how to enhance this  code? on: April 01, 2008, 08:28:58 pm
Try something like this:

enum
{
 forward_action
 backward_action
 stop_action
 rolleyes_action
} action_enum;

uint16_t actions;

void forward()
{
  setMotor(mLeft, 255);
  setMotor(mRight, 255); // or whatever you do to go forwards
  actions &= ~forward_action; // we're going forwards now, don't need to come back to this function
}

void backward()
{
  setMotor(mLeft, 0);
  setMotor(mRight, 0); // or whatever you do to go backwards
  actions &= ~backward_action; // we're going backwards now, don't need to come back
}

void stop()
{
  setMotor(mLeft, 128);
  setMotor(mRight, 128); // or whatever you do to stop
  actions &= ~(forward_action | backward_action | stop_action); // make sure we kill all movement commands
}

void rolleyes()
{
  setMotor(mEyes, 255);     // or however you do this in your robot
  if (digitalInput(inEyesHome))  // detect when eyes have returned to start point
    actions &= ~rolleyes_action; // and only then disable this action, otherwise we do it again every loop
}

void main()
{
  setup();  // this does all your I/O and serial and whatever else setup

  for (;smiley-wink
  {
    /*
      these functions should never wait for anything. If you have to wait for something, simply don't make it disable itself until that thing has happened. They'll be called tens of thousands of times per second, so don't worry too much about missing things.
      in fact, nothing in this loop should ever wait. don't even use delay(). If you want to delay, count a variable down every time your function is called, do something when it reaches zero. Alternatively, use the clock that the timers use, or even a whole timer.
      the less time it takes each function to sort itself out, the faster they'll be called. fast is good.
    */
    if (actions & (1<<forward_action)) forward();
    if (actions & (1<<backward_action)) backward();
    if (actions & (1<<stop_action)) stop();
    if (actions & (1<<rolleyes_action)) rolleyes();

    if (rfSerial.canread())  // critically important that we never sit and wait - the loop *must* keep looping or actions won't be able to stop themselves when they want to
    {
      switch (rfSerial.read())
      {
        case 'F':
          actions |= forward_action; break;
        case 'B':
          actions |= backward_action; break;
        case 'S':
          actions |= stop_action; break;
        case 'R':
          actions |= rolleyes_action; break;
      }
    }
  }
}


note that the above needs *lots* of filling in and adapting to your needs, also you can't have more than 16 actions unless you add another variable. It's purely a demonstration of what you're probably heading towards anyway.

Note that if you want sequences, all you have to do is make one action enable the next one before disabling itself

If you want to abstract this into a reusable code block, you'll want to define a struct act { uint8_t enabled; void (*func)(struct act *); }; then a struct act actarray[MAXACTIONS]; then initialise all the fuction pointers to 0 so you know which actions aren't in use. set enabled to 1 to start the action, set it to a larger nonzero number if you want timed stuff then decrement it within the action function itself. pass the function a pointer to it's struct action so it can change its own enable state. Then, just for (;smiley-wink { for (i=MAXACTIONS-1;i;i--) actarray.enabled && actarray.func(&actarray); enableActionsHere(); }
14  Forum 2005-2010 (read only) / Troubleshooting / Re: power supply problem on Arduino NG on: April 06, 2008, 09:00:12 pm
sounds like the ft232r is holding reset low. I think that's why they replaced the resistor from DTR to reset with a capacitor on the diecimila, and completely removed the one from RTS.
15  Forum 2005-2010 (read only) / Troubleshooting / Re: Solid Tx/Rx LEDs on: March 31, 2008, 06:53:37 pm
I've noticed that my tx/rx leds go solid on when bus powered from my switched off computer, which evidently holds the ft232r in reset.
Pages: [1] 2 3 ... 5