Displaying of 2 different sensor outputs together in one serial monitor

Hi guys,

I am new to Arduino and I need some help with the mini project I am doing.

I have an ultrasonic sensor(SFR ranger) & a flex sensor.
I am able to get the out put of the both sensors individually now( one at a time). But I cannot get both sensor to work together and display the data in one serial monitor as I wanted.
Please advise me on how to get it be done

Below are the individual sensor codes that I am using now

SFR ranger codes:

const int trigPin = 2;
const int echoPin = 4;
int count=0;

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;

// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(cm);
Serial.print(“cm”);
Serial.println();
if (cm <=4)
{
count++;
Serial.print("COUNT: ");
Serial.println(count);
Serial.println();
}
delay(500);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

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

Flex sensor code

const int FLEX_PIN = A0; // Pin connected to voltage divider output

// Measure the voltage at 5V and the actual resistance of your
// 10k resistor, and enter them below:
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 10500.0; // Measured resistance of 3.3k resistor

// Upload the code, then try to adjust these values to more
// accurately calculate bend degree.
const float STRAIGHT_RESISTANCE = 9800.0; // resistance when straight
const float BEND_RESISTANCE = 20000.0; // resistance at 90 deg

void setup()
{
Serial.begin(9600);
pinMode(FLEX_PIN, INPUT);
}

void loop()
{
// Read the ADC, and calculate voltage and resistance from it
int flexADC = analogRead(FLEX_PIN);
float flexV = flexADC * VCC / 1023.0;
float flexR = R_DIV * (VCC / flexV - 1.0);
Serial.println(“Resistance: " + String(flexR) + " ohms”);

// Use the calculated resistance to estimate the sensor’s
// bend angle:
float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
0, 90.0);
Serial.println(“Bend: " + String(angle) + " degrees”);
Serial.println();

delay(500);
}

I needed it quite urgently and really appreciate the help
Thanks guys``

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

https://arduino-info.wikispaces.com/CombiningArduinoSketches

groundFungus:
Merging Code

https://arduino-info.wikispaces.com/CombiningArduinoSketches

Thanks alot, I will look into the resources,
But it will be hard for me ...haha

Give it a try and if you have trouble post the attempt and tell us what is wrong. Post the complete error messages if there are any. Please read the "how to use the forum" stickies to see how to format and post code and error messages.

Hi I have tried , but showing some error like below

Arduino: 1.6.12 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Sree\Desktop\final_project_ardunio_combine\final_project_ardunio_combine.ino: In function 'void loop()':

final_project_ardunio_combine:62: error: 'microsecondsToCentimeters' was not declared in this scope

   cm = microsecondsToCentimeters(duration);

                                          ^

final_project_ardunio_combine:81: error: expected '}' at end of input

 }//--(end main loop )---

 ^

exit status 1
'microsecondsToCentimeters' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I defined it in the code as below, is there something wrong?

//Pin definition
//flex sensor
const int FLEX_PIN = A0; // Pin connected to voltage divider output
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 10500.0; // Measured resistance of 10k resistor
const float STRAIGHT_RESISTANCE = 9800.0; // resistance when straight
const float BEND_RESISTANCE = 20000.0; // resistance at 90 deg
//ultrasonic sensor
const int Ultra_trigPin = 2;//utlrtasonic sensor pin1
const int Ultra_echoPin = 4;// ultrasonic sensor pin2
int count=0;



void setup()   /****** SETUP: RUNS ONCE ******/
 {
  // initialize serial communication:

  
    pinMode(FLEX_PIN, INPUT);
    pinMode(Ultra_trigPin, INPUT);
    pinMode(Ultra_echoPin, INPUT);
    Serial.begin(9600);
     delay(500);

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
// Read the ADC, and calculate voltage and resistance from it
  int flexADC = analogRead(FLEX_PIN);
  float flexV = flexADC * VCC / 1023.0;
  float flexR = R_DIV * (VCC / flexV - 1.0);
  Serial.println("Resistance: " + String(flexR) + " ohms");

  // Use the calculated resistance to estimate the sensor's
  // bend angle:
  float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
                   0, 90.0);
// establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
 long duration, inches, cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  pinMode(Ultra_trigPin, OUTPUT);
  digitalWrite(Ultra_trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(Ultra_trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(Ultra_trigPin, LOW);
   

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(Ultra_echoPin, INPUT);
  duration = pulseIn(Ultra_echoPin, HIGH);

   // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
                   
  Serial.println("Bend: " + String(angle) + " degrees");
  Serial.println();
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  if (cm <=4)
  {
    count++;
    Serial.print("COUNT: ");
    Serial.println(count);
    Serial.println();




}//--(end main loop )---

You are missing the microsecondsToCentimeters() function and a curly bracket to close loop().

Add this to the bottom of your sketch. This has the missing curly bracket and the missing function.

}//--(end main loop )---


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

Thanks a lot, Mr. groundFungus
It works now I am able to capture both sensor data in one serial monitor.
But the delay even I set to 1000 in the codes, it seems to be very fast ( I mean I tried different delay timings, but it does not seems to be changing)

What can I do about it?

//Pin definition
//flex sensor
const int FLEX_PIN = A0; // Pin connected to voltage divider output
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 10500.0; // Measured resistance of 10k resistor
const float STRAIGHT_RESISTANCE = 9800.0; // resistance when straight
const float BEND_RESISTANCE = 20000.0; // resistance at 90 deg
//ultrasonic sensor
const int Ultra_trigPin = 2;//utlrtasonic sensor pin1
const int Ultra_echoPin = 4;// ultrasonic sensor pin2
int count=0;



void setup()   /****** SETUP: RUNS ONCE ******/
 {
  // initialize serial communication:

  
    pinMode(FLEX_PIN, INPUT);
    pinMode(Ultra_trigPin, INPUT);
    pinMode(Ultra_echoPin, INPUT);
    Serial.begin(9600);
     delay(1000);

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
// Read the ADC, and calculate voltage and resistance from it
  int flexADC = analogRead(FLEX_PIN);
  float flexV = flexADC * VCC / 1023.0;
  float flexR = R_DIV * (VCC / flexV - 1.0);
  Serial.println("Resistance: " + String(flexR) + " ohms");

  // Use the calculated resistance to estimate the sensor's
  // bend angle:
  float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
                   0, 90.0);
// establish variables for duration of the ping, 
  // and the distance result in  centimeters:
 long duration,cm;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse
  pinMode(Ultra_trigPin, OUTPUT);
  digitalWrite(Ultra_trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(Ultra_trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(Ultra_trigPin, LOW);
   

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(Ultra_echoPin, INPUT);
  duration = pulseIn(Ultra_echoPin, HIGH);

   // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  
                   
  Serial.println("Bend: " + String(angle) + " degrees");
  Serial.println();
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  if (cm <=4)
  {
    count++;
    Serial.print("COUNT: ");
    Serial.println(count);
    Serial.println();
    delay(1000);


}//--(end main loop )---



}//--(end main loop )---


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;
}
if (cm <= 4)
  {
    count++;
    Serial.print("COUNT: ");
    Serial.println(count);
    Serial.println();
    delay(1000);
  }

The delay only happens when cm <= 4. Move the delay outside of the if block. Even better, look at the several things at a time thread and the blink without delay example (in the IDE, File, Examples, Digital) to use millis() or micros() for timing.

And I notice that you are setting pinModes in loop(). There is no reason to do that. Set the trigger pinMode to OUTPUT in setup() and remove the pinModes from loop().

got it :slight_smile: :slight_smile: :slight_smile:

Thanks a lot Mr. groundFungus, really appreciate your quick replies.

Your code using the blink without delay concept (no delay() and pinModes fixed).

//Pin definition
//flex sensor
const int FLEX_PIN = A0; // Pin connected to voltage divider output
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 10500.0; // Measured resistance of 10k resistor
const float STRAIGHT_RESISTANCE = 9800.0; // resistance when straight
const float BEND_RESISTANCE = 20000.0; // resistance at 90 deg
//ultrasonic sensor
const int Ultra_trigPin = 2;//utlrtasonic sensor pin1
const int Ultra_echoPin = 4;// ultrasonic sensor pin2
int count = 0;



void setup()   /****** SETUP: RUNS ONCE ******/
{
  // initialize serial communication:


  pinMode(FLEX_PIN, INPUT);
  pinMode(Ultra_trigPin, OUTPUT);
  pinMode(Ultra_echoPin, INPUT);
  Serial.begin(9600);
  delay(1000);

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{

  static unsigned long timer = 0;
  unsigned long interval = 1000; 
  if (millis() - timer >= interval) // if interval is up execute
  {
    timer = millis();  // reset timer

    // Read the ADC, and calculate voltage and resistance from it
    int flexADC = analogRead(FLEX_PIN);
    float flexV = flexADC * VCC / 1023.0;
    float flexR = R_DIV * (VCC / flexV - 1.0);
    Serial.println("Resistance: " + String(flexR) + " ohms");

    // Use the calculated resistance to estimate the sensor's
    // bend angle:
    float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
                      0, 90.0);
    // establish variables for duration of the ping,
    // and the distance result in  centimeters:
    long duration, cm;
    // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse
    digitalWrite(Ultra_trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(Ultra_trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(Ultra_trigPin, LOW);

    // Read the signal from the sensor: a HIGH pulse whose
    // duration is the time (in microseconds) from the sending
    // of the ping to the reception of its echo off of an object.
    duration = pulseIn(Ultra_echoPin, HIGH);

    // convert the time into a distance
    cm = microsecondsToCentimeters(duration);

    Serial.println("Bend: " + String(angle) + " degrees");
    Serial.println();

    Serial.print(cm);
    Serial.print("cm");
    Serial.println();
    if (cm <= 4)
    {
      count++;
      Serial.print("COUNT: ");
      Serial.println(count);
      Serial.println();
      //delay(1000);
    }
  }
}//--(end main loop )---


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

groundFungus:
Your code using the blink without delay concept (no delay() and pinModes fixed).

//Pin definition

//flex sensor
const int FLEX_PIN = A0; // Pin connected to voltage divider output
const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 10500.0; // Measured resistance of 10k resistor
const float STRAIGHT_RESISTANCE = 9800.0; // resistance when straight
const float BEND_RESISTANCE = 20000.0; // resistance at 90 deg
//ultrasonic sensor
const int Ultra_trigPin = 2;//utlrtasonic sensor pin1
const int Ultra_echoPin = 4;// ultrasonic sensor pin2
int count = 0;

void setup()  /****** SETUP: RUNS ONCE ******/
{
  // initialize serial communication:

pinMode(FLEX_PIN, INPUT);
  pinMode(Ultra_trigPin, OUTPUT);
  pinMode(Ultra_echoPin, INPUT);
  Serial.begin(9600);
  delay(1000);

}//–(end setup )—

void loop()  /****** LOOP: RUNS CONSTANTLY ******/
{

static unsigned long timer = 0;
  unsigned long interval = 1000;
  if (millis() - timer >= interval) // if interval is up execute
  {
    timer = millis();  // reset timer

// Read the ADC, and calculate voltage and resistance from it
    int flexADC = analogRead(FLEX_PIN);
    float flexV = flexADC * VCC / 1023.0;
    float flexR = R_DIV * (VCC / flexV - 1.0);
    Serial.println(“Resistance: " + String(flexR) + " ohms”);

// Use the calculated resistance to estimate the sensor’s
    // bend angle:
    float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
                      0, 90.0);
    // establish variables for duration of the ping,
    // and the distance result in  centimeters:
    long duration, cm;
    // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
    // Give a short LOW pulse beforehand to ensure a clean HIGH pulse
    digitalWrite(Ultra_trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(Ultra_trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(Ultra_trigPin, LOW);

// Read the signal from the sensor: a HIGH pulse whose
    // duration is the time (in microseconds) from the sending
    // of the ping to the reception of its echo off of an object.
    duration = pulseIn(Ultra_echoPin, HIGH);

// convert the time into a distance
    cm = microsecondsToCentimeters(duration);

Serial.println(“Bend: " + String(angle) + " degrees”);
    Serial.println();

Serial.print(cm);
    Serial.print(“cm”);
    Serial.println();
    if (cm <= 4)
    {
      count++;
      Serial.print("COUNT: ");
      Serial.println(count);
      Serial.println();
      //delay(1000);
    }
  }
}//–(end main loop )—

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

Yeah, it works well now, thanks again for your help:)