# Extracting numbers, to put in 3 array, from a string (Computer side code added)

I dont know where to start. Let's say i receave a string with 99 number in it and I want to extract them to put them in 3 arays. To be clear, my computer send line by line, one line at the time each pixel of a picture separater by ",". In such a fachion that it's

,r,g,b,r,g,b,r,g,b...

And I want all the red number to go in the Red aray, all the green number to got in the green aray and all the blue number to go in the blue aray and i want those aray to be byte arays, well i think.

And all the example that come with the arduino software seem to be un clear to me. And since i also have to do the protocole on the computer side, well my brain seem to be about to blow up.

Here is how I build my string, sorry it's in vb6, (but for some reason it does the job in 3/4 faster then the time Processing does the same...) ( And by the way " ' " mean " // " in vb) :

Dim R As Byte
Dim G As Byte
Dim B As Byte
Public Picheight As Integer
Public Sendstr(1200) As String   ' I over killed my aray size
Dim Pixel(1200, 1200) As Long  'I over killed my aray size

Function Color_to_RGB(Color As Long) As Long  'The function that transform the long pixel color number in byte

R = Color And &HFF&
G = (Color And &HFF00&) \ &H100&
B = (Color And &HFF0000) \ &H10000

End Function

Function transform_JPG_to_RGB()

For y = 1 To Picture2.Height / 15                 ' /15 cause each pixel has 15 twip and the heigh is in twip

For x = 1 To Picture2.Width / 15            ' /15 cause each pixel has 15 twip and the width is in twip

Pixel(x, y) = GetPixel(Picture2.hDC, x, y) 'Return a Long Number representing the color of that Pixel
Color_to_RGB (Pixel(x, y))                  ' Call the fonction discribed up there
Sendstr(y) = Sendstr(y) + "," + Str(R) + "," + Str(G) + "," + Str(B)  'partialy i build the string to send

Next

Sendstr(y) = Sendstr(y) + "."          'Where i finish the string sended to Arduino

Me.Caption = "Traitement de la ligne:" + Str(y + 1) + " en cours"  'Not important
DoEvents                              'This is to return the focus to the OS  so computer doesn't freeze

Next

End Function

Can any one be kind enogh to help?

And all the example that come with the arduino software seem to be un clear to me.

Which example? What is not clear?

strtok() is pretty easy to use, as is atoi().

Well the example Name StringToIntRGB.pde

Here what they do:

/*
Serial RGB controller

Reads a serial input string looking for three comma-separated
integers with a newline at the end. Values should be between
0 and 255. The sketch uses those values to set the color
of an RGB LED attached to pins 9 - 11.

The circuit:
* Common-anode RGB LED cathodes attached to pins 9 - 11
* LED anode connected to pin 13

To turn on any given channel, set the pin LOW.
To turn off, set the pin HIGH. The higher the analogWrite level,
the lower the brightness.

created 29 Nov 2010
by Tom Igoe

This example code is in the public domain.
*/

String inString = "";    // string to hold input
int currentColor = 0;
int red, green, blue = 0;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// send an intro:
Serial.println("\n\nString toInt() RGB:");
Serial.println();
// set LED cathode pins as outputs:
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
// turn on pin 13 to power the LEDs:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
}

void loop() {
int inChar;

if (Serial.available() > 0) {
}

if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}

// if you get a comma, convert to a number,
// set the appropriate color, and increment
// the color counter:
if (inChar == ',') {
// do something different for each value of currentColor:
switch (currentColor) {
case 0:    // 0 = red
red = inString.toInt();
// clear the string for new input:
inString = "";
break;
case 1:    // 1 = green:
green = inString.toInt();
// clear the string for new input:
inString = "";
break;
}
currentColor++;
}
// if you get a newline, you know you've got
// the last color, i.e. blue:
if (inChar == '\n') {
blue = inString.toInt();

// set the levels of the LED.
// subtract value from 255 because a higher
// analogWrite level means a dimmer LED, since
// you're raising the level on the anode:
analogWrite(11,  255 - red);
analogWrite(9, 255 - green);
analogWrite(10, 255 - blue);

// print the colors:
Serial.print("Red: ");
Serial.print(red);
Serial.print(", Green: ");
Serial.print(green);
Serial.print(", Blue: ");
Serial.println(blue);

// clear the string for new input:
inString = "";
// reset the color counter:
currentColor = 0;
}

}

