PDA

View Full Version : Arduino Lib Help!.



brian25
09-03-2013, 07:31 AM
how to put a value of int or byte in a string


SendSMS(byte sim_phonebook_position, char *message_str)


i want to read the pins and ouput like this 10111(1 if high and 0 if low)
but this part (char *message_str) only accept string.

KurtEck
09-03-2013, 08:32 AM
For quick and dirty I would say use something like sprintf to create a string to pass to the SendSMS, but I don't think there is a output binary conversion. There is hex (%x), Decimal (%d), Octal(%o)...

But you can also do your own thing like:


#define NUM_PINS 8
static const byte aPins[] = {3, 4, 5, 6, 7, 8, 9, 10}; // note do whatever pins you want...
char szOutput[NUM_PINS+1];
int i;
for (i=0; i<NUM_PINS; i++) {
szOutput[i] = digitalRead(aPins[i]) ? '1' : '0';
}
szOutput[NUM_PINS] = \0;

Not saying this is great, but could get the job done. Also typed on fly so could have issues.

Kurt

jwatte
09-03-2013, 10:37 AM
Kurt's solution is how I would do it, if I had that particular data formatting need.

brian25
09-04-2013, 01:08 AM
tnx kurt..

btw i named my pins like this

const int L1 = 8;
const int L2 = 9;
const int L3 = 10;

can this be ovewrite with the code?

static const byte aPins[] = {8,9,10};


here's my latest codes.

void Status(){
char stringBuffer[6];
char szOutput[NUM_PINS+1];
int i;
for (i=0; i<NUM_PINS; i++) {
szOutput[i] = digitalRead(aPins[i]) ? '1' : '0';
sprintf (szOutput,"%s", i);
}
SendSMS(1, szOutput)

}

i'm new in programming i hope you can help me with this..:sad:

KurtEck
09-04-2013, 08:40 AM
Again I am just doing stuff on the fly, so no promises that things will compile without a few tweaks here and there...

Note: Code is easier to read when you put them in code blocks. (Use the # button in the advanced editing)



#define L1 8
#define L2 = 9
#define L3 = 10

static const byte aPins[] = {L1,L2,L3};
#define NUM_PINS (sizeof(aPins)/sizeof(aPins[0])) // Count size of complete / size of element

void Status(){
char szOutput[NUM_PINS+1];
int i;
for (i=0; i<NUM_PINS; i++) { // Loop through all of the pins
szOutput[i] = digitalRead(aPins[i]) ? '1' : '0'; // Set output to status of pin
}
szOutput[NUM_PINS] = 0; // You need to terminate the string
SendSMS(1, szOutput)

}

Again we all new to programming at one point.

Good Luck.
Kurt

brian25
09-04-2013, 09:25 AM
thank you kurt, you save my day:veryhappy:

God Bless