Go Down

Topic: Manipulate String Array (Read 10711 times) previous topic - next topic

mlaser

Apr 27, 2009, 12:27 pm Last Edit: Apr 27, 2009, 12:36 pm by mlaser Reason: 1
G'day All,

I'm having issues with some data handling within my program.  Basically I can't figure out how to manipulate the strings in my array of strings.

I need to be able to have a string array with data already in them and search that data so that i can either replace the data or add to it.

Its for an RFID project i have to do for TAFE, it monitors a tool board and when a user removes tools it stores which tool that user has currently.  So when a user puts a tool back he/she may still have other tools attached to his/her name so i can't simply wipe the data from that user.  I need to be able to find and edit the data within the array.

eg.

char* user_tools[17]={"spanner8", "spanner10", "spanner12", "spanner14"}

say the user returns "spanner10"
i need the command to search user_tools for "spanner10" and then re-arrange the strings so that the data would look like

{"spanner8", "spanner12", "spanner14"}

any help greatly appreciated.

Cheers

Digger450

Check out the string library, should help you do what you need.

http://www.arduino.cc/en/Tutorial/TextString

mem

#2
Apr 27, 2009, 08:55 pm Last Edit: Apr 27, 2009, 08:59 pm by mem Reason: 1
I would take a differnt approach and fiddle with an index to the tools instead of the strings. Lots of ways to do this, here is one idea:

Code: [Select]

enum Tools { SPANNER8,SPANNER10,SPANNER12, SPANNER14,NumberOfTools}; // enum is just a sequential list of items starting from 0
char* user_tools[NumberOfTools] = {"spanner8", "spanner10", "spanner12", "spanner14"};

boolean usedTools[NumberOfTools];

void useTool(int tool )
{
   usedTools[tool] = true;
}

void returnTool(int tool )
{
   usedTools[tool] = false;    
}

// prints strings of used tools if argument is true, unused tools if argument is false
void printTools(boolean isUsed){
 for( int i=0; i <NumberOfTools; i++)
   if(usedTools[i] == isUsed)
     Serial.println(user_tools[i]);
}

//usage:
printTools(true); // print used tools
useTool(SPANNER8);
printTools(true); // print used tools
returnTool(SPANNER8);


Not run the code but I hope the idea helps

AlphaBeta

#3
Apr 27, 2009, 08:58 pm Last Edit: Apr 27, 2009, 09:07 pm by AlphaBeta Reason: 1
I've actually made a library for you, if you're interested.

This is an example of usage: http://arduino.pastebin.com/f3b6a0334
It will enable you to send command strings through serial, and the arduino will act upon them.

My library consists of three classes/structs


  • Tool - definition of a single tool
  • Toolkit - collection of tools
  • ToolUser - a user of a toolkit


If you send +Spanner10$ it will add a spanner10 to a user
If you send -Spanner10$ it will remove a spanner10 from a user


I imagine there are a few functions that you would like to add to the API.
Maybe a getNumberOfTools() for knowing how many tools a user has, or a has(tool); to find out if a user has a tool.

You are free to edit the source and do whatever you want with it. If you have questions, PM me or use the @contact from the example.


This is a Serial log from a testrun of the example sketch that comes with the library.
Code: [Select]
You can add or remove tools by usnig these commands:
     +SpannerXX$
     -SpannerXX$
     Where:
           + and - indicates the wanted operation (add / remove)
           XX could be either 8,10,12 or 14
           $ indicates command end
Write '+Spanner10$' in order to add spanner10
Write '-Spanner10$' in order to remove spanner10

Tools in toolkit:
     spanner8
     spanner10
     spanner12
     spanner14
mlaser has these tools:
     spanner8
     spanner12
     spanner14

//sent: +Spanner10$
//result:
mlaser has these tools:
     spanner8
     spanner12
     spanner14
     spanner10
//sent: -Spanner10$
//result:
mlaser has these tools:
     spanner8
     spanner12
     spanner14



[size=14]Donwload here[/size]

[edit][size=12]A quick example:[/size]
Quote


#include <Toolkit.h> //http://hosting.alexanderbrevig.com/arduino/libraries/Toolkit.zip

//initialize some tools
Tool spanner8 = Tool("spanner8");
Tool spanner10 = Tool("spanner10");
//prepare the toolkit
Toolkit toolkit;
//initialize a user
ToolUser mlaser = ToolUser("mlaser",toolkit);   //user name is mlaser, and this user can use tools from the Toolkit named toolkit

void setup() {
 Serial.begin(9600);
 //add tools to the toolkit
 toolkit.addTool(spanner8);
 toolkit.addTool(spanner10);
 
 //ask toolkit for spanner8 and spanner10
 mlaser.request(spanner8);
 mlaser.request(spanner10);
 
 mlaser.printTools();
 /* prints:
 mlaser has these tools:
     spanner8
     spanner10
 */
 
 //ask toolkit to recieve spanner8
 mlaser.replace(spanner8);
 
 mlaser.printTools();
 /* prints:
 mlaser has these tools:
     spanner10
 */
}

void loop(){
 
}
[/edit]

mlaser

man this forum is excellent.  Never had this many helpful replies in such a short time.  Thanks for all your input, now to decide how to approach it :)

mlaser

another quick question.

I have my RFID transmit hooked up to the RX pin on the arduino.  Is there something i can do so that i don't have to pull the RFID pin out of the RX arduino pin when i want to upload a new program?

Cheers

retrolefty

Quote
I have my RFID transmit hooked up to the RX pin on the arduino.  Is there something i can do so that i don't have to pull the RFID pin out of the RX arduino pin when i want to upload a new program?


Well you could try wiring a series 1K resistor, that is how the USB chip has it's send and rec lines wired to pins 0 & 1 on the Arduino board. As long as the RFID is not actively sending data during the upload it should be OK.

Lefty

nussbox

Hi AlphaBeta,

Could you post a fresh link to your Toolkit.zip. I'm looking to do something similar and it would be really helpful to have a look at it.

Cheers

Go Up