I go true it, But my mind is so corupted but 1 day of VB programing that it mean nothing any more. lol

Read this: How to post code properly

Pete

Hey just I just upgraded to JrMember Horray!

But my mind is so corupted but 1 day of VB programing

That's exactly why I've never tried to learn VB. C++ is so much easier.

True Paul,

But for some reason i dont get, this job is done faster on vb6 then on Processing, I wanted to go with processing but it's very slow.

I got it mostly coded allready. but i gave up by disapointment.

For some reason vb6 aray up the whole picture in a finger click. Weird.

At first, i did it whit processing.

But in any case, the code on the computer side is working fine, i get the string just the right way in the arduino. The Cuhead socketapp return exactly what i want.

Thank's Pete, im going truw it and I already did some of the advice. Has you can see the code is now in his own window. Ill make it good looking dont worry.

Any way's ill probably find my awnser before any one show up to help me aroud with this little mess.

Ill just post it once i find it. So it can help some one else, as usual.

if (Serial.available() > 0) {
}

That reads a character if one is available and then proceeds even if one is not available!

Try replacing it with this:

while(Serial.available() < 1);

You will also run into problems eventually because it is a bad idea to use the String class.
But give my fix a try first and let’s see if the code parses the string properly.

Pete

Well your cool el_supremo, And since im doing this for FUN and not for MONEY(Cause my only goal is to make this little guy trip to see is picture on a 1,8" screen) I’ll share what i’ve done allready.

So here what i’ve done so far, It’s running the socketapp client for a CuHead wifi Shield the thing already connect on the local network and on my computer, then request it’s first line and my computer send it, all that is fine:

#include <WiShield.h>
#include <string.h>
extern "C" {
#include <uip.h>
}
#define WIRELESS_MODE_INFRA   1
#include <UTFT.h>
#include <avr/pgmspace.h>

UTFT myGLCD(ITDB18SP,6,5,2,3,4);   // Remember to change the model parameter to suit your display module!

unsigned char local_ip[] = {192,168,1,10};   // IP address of WiShield
unsigned char gateway_ip[] = {192,168,0,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};   // subnet mask for the local network
char ssid[]  = {"TheNetwork"};      // max 32 bytes
unsigned char security_type = 3;   // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
const prog_char security_passphrase[] PROGMEM = {"nceton123"};   // max 64 characters
int X
int Y
char R
char G
char B
prog_uchar wep_keys[] PROGMEM = { 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A,   // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00   // Key 3
};

unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;

// End of wireless configuration parameters ----------------------------------------

boolean connectToServer(void);

char            header[] = "POST /tutorial/showBrowser.jsp HTTP/1.0\r\nContent-Length: 10000\r\n\r\n";
unsigned int    xmitCount = 0;

boolean         connectedToServer = false;
boolean         inited = false;
unsigned long   nextXMitTime;
char        buffer[105];

long previousMillis = 0;
long interval1 = 20000;

void setup() {

//  Serial.begin(57600);
//  Serial.println("starting");
pinMode(9, INPUT_PULLUP);
connectedToServer = false;
WiFi.init();
myGLCD.InitLCD(PORTRAIT);

}

void loop() {

WiFi.run();

if(millis() - previousMillis > interval1) {

previousMillis = millis();

}

if (millis() > nextXMitTime && inited && !connectedToServer) {

//    Serial.print("Connecting to server... ");
xmitCount = 0;

if (connectToServer()) {

connectedToServer = true;
//      Serial.println("connection OK");

}
else {

//      Serial.println("connection NOT OK");

}

}

}

// Make the initial connection to the remote server at 192.168.0.2
boolean connectToServer(void){

struct uip_conn *conn;
return (conn != NULL);
}

extern "C" {

void socket_app_init() {

//    Serial.println("inited!!!");
inited = true;
nextXMitTime = millis();
}

// Called by the framework when there's an event which needs attention
// Here's where the work gets done.
void socket_app_appcall() {
// A connection was just made and it's OK to send data.
if (uip_connected()) {

//      Serial.println("{connected}");

}
// connection was broken.  Perhaps server went down or cable unplugged?  Retry immediately
if (uip_aborted()) {

//      Serial.println("{aborted}");
connectedToServer = false;

}
// connection timed out.  Not good but we can try again.  Retry immediately
if (uip_timedout()) {

//      Serial.println("{timedout}");
connectedToServer = false;

}

// connection was closed.  This is usually expected after the transaction is complete
// It seems we'll get this once with the UIP_NEWDATA flag set, and then again with the
// UIP_NEWDATA cleared.  We only want to advance 'nextXMitTime' once, so we ignore the
// UIP_CLOSED unless it has no new data
if(uip_closed() && !uip_newdata()) {

//      Serial.println("{closed}");
connectedToServer = false;
nextXMitTime += 30000L;        // XMit again in 30 seconds

}

// An error occurred and we need to retransmit.
if (uip_rexmit()) {

//      Serial.println("{retransmit}");
uip_send(buffer, strlen(buffer));

}

// The current packet is done being transmitted and now we're OK to transmit another packet.
if (uip_acked()) {

if (xmitCount < 20) {

//        Serial.print("xmit: ");
//        Serial.println(xmitCount, DEC);
// Build a string that is 100 lines long
// We can actually transmit a packet up to the size of 'uip_mss()', which is tyipcally 327 bytes.

sprintf(buffer, "Prêt à recevoir\r\n", xmitCount);
xmitCount += strlen(buffer);
uip_send(buffer, strlen(buffer));
}
else {        // send another linefeed to indicate the end of this POST data.

if (uip_connected()) {

uip_send("\r\n", 2);
}

}

}
// here's where new data arrives.  Do any string processing here.
if(uip_newdata()) {

String mydata =""
unsigned int len = uip_datalen();

myGLCD.setColor (R,G,B);
myGLCD.drawPixel(X,Y);

//      Serial.write((const uint8_t*)uip_appdata, len);
while (uip_appdata != '.') {

mydata += uip_appdata

}

for
}

}

}

But spécificaly this part is important:

// here's where new data arrives.  Do any string processing here.
if(uip_newdata()) {

String mydata =""
unsigned int len = uip_datalen();

// myGLCD.setColor (R,G,B);             //These to will go in a for {  }  once the 3 arrays will be build
// myGLCD.drawPixel(X,Y);               //These to will go in a for {  }  once the 3 arrays will be build

//      Serial.write((const uint8_t*)uip_appdata, len);
while (uip_appdata != '.') {

mydata += uip_appdata

}

for
}

}

The Data come well, Line by line and i allready know how I will ask for the next line, it’s already programe in my computer application. But that’s it for now it’s just a buntch of R,G,B,R,G,B, And i know the lise is over when i find a " . ". And althouht ill probably figure it out while im dreaming tonight. I would like to go the sleep with a peacefull mind lol

I could just send the whole crap to a SDCard and load it from ther with the demo given at Adafruit, done it and it work, but it would not but as fun.

I want to make some thing to work with people and talk about it. And it's not with a all made library that it will happen.

I was fead up with Sodoku and crossword puzzle so i bouth a Arduino. lol

Then Ill cut truw a crayon plastic box to stick out the screen then glue the box so the little guy cant open it and ill be able to upload picture and let him have a ball with is box.

Some will say that he is gonna play with expensive toy’s, well he will. lol

He as not broken my cell phone yet, he is already the kind that care for thing. So hey it will be a cool toy.