Show Posts
Pages: 1 ... 56 57 [58] 59 60 ... 105
856  Using Arduino / Programming Questions / Re: Servo control on: March 19, 2012, 12:15:18 pm
No, not natively. You could create a function to do that, though.

Hah!  May as well do it one line at a time then.  I'd probably end up with the same overhead.
857  Using Arduino / Programming Questions / Re: Servo control on: March 18, 2012, 06:23:42 pm
Normally, I'd have to go through and assign each variable, one by one:
Code:
data[] = {90, 2, 90, 2, 5};
angle_pan = data[0];
speed_pan = data[1];
angle_tilt = data[2];
speed_tilt = data[3];
pause = data[4];

Is there a way to something like this?  (this is PHP code code by the way)
Code:
$data = Array(90, 2, 90, 2, 5);
list($angle_pan, $speed_pan, $angle_tilt, $speed_tilt, $pause) = $data;

That assigns all the values to the various variables in one line.
858  Using Arduino / Programming Questions / Re: Servo control on: March 18, 2012, 04:28:42 pm
Ta-ahaaa, I forgot to increment index, you're right.  With index++ inside of the while loop, it works.  Now on to step umpteen ...
859  Using Arduino / Programming Questions / Re: Servo control on: March 18, 2012, 08:51:50 am
After fixing the buffer size ... (how'd I miss that) ... and adding the Serial.print/println lines, it looks like this:
Code:
void parseBuffer() {
  Serial.print("String to parse: [");
  Serial.print(buffer);
  Serial.println("]");
  if (strlen(buffer) > 0) {
    int vals[] = {0, 0, 0, 0, 0};
    byte index = 0;
    char *token = strtok(buffer, ",");
    while (token) {
      vals[index] = atoi(token);
      token = strtok(NULL, ",");
    }
    // print vals
    for (int i = 0; i < 5; i++) {
      Serial.print(vals[i]);
      Serial.print(" - ");
    }
    Serial.println();
  }
}

And the result is this:
Code:
Initializing SD card...
initialization done.
String to parse: [90,2,90,2,5]
5 - 0 - 0 - 0 - 0 -
String to parse: []
String to parse: [45,4,135,2,5]
5 - 0 - 0 - 0 - 0 -
String to parse: []
String to parse: [135,2,45,4,5]
5 - 0 - 0 - 0 - 0 -
String to parse: []
String to parse: [90,2,90,2,5]
5 - 0 - 0 - 0 - 0 -
String to parse: []

Done reading file.
860  Using Arduino / Programming Questions / Re: Servo control on: March 18, 2012, 01:50:51 am
Hrm, getting something odd here
Code:
#include <SD.h>

File dataFile;
int done = 0;
char c;
char buffer[4];
byte index;

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop() {
  dataFile = SD.open("data.txt", FILE_READ);
 
  if (!done) {
    if (dataFile) {
      index = 0;
      buffer[index] = '\0';
     
      while (dataFile.available()) {
        c = dataFile.read();
        if (c == '\n' || c == '\r') {
          parseBuffer();
          index = 0;
          buffer[index] = '\0';
        } else if (index < 39) {
          buffer[index++] = c;
          buffer[index] = '\0';
        }
      }
      done = 1;
      dataFile.close();
      Serial.println();
      Serial.println("Done reading file.");
    } else {
      Serial.println("error opening data.txt");
    }
  }
}

void parseBuffer() {
  if (strlen(buffer) > 0) {
    int vals[] = {0, 0, 0, 0, 0};
    byte index = 0;
    char *token = strtok(buffer, ",");
    while (token) {
      vals[index] = atoi(token);
      token = strtok(NULL, ",");
    }
    // print vals
    for (int i = 0; i < 5; i++) {
      Serial.print(vals[i]);
      Serial.print(" - ");
    }
    Serial.println();
  }
}

Data file contains:
Code:
90,2,90,2,5
45,4,135,4,2
135,4,45,4,2
90,2,90,2,5


The ouputs the following to the serial monitor:
Quote
Initializing SD card...
initialization done.
5 - 0 - 0 - 0 - 0 -
5 - 0 - 0 - 0 - 0 -
5 - 0 - 0 - 0 - 0 -
5 - 0 - 0 - 0 - 0 -

Done reading file.

On a side note, why can't I use 'i < sizeof(vals)' ?  When I try that, I get a different output:
Quote
Initializing SD card...
initialization done.
5 - 0 - 0 - 0 - 0 - 2268 - 0 - 1297 - -1534 - 397 -
5 - 0 - 0 - 0 - 0 - 2268 - 0 - 1297 - -1534 - 397 -
5 - 0 - 0 - 0 - 0 - 2268 - 0 - 1297 - -1534 - 397 -
5 - 0 - 0 - 0 - 0 - 2268 - 0 - 1297 - -1534 - 397 -

Done reading file.
861  Using Arduino / Programming Questions / Re: Servo control on: March 16, 2012, 01:39:15 pm
Could be either one. Depends on whether you want to read a whole record, then parse it, and convert each token to an int, or whether you want to read and parse in one step, as you are doing now.

Probably the first method, read till I get a new line, then parse the whole thing.  Unless you can think of a reason why I should be doing it one at a time.  In the end, I'd like to just pass all the arguments on to the servo function as one piece (at least, I think I can.)  So something like myServos(arg1, arg2, arg3, arg4).  The pause argument is being used by the main loop itself, not the servo function.
862  Using Arduino / Programming Questions / Re: Servo control on: March 16, 2012, 01:08:07 pm
A couple corrections. What you need are ints, not strings. Ditch the String class. You will run out of memory that way.

A fixed array of chars that you store c in (and increment an index and add a NULL terminator to) will, overall, use less memory. Each time you encounter a comma, call atoi() to convert the value to an int, and reset index (and put a NULL in the 0th position of the array).

While I understand what you're explaining to me, the whole process of converting from one to the other is elusive to me. The fixed array, would that be for the 5 elements I will eventually end up with, or is that for each character read to be stored in prior to converting to INT?
863  Using Arduino / LEDs and Multiplexing / Re: LEDs for a robot costume on: March 15, 2012, 10:21:32 pm
There are ready made SMD LED specifically for the LilyPad.  Just use those.

http://www.sparkfun.com/search/results?term=lilypad&what=products
864  Using Arduino / Programming Questions / Re: Servo control on: March 15, 2012, 06:08:31 pm
Agreed, testing is definitely needed.

While futzing around last night, learning the SD library and what not, I came up with this little snippet:
Code:
#include <SD.h>

File dataFile;
int done = 0;
char c;
String data;

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);
   
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
}

