Hello there. My Arduino project has several components such as the ultrasonic sensor and rain sensor. I would want the data of the output to appear at the serial monitor together. However, it is only printing the ultrasonic sensor's data. But the rain sensor alone works fine.
Is there any way to print both data at the same time at the serial monitor? Or that I have to open multiple serial monitors?
Please post your sketch using code tags when you do
It sounds like parts of your code may be blocking the execution of other sections otherwise there is no reason why you should not print two sets of data at the same time
Yes but nobody nows what you're doing. As said, post your code.
You can't unless your board has multiple serial ports (e.g. Mega) and you connect them all to your computer. Only one application (on a computer) at a time can use the serial port.
#include <TinyGPS++.h>
#include <TinyGPSPlus.h>
#define echoPin 6 // attach pin D6 Arduino to pin Echo of HC-SR04
#define trigPin 7 //attach pin D7 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
//gps
#include "SoftwareSerial.h"
SoftwareSerial serial_connection(10, 11); //RX=pin 10, TX=pin 11
TinyGPSPlus gps;
void setup() {
Serial.begin(9600);
serial_connection.begin(9600);//
Serial.println("GPS Start");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.println("Ultrasonic Sensor HC-SR04 Test");
pinMode(8, OUTPUT);
pinMode(4, OUTPUT);
}
void loop() {
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance > 0){
if (distance < 23){
tone (8, 294, 250);
delay (325);
tone (8, 294, 250);
delay (325);
}
}
while(serial_connection.available())//While there are characters to come from the GPS
{
gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
}
if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
{
//Get the latest info from the gps object which it derived from the data sent by the GPS unit
Serial.println("Satellite Count:");
Serial.println(gps.satellites.value());
Serial.println("Latitude:");
Serial.println(gps.location.lat(), 6);
Serial.println("Longitude:");
Serial.println(gps.location.lng(), 6);
Serial.println("Speed MPH:");
Serial.println(gps.speed.mph());
Serial.println("Altitude Feet:");
Serial.println(gps.altitude.feet());
Serial.println("");
int value = analogRead(A3);//read value
Serial.print("Value : ");
Serial.println(value);
if (value < 300) {//check condition
digitalWrite(4, HIGH);
Serial.print("Heavy rain LED on ");
} else {
digitalWrite(4, LOW);
}
}
}
Please post an example of the output. Turn on timestamps in the Serial monitor and copy/paste the output here in code tags. Please do not post a picture of the Serial monitor
That means that if (gps.location.isUpdated()) never evaluateds to true. Hence you don't see GPS info (and your rain info).
Because you also have placed the below inside that same if statement, you will also never see that info.
int value = analogRead(A3); //read value
Serial.print("Value : ");
Serial.println(value);
if (value < 300) { //check condition
digitalWrite(4, HIGH);
Serial.print("Heavy rain LED on ");
} else {
digitalWrite(4, LOW);
}
As the analogRead has nothing to do with the gps stuff, I suggest that you take it out of if (gps.location.isUpdated()).