serial.print problem

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:

//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

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”

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

This way

#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)

Does this help...

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");

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 :)

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.

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

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.

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.

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?

Finally, I’ll take a stab at answering the question, though it is not at all clear what the question 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();

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);

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.

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

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.

compiling..

"invalid conversation from const char to char"

because this line

"stuffToSend[0] = "A";"

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 :)

stuffToSend[0] = "A";

Oops. That should be

stuffToSend[0] = 'A';

it works... partly. now they are one list in max but the "output_value" is ascii now, no number, why is that? I am almost there. thanks

can u print ascii and unsigned char in one list..? guess not.. I think that's the problem.

but the "output_value" is ascii now, no number, why is that?

What does this mean? output_value contained a value. That value was sized to fit in an 8 bit variable. That value was copied into another array of 8 bit values, and sent out the serial port. The value was never changed.

Give an example of what you stored in output_value, and what Max received.