Go Down

Topic: serial.print problem (Read 1 time) previous topic - next topic

dontmarryme

Hi all, I am using 4 accelerometer to detect knock, tap dance and I m using max/msp to receive data from arduino through xbees.

pls some one can help me to fix this code to what I need?
I will try to explain my question clearer:

my code now is:

Code: [Select]
//this is the analog port of the accelerator axis (probably Z - check the board)
const unsigned char accelerator_input= 1;
const unsigned char accelerator_input2= 4;

const unsigned char output_value = 0;
const unsigned char output_value2 = 0;


//this is the avaeraging factor to smooth out the noise - be aware this must be below 50
//do not make it too big either - since you want to detect impulses!
const byte smooth_factor = 0;
const byte smooth_factor2 = 0;

//is a tap in positive (>) or negative (<) direction - to differentiate between tap and lifting your foot
#define TAP_CRITERIA > 750
#define TAP_CRITERIA2 > 750

//how big must the acceleration bee to detect a tap
const int tap_treshold = 750;
const int tap_treshold2 = 750;
//by what value it is scaled down?
const byte scale_down_factor = 2;
const byte scale_down_factor2 = 2;
//a rule of thumb - if you connect 3.3V to AREF:
//(1024-tap_treshold) / scale_factor must be smaller than 256

//this is used to store the average
int acceleration_value = 0;
int acceleration_value2 = 0;

void setup(){
 Serial.begin(9600);
 //this is to tell the Arduino to use AREF - connect 3.3V
 analogReference(EXTERNAL);
}



void loop(){
 //read the acceleration  
 int acceleration_readout = analogRead(accelerator_input);
 int acceleration_readout2 = analogRead(accelerator_input2);

 //smooth the readout
 acceleration_value = (acceleration_value*smooth_factor+acceleration_readout)/(smooth_factor+1);
 acceleration_value2 = (acceleration_value2*smooth_factor2+acceleration_readout2)/(smooth_factor2+1);

 //is it a tap or foot lifting?
 if (acceleration_value TAP_CRITERIA) {
   //ok, now we are interested in the absolute value
   unsigned int abs_acceleration = abs(acceleration_value);
   //is it bigger than our treshold?
   if (abs_acceleration>tap_treshold) {
     //then apply some math to get
     unsigned char output_value = (unsigned char) ((abs_acceleration-tap_treshold)/scale_down_factor);
     Serial.print("A");
     Serial.print(output_value);
     Serial.flush();
   }
   
 
 }
 if (acceleration_value2 TAP_CRITERIA2) {
   unsigned int abs_acceleration2 = abs(acceleration_value2);
   if (abs_acceleration2>tap_treshold2) {
     unsigned char output_value2 = (unsigned char) ((abs_acceleration2-tap_treshold2)/scale_down_factor2);
     Serial.print("C");
     Serial.print(output_value2);
     Serial.flush();
   }
   
 }
 
 
 }
 
     



now I need a better prefix way like
Code: [Select]

Serial.print("A");
Serial.print(output_value0);
Serial.print(output_value1);
Serial.print(output_value2);
Serial.print(output_value3);
Serial.print("B");



anyone knows how to do it under this situation?
cause I m using "unsigned char"

deSilva

Sorry, I do not really understand your question  :-/

AWOL

#2
Aug 29, 2010, 09:49 pm Last Edit: Aug 29, 2010, 09:50 pm by AWOL Reason: 1
This way
Code: [Select]
#define TAP_CRITERIA > 750
#define TAP_CRITERIA2 > 750

//how big must the acceleration bee to detect a tap
const int tap_treshold = 750;
const int tap_treshold2 = 750;

lie bugs.
(but otherwise, what deSilva said)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Coding Badly


Does this help...

Code: [Select]
Serial.print("A");
Serial.print(output_value0[glow],DEC[/glow]);
Serial.print(output_value1[glow],DEC[/glow]);
Serial.print(output_value2[glow],DEC[/glow]);
Serial.print(output_value3[glow],DEC[/glow]);
Serial.print("B");

dontmarryme

well, DEC is not helping..the reason is max/msp only accept byte form,
that's why I use unsigned char

sorry about my unclear question.

What I mean is:

now I put the Serial.print in the if loop
in that case I have to print twice:

Serial.print("A");
     Serial.print(output_value);
     Serial.flush();

and

Serial.print("C");
     Serial.print(output_value2);
     Serial.flush();


"but I don't want this, I want to print them all at the end together WITHOUT losing my if function"

so I can reduce the chance to mix them all...

hope this is clearer  :)

PaulS

Do you know why you are using Serial.flush? If you don't, stop using it.

If you do, then please explain why you need to flush the buffer when you never read from the buffer.

PaulS

This has nothing to do with your problem, but is a pet peeve of mine.

Code: [Select]
const byte scale_down_factor = 2;
const byte scale_down_factor2 = 2;

Do you really need to scale_down_factor variables? If so, name them scale_down_factor1 and scale_down_factor2, not scale_down_factor and scale_down_factor2.

Even better, when you have (or need) more than one of something, create an array and use indexes into that array.

dontmarryme

i think it refresh the buffer faster, doesn't it?

it was at the end of the loop scope but i move them to each if scope.

dontmarryme

about the scalre_down

I don't really need it but I write this code in a most secure way.
and how to use array for multiple analog reads?

PaulS

Finally, I'll take a stab at answering the question, though it is not at all clear what the question is.
Quote
now I put the Serial.print in the if loop
in that case I have to print twice:

Serial.print("A");
    Serial.print(output_value);
    Serial.flush();

and

Serial.print("C");
    Serial.print(output_value2);
    Serial.flush();


Do you want to be able to print "A" and output_value1 in a single statement? If that's what you want to do, you can use sprintf to create the combined array, and then print that array. Or, since output_value1 and "A" are the same type, create an array, and populate it yourself.

char stuffToSend[3];
stuffToSend[0] = "A";
stuffToSend[1] = output_value1;
stuffToSend[2] - '\0';
Serial.print(stuffToSend);

Quote
so I can reduce the chance to mix them all...

You have far more opportunities to improve the code than just combining the serial output into one operation.

Using arrays and functions and clear statements (if (acceleration_value TAP_CRITERIA) requires more study that necessary to understand that there isn't something missing) will prove to be more useful that reducing the Serial.print to a single statement.

dontmarryme

Quote

char stuffToSend[3];
stuffToSend[0] = "A";
stuffToSend[1] = output_value1;
stuffToSend[2] - '\0';
Serial.print(stuffToSend);



1. this is pretty useful, does it combine 3 data into a list(in max)?
2. what does line3 means? "stuffToSend[2] - '\0';"

thanks

PaulS

I don't know what you are doing with the data in Max, so I can't answer that question.

The Serial.print() function, when handed an array of characters sends ever character in the array to the serial port, until it encounters a NULL in the array. The '\0' is a NULL, so it terminates the sending of data to the serial port.

dontmarryme

compiling..

"invalid conversation from const char to char"

because this line

"stuffToSend[0] = "A";"

dontmarryme

what I wanna do in max is to use route
separate them according to the first char of each list
then send them to the right places.

I think your last answer is very close to what I need
but it doesn't work due to the format.

thanks :)

PaulS

Code: [Select]
stuffToSend[0] = "A";
Oops. That should be
Code: [Select]
stuffToSend[0] = 'A';

Go Up