0

Why are there two Serial.begin() function calls? What does that signify? Here is the minimal code i am working on:

void setup() {
      Serial.begin(115200);
      Serial.begin(9600);
      Serial.println("Pressure Sensor Test"); Serial.println("");    
    }

Here is the full Code:

#include <SimpleDHT.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>

int pinDHT11 = 2;
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);


void displaySensorDetails(void)
{
  sensor_t sensor;
  bmp.getSensor(&sensor);
  Serial.println("------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" hPa");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" hPa");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" hPa");  
  Serial.println("----------SDNx real time scientific data---------------");
  Serial.println("");
  delay(500);
}

void setup() {
  Serial.begin(115200);
  Serial.begin(9600);
  Serial.println("Pressure Sensor Test"); Serial.println("");
  if(!bmp.begin())
  {
    /* There was a problem detecting the BMP085 ... check your connections */
    Serial.print("  Ooops, no BMP085 detected ... Check your circuit some errors are there!");
    while(1);
  }

  /* Display some basic information on this sensor */
  displaySensorDetails();

}

void loop() {

  Serial.println("===========");
  Serial.println("Sample DHT11...");


  byte temperature = 0;
  byte humidity = 0;
  byte data[40] = {0};
  if (simple_dht11_read(pinDHT11, &temperature, &humidity, data)) {
    Serial.print("Read DHT11 failed");
    return;
  }

  Serial.print("Sample RAW Bits: ");
  simple_dht11_serial_print(data);

  Serial.print("SDNx says you are genius and every thing is OK: ");
  Serial.print((int)temperature); Serial.print(" *C, ");
  Serial.print((int)humidity); Serial.println(" %");

  // DHT11 sampling rate is 1HZ.
  delay(1000);

  sensors_event_t event;
  bmp.getEvent(&event);

  if (event.pressure)
  {

    Serial.print("Pressure:    ");
    Serial.print(event.pressure);
    Serial.println(" hPa");


    float temperature;
    bmp.getTemperature(&temperature);
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" C");


    float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
    Serial.print("Altitude:    "); 
    Serial.print(bmp.pressureToAltitude(seaLevelPressure,
                                        event.pressure)); 
    Serial.println(" m");
    Serial.println("");
  }
  else
  {
    Serial.println("Sensor error");
  }
  delay(1000);
}
  • 2
    It signifies that the author of this code has made a mistake of either implementation or understanding. – Chris Stratton Jan 17 '18 at 4:52
  • The first begin() is probably being ignored by the Arduino. – Fusseldieb Jan 18 '18 at 9:43
  • I thought it would be enacted upon and then overwritten by the second. I know that's the same result, but there might be some subtle difference. – Code Gorilla Jan 18 '18 at 14:22
3

It looks like a mistake, as Chris says. I can't tell you what will happen but my guess is it will work at 9600.

I would recommend that you delete one of the lines to avoid further confusion.

  • when i asked the author of this code he said that they were for maximum and minimum value of baud rate. Is there anything like that? PS. Also the arduino was connected to BMP085, DHT11 and a HC06 – rimalonfire Jan 17 '18 at 13:32
  • 1
    I think he misunderstands the calls he has made, they aren't. Serial only refers to the serial communications over pin 0 and 1 on an Arduino. You can have other objects that refer to other serial ports, but in this case its the serial monitor. – Code Gorilla Jan 17 '18 at 13:56
  • Also the output was taken from a Bluetooth terminal in an Android app – rimalonfire Jan 17 '18 at 14:11
  • What do you mean by "You can have other objects that refer to other serial ports, but in this case its the serial monitor" – rimalonfire Jan 18 '18 at 13:38
  • On a Mega you have Serial1 and 2(?). You can create SoftwareSerial objects which create a serial connection out of any pins. You could even define you own and bitbang a serial port. Heck you could even have an object that create a virtual serial port over Ethernet, Wifi, Bluetooth, etc, which is what I think you are using. – Code Gorilla Jan 18 '18 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.