void loop() {
  dataFile = SD.open("data.txt", FILE_READ);
 
  if (!done) {
    if (dataFile) {
      while (dataFile.available()) {
        c = dataFile.read();
        switch(c) {
          case ',':
            Serial.print(data);
            Serial.print(" - ");
            data = "";
            break;
          case '\n':
            Serial.println(data);
            /*
              Process instructions here ...
              Break up into {angle_pan (INT), speed_pan (INT), angle_tilt (INT), speed_tilt (INT), pause (INT)}
            */
            Serial.println("new line detected!");
            data = "";
            break;
          default:
            data = data + String(c);
            break;
        }
      }
      done = 1;
      dataFile.close();
      Serial.println();
      Serial.println("Done reading file.");
    } else {
      Serial.println("error opening data.txt");
    }
  }
}

The data.txt file looks like this:
Code:
90,2,90,2,5
45,4,135,4,2
135,4,45,4,2
90,2,90,2,5

The sketch does what I'm expecting it to do, it prints out (I'm not at my workbench, so I'm writing from memory here)
Code:
90 - 2 - 90 - 5
new line detected!
45 - 4 - 135 - 4 - 2
new line detected!
etc., etc.

Now, I did that as an exercise, see if I could figure out the parsing and all of that.  Great.  However, what I need are INTs, not Strings.  Because I need to pass that data on to the Servo library.  And this is where I wish I had paid more attention, converting from one to the other.

If you notice, in my sketch I wrote myself a little comment of where I need to parse the full instruction line.  I don't know how to actually do that.  Ideally it's just an array with the various values in it which I will then pass on to the servo function to do its thing.

Some help will be much appreciated here.
865  Using Arduino / Project Guidance / Re: Arduino (clone) as HID on: March 15, 2012, 03:31:12 pm
One possible "run" would be something akin to this:

- device boots up and configures itself as an EthernetClient
- scans for a specific server on the network
- if found, requests a data file and downloads it to its onboard SD card
- if the server isn't found, reconfigure itself as a listening server and jump in it's normal loop
(the purpose of each one being a listening server is so I can poll them later and retrieve status information from each one, as opposed to having them constantly send stuff out.)

