send two data from sensor into serial communication.

dear all... i have a project using arduino deumilanove which read the data from loadcell and ping. as i read before, ping has output data as pwm and loadcell has output as analog. can i send both of data in the same time into 1 serial? i will use the data serial in visual basic. i haven't understand yet about arduino so i can't post any code here. big appreciate for your explanation and for the sample code if you dont mind :)

regard, amy

as i read before, ping has output data as pwm

No. The code to read a ping sensor uses pulseIn() to time how long it takes for the return pulse to arrive. The resulting value is an unsigned long.

and loadcell has output as analog

Which you read using analogRead(), which returns an int.

can i send both of data in the same time into 1 serial?

Yes, using two or more Serial.print() statements.

thank's PaulS, and what about the timing of reading both of the data? if I use different 'delay', i can't get the precision data from that two sensor in same time... i try this code one, which i've got from http://cerulean.dk/words/?page_id=42

/ Arduino as load cell amplifier
// by Christian Liljedahl 
// christian.liljedahl.dk

// Load cells are linear. So once you have established two data pairs, you can interpolate the rest.

// Step 1: Upload this sketch to your arduino board

// You need two loads of well know weight. In this example A = 10 kg. B = 30 kg
// Put on load A 
// read the analog value showing (this is analogvalA)
// put on load B
// read the analog value B

// Enter you own analog values here
float loadA = 0; // kg
int analogvalA = 22; // analog reading taken with load A on the load cell

float loadB = 20; // kg 
int analogvalB =176; // analog reading taken with load B on the load cell

// Upload the sketch again, and confirm, that the kilo-reading from the serial output now is correct, using your known loads

float analogValueAverage = 0;

// How often do we do readings?
long time = 0; // 
int timeBetweenReadings = 200; // We want a reading every 200 ms;
const int pingPin = 7;
   long duration, inches, cm;
void setup() {
  Serial.begin(9600);
}

void loop() {
  int analogValue = analogRead(0);
    float a,data[2]; 


   data[1]=cm * 1000;
  // running average - We smooth the readings a little bit
  analogValueAverage = 0.99*analogValueAverage + 0.01*analogValue;

  // Is it time to print? 
  if(millis() > time + timeBetweenReadings){
   data[0] = analogToLoad(analogValueAverage);

data[1]=(cm+1)*1000;
  a=data[1]+data[0];
  //  Serial.print("analogValue: ");Serial.println(analogValueAverage);
   // Serial.print("             load: ");
   Serial.println(a,1);
    time = millis();
  }
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
    pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
  delay(10);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

float analogToLoad(float analogval){

  // using a custom map-function, because the standard arduino map function only uses int
  float load = mapfloat(analogval, analogvalA, analogvalB, loadA, loadB);
  return load;
}

float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

and the output in serial monitor is unstable. it seem like the arduino is counting from the minimum data until the actual data, and take to much time. sorry it's hard to explain the condition, but that's about to happen. :blush:

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
    pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);

This has no place in loop. Put it in a separate function, and call it.

Your code is very hard to read seeing how it wanders all over the page. Use the Tools + Auto Format option to correct the random indenting.

// Add some comments // to explain what each section // of the code is supposed // to be doing

The relationship between load cell data and ping sensor completely escapes me.

I am really desperate to explain how the code was, because I did not much understand the Arduino. so, could you like to help me with sample code that I can use for this project?
in essence, I have 2 data: from the loadcell and ping, then I need to send data via serial.
thank you for your attention .

from the loadcell and ping,

Hum… no hardware schematic… no help here…

You design a code according to what hardware you have. So study the hardware, do a test code.

Please go to : http://arduino.cc/en/Reference/HomePage and click on the commands / instructions. So of them come with sample code to explain how it work. And go there … A lots of times. even you have to read it many times. If I want to learn some codes, I go there, study it and do a sample code to see what happen.

I have 2 data: from the loadcell and ping, then I need to send data via serial

You may need : pulseIn(), digitalRead(), analogRead(), <— input the data… Convert the data —> byte to int, int to byte, ect…
and learn about Serial.

In your IDE, some examples codes is already availables. Select FILE in your IDE and check for Examples.

That what I did when I got my Arduino. One step at a time.

@Amy - I did a quick and dirty re-write of your code (based on the assumption that your ping and load cell logic is correct). I removed the averaging from the load cell readings, and you can put that back yourself after you have verified that the code is working correctly.

I did the re-write not to hand you a completed solution for your project, but rather to show how program structure can either complicate or simplify coding. I don’t have your hardware to test with and so haven’t compiled or tested what I’m showing.

You will need to set your serial monitor to 115200 baud to test - there shouldn’t be any need to slow communication down to 9600 baud.

#define LOAD 0
#define PING 7
#define WAIT 200
/*----------------------------------------------------------------------------*/
/* Load cell calibration values                                               */
/*----------------------------------------------------------------------------*/
const float loadA = 0;        // kg
const float analogvalA = 22;  // analog reading taken with load A on load cell
const float loadB = 20;       // kg 
const float analogvalB = 176; // analog reading taken with load B on load cell
/*----------------------------------------------------------------------------*/
/* Program variables                                                          */
/*----------------------------------------------------------------------------*/
unsigned d;                // Distance in cm
float    m;                // Mass in kg
unsigned long cur,prv=0;   // Current, previous time (msec)
/*----------------------------------------------------------------------------*/
/* The speed of sound is 340 m/s or 29 microseconds per centimeter. The wave  */
/* goes and returns, so we take half the distance travelled. (32767 cm max)   */
/*----------------------------------------------------------------------------*/
unsigned dist(void)
{  pinMode(PING,OUTPUT);
   digitalWrite(PING,LOW);
   delayMicroseconds(2);
   digitalWrite(PING,HIGH);
   delayMicroseconds(5);
   digitalWrite(PING,LOW);
   pinMode(PING,INPUT);
   return (unsigned)pulseIn(PING,HIGH) / 58;
}
/*----------------------------------------------------------------------------*/
/* mapfloat() - Custom float map function                                     */
/*----------------------------------------------------------------------------*/
float mapfloat(float x,float in_min,float in_max,float out_min,float out_max)
{  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/*----------------------------------------------------------------------------*/
/* mass() - Return mapped load cell reading                                   */
/*----------------------------------------------------------------------------*/
float mass(void)
{  return mapfloat(analogRead(LOAD),analogvalA,analogvalB,loadA,loadB);
}
/*----------------------------------------------------------------------------*/
/* setup() - Arduino initialization                                           */
/*----------------------------------------------------------------------------*/
void setup(void)
{  Serial.begin(115200);
   prv = millis();
}
/*----------------------------------------------------------------------------*/
/* loop() - Arduino cycle mechanism                                           */
/*----------------------------------------------------------------------------*/
void loop(void)
{  cur = millis();
   if (cur - prv >= WAIT)
   {  prv += WAIT;
      d = dist();
      m = mass();
      Serial.print("m=");
      Serial.print(m);
      Serial.print("kg, d=");
      Serial.print(d);
      Serial.println("cm");
   }
 }
/*----------------------------------------------------------------------------*/

thanks PaulS, Techone and Morris Dovey.
i just need to learn more and more.
:slight_smile:
your replies are help full, although it didn’t give me the point :stuck_out_tongue:

thank’s a lot XD XD