This way I don't have to be scanning for that server during a program run, but only when the thing boots up (or gets reset).  Very much like what a bootloader does, either go into programming mode, or run the sketch.
866  Using Arduino / Project Guidance / Re: Arduino (clone) as HID on: March 15, 2012, 03:10:50 pm
Hrm, that's specific to the Teensy.  I wonder if the same can be applied to a 328P, or whether I'll be forced to build a Teensy in the design with a second USB port.  Meh.

Something else popped in my head this morning though ... they say a man thinks best when on the throne ... maybe so.  This board design will also have one of either wired or wireless ethernet.  Technically it should be possible to have the device show up on the network, and be able to transmit a new data file for it to write on to its SD card, yes?  It should all come down to the coding, and how to tell it that you're about to send it a new data file that it should just put on it's onboard SDcard.  That same coding should also do its normal stuff, like running the sketch itself.

Thinking out loud, the sketch would be like any other sketch would be, tell the MCU what to do, from receiving data through the ethernet (whether wired or wireless), to controlling the servos.  With everything build as loop calls, it should be possible to add a section that says 'if you see this sequence of commands over ethernet, go in this mode' where this 'mode' would be to receive data that goes straight to the SD card (while halting everything else the sketch is doing.)  Or just have each unit poll a master unit on the network and if present, pull a file from it.

I'm sure I'm missing something here ...
867  Using Arduino / Project Guidance / Re: Arduino (clone) as HID on: March 14, 2012, 11:12:52 pm
I suppose another possible kludge would be to have a specific sketch which sole purpose is to write data (embedded in the sketch) onto the SD card.  Then load up the actual sketch that's going to drive everything.  But I hate having to go back and forth and ultimately that's not a good solution for the end-user (which won't always be me.)

Like I said, the SD card won't be easily accessible (it's inside of an enclosure), so taking it out to put in a card reader isn't an option without tearing open the enclosure each time.
868  Using Arduino / Programming Questions / Re: Servo control on: March 14, 2012, 10:59:34 pm
anyways..  maybe to get rid of the overhead of accessing the SD card..reading, parsing date.....etc..

maybe read in a 'few' line/variables at a time.. maybe the first 10 to give you a bit of a 'buffer' for smoother/continuous movement..?

Where would you be reading those 'few' lines/variables at a time from?  Thin air?  They're coming off of the SD card.

How many records will there be?

You can't alter how the SD read is done. It will read 512 byte chunks from the file, or less if there is not that much data. If all the values are byte sized, you could create a binary file, with 5 bytes per record (no cr/lf needed). That would allow for 102 records before another disk read is performed.

The proper thing to do,though is perform a timing test. See if the read will be fast enough.

How many records, not entirely sure yet.  It's a 6 minute show, and the servos will be doing "stuff" the whole time.  At the most, they'll be receiving a new instruction every second.  For the moment, the instruction that needs to get parsed is what I've shown above, 5 parameters per record.  Two of them will have a number between 0 and 180, two of them will have a number between 0 and 10, and the last one is 0 to 30 ... maybe larger, but no more than 3 digits.

Can I fit it all in 512 bytes?  Just a random set of instructions, sending both servos into sweep mode and changing every 2-5 seconds, and I can get about 35 instructions, one per line (cr/lf at the end), in a TEXT file.  I want to stay away from having to create a binary file.  The idea is that anyone can enter their instructions in an Excel file and export the sheet as a CSV file.

I'm going to need more than 35 instructions, that's for sure. smiley
869  Using Arduino / Project Guidance / Re: Arduino (clone) as HID on: March 14, 2012, 10:21:48 pm
This might be possible with a true Uno or 100% compatible Uno clone.  They work as HIDs.  But I think you may have to do some coding to get the SD card to appear as removable storage.  But seeing as how you can just put the SD card into most computers made since 2006, why would you want to do this?

Unos don't work as HID.  And in this instance, the SD Card isn't easily accessible to pull out every time it needs to have a new set of instructions downloaded on it.  So if I can have the device show up as a mass storage, that would be a better solution.
870  Using Arduino / Project Guidance / Arduino (clone) as HID on: March 14, 2012, 02:55:59 pm
Does anyone know if an Arduino (clone) with an SD card on board can be made to show up as an HID device on a computer, so the SD card shows up as a mass storage device?  Basically, plug the board in, and the SD card shows up as a mass storage, or one can use the Arduino IDE to actually program the controller.

Even if it means doing some manual work, such as removing the SD card prior to programming the board.  I'm thinking since one can use the card detect pin on a card holder, it should be easy to detect whether to have the device show up as a mass storage, or as a virtual comm port for programming.

Anyone ever do this, or know of a board that does this?
Pages: 1 ... 56 57 [58] 59 60 ... 105