Go Down

Topic: [SOLVED]Serial monitor println suddenly change position  (Read 396 times) previous topic - next topic

ronaldogm6

hi i want combine two same sensor which is dust sensor GP2Y1014AU0F . So my problem is sometimes the dust density wont come and suddenly the println become horizontal, and iam confused about this .
and this is my program

/////////////////////////////////////////////////////////////////////////////
// Sharp GP2Y1014AU0F Dust Sensor Demo
//
// Board Connection:
//   GP2Y1014    Arduino
//   V-LED       Between R1 and C1
//   LED-GND     C1 and GND
//   LED         Pin 7
//   S-GND       GND
//   Vo          A5
//   Vcc         5V
//
// Serial monitor setting:
//   9600 baud
/////////////////////////////////////////////////////////////////////////////

// Choose program options.
//#define PRINT_RAW_DATA
#define USE_AVG

// Arduino pin numbers.
const int sharpLEDPin = 9;   // Arduino digital pin 7 connect to sensor LED.
const int sharpVoPin = A1;   // Arduino analog pin 5 connect to sensor Vo.

// For averaging last N raw voltage readings.
#ifdef USE_AVG
#define N 100
static unsigned long VoRawTotal = 0;
static int VoRawCount = 0;
#endif // USE_AVG

// Set the typical output voltage in Volts when there is zero dust.
static float Voc = 0.6;

// Use the typical sensitivity in units of V per 100ug/m3.
const float K = 0.5;
 
/////////////////////////////////////////////////////////////////////////////

// Helper functions to print a data value to the serial monitor.
void printValue(String text, unsigned int value, bool isLast = false) {
  Serial.print(text);
  Serial.print("=");
  Serial.print(value);
  if (!isLast) {
    Serial.print(", ");
  }
}
void printFValue(String text, float value, String units, bool isLast = false) {
  Serial.print(text);
  Serial.print("=");
  Serial.print(value);
  Serial.print(units);
  if (!isLast) {
    Serial.print(", ");
  }
}

/////////////////////////////////////////////////////////////////////////////

// Arduino setup function.
void setup() {
  // Set LED pin for output.
  pinMode(sharpLEDPin, OUTPUT);
 
  // Start the hardware serial port for the serial monitor.
  Serial.begin(9600);
 
  // Wait two seconds for startup.
  delay(2000);
  Serial.println("");
  Serial.println("GP2Y1014AU0F Demo");
  Serial.println("=================");
}

// Arduino main loop.
void loop() { 
  // Turn on the dust sensor LED by setting digital pin LOW.
  digitalWrite(sharpLEDPin, LOW);

  // Wait 0.28ms before taking a reading of the output voltage as per spec.
  delayMicroseconds(280);

  // Record the output voltage. This operation takes around 100 microseconds.
  int VoRaw = analogRead(sharpVoPin);
 
  // Turn the dust sensor LED off by setting digital pin HIGH.
  digitalWrite(sharpLEDPin, HIGH);

  // Wait for remainder of the 10ms cycle = 10000 - 280 - 100 microseconds.
  delayMicroseconds(9620);
 
  // Print raw voltage value (number from 0 to 1023).
  #ifdef PRINT_RAW_DATA
  printValue("VoRaw", VoRaw, true);
  Serial.println("");
  #endif // PRINT_RAW_DATA
 
  // Use averaging if needed.
  float Vo = VoRaw;
  #ifdef USE_AVG
  VoRawTotal += VoRaw;
  VoRawCount++;
  if ( VoRawCount >= N ) {
    Vo = 1.0 * VoRawTotal / N;
    VoRawCount = 0;
    VoRawTotal = 0;
  } else {
    return;
  }
  #endif // USE_AVG

  // Compute the output voltage in Volts.
  Vo = Vo / 1024.0 * 5.0;
  printFValue("Vo", Vo*1000.0, "mV");

  // Convert to Dust Density in units of ug/m3.
  float dV = Vo - Voc;
  if ( dV < 0 ) {
    dV = 0;
    Voc = Vo;
  }
  float dustDensity = dV / K * 100.0;
  printFValue("DustDensity", dustDensity, "ug/m3", true);
  Serial.println("");
 
} // END PROGRAM


i will attach the serial monitor display.

pert

I don't see anything in your code that would print the "Vo2= part of what's shown in the screenshot. Are you sure you posted the code that's actually running on your Arduino?

TomGeorge

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What is this bit of code supposed to do?
Code: [Select]
 } else {
    return;
  }


https://www.arduino.cc/reference/en/language/structure/control-structure/else/

Thanks..Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

ronaldogm6

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What is this bit of code supposed to do?
Code: [Select]
  } else {
    return;
  }


https://www.arduino.cc/reference/en/language/structure/control-structure/else/

Thanks..Tom... :)
Hi Tom, thank you for reminding the rules. Iam new at this forum, iam apologize about that. That was loop for the voltage, do you think is it not necessary ? iam trying copy it from github. Can i erase it ?

ronaldogm6

Hi Tom,
Sorry i just realize there was limitation to make reply here.
So iam trying to change the else and the return, if i use them it will be infinite loop.
And i will attach the circuit diagram .
By the way, i use 2 same type sensor which is GP2Y1014AU0F, and the wiring diagram quite same with the attachment but i only make it seperated (the capacitor and resistor).

ronaldogm6

Hi @pert ,
I realize my mistakes, i write wrong code. This is the right one.


Code: [Select]

/////////////////////////////////////////////////////////////////////////////
// Sharp GP2Y1014AU0F Dust Sensor Demo
//
// Board Connection:
//   GP2Y1014    Arduino
//   V-LED       Between R1 and C1
//   LED-GND     C1 and GND
//   LED         Pin 7
//   S-GND       GND
//   Vo          A5
//   Vcc         5V
//
// Serial monitor setting:
//   9600 baud
/////////////////////////////////////////////////////////////////////////////

// Choose program options.
//#define PRINT_RAW_DATA
#define USE_AVG

// Arduino pin numbers.
const int sharpLEDPin = 7;   // Arduino digital pin 7 connect to sensor LED.
const int sharpVoPin = A7;   // Arduino analog pin 5 connect to sensor Vo.
const int sharpLEDPin2 = 2;   // Arduino digital pin 7 connect to sensor LED.
const int sharpVoPin2= A0;   // Arduino analog pin 5 connect to sensor Vo.

// For averaging last N raw voltage readings.
#ifdef USE_AVG
#define N 150
#define N2 150
static unsigned long VoRawTotal = 0;
static unsigned long VoRawTotal2 = 0;
static int VoRawCount = 0;
static int VoRawCount2 = 0;
#endif // USE_AVG

// Set the typical output voltage in Volts when there is zero dust.
static float Voc = 0.6;
static float Voc2 = 0.6;

// Use the typical sensitivity in units of V per 100ug/m3.
const float K = 0.4;
const float K2 = 0.4;
 
/////////////////////////////////////////////////////////////////////////////

// Helper functions to print a data value to the serial monitor.
void printValue(String text, unsigned int value, bool isLast = false) {
  Serial.print(text);
  Serial.print("=");
  Serial.print(value);
  if (!isLast) {
    Serial.print(", ");
  }
}
void printFValue(String text, float value, String units, bool isLast = false) {
  Serial.print(text);
  Serial.print("=");
  Serial.print(value);
  Serial.print(units);
  if (!isLast) {
    Serial.print(", ");
  }
}

/////////////////////////////////////////////////////////////////////////////

// Arduino setup function.
void setup() {
  // Set LED pin for output.
  pinMode(sharpLEDPin, OUTPUT);
   pinMode(sharpLEDPin2, OUTPUT);
 
  // Start the hardware serial port for the serial monitor.
  Serial.begin(9600);
 
  // Wait two seconds for startup.
  delay(2000);
  Serial.println("");
  Serial.println("GP2Y1014AU0F Demo");
  Serial.println("=================");
}

// Arduino main loop.
void loop() { 
  // Turn on the dust sensor LED by setting digital pin LOW.
  digitalWrite(sharpLEDPin, LOW);
   digitalWrite(sharpLEDPin2, LOW);

  // Wait 0.28ms before taking a reading of the output voltage as per spec.
  delayMicroseconds(500);

  // Record the output voltage. This operation takes around 100 microseconds.
  int VoRaw = analogRead(sharpVoPin);
  int VoRaw2 = analogRead(sharpVoPin2);
 
 
  // Turn the dust sensor LED off by setting digital pin HIGH.
  digitalWrite(sharpLEDPin, HIGH);
  digitalWrite(sharpLEDPin2, HIGH);

  // Wait for remainder of the 10ms cycle = 10000 - 280 - 100 microseconds.
  delayMicroseconds(9620);
 
  // Print raw voltage value (number from 0 to 1023).
  #ifdef PRINT_RAW_DATA
  Serial.println();
  printValue("VoRaw", VoRaw, true);
  Serial.println();
  printValue("VoRaw2", VoRaw2, true);
  Serial.println();
  #endif // PRINT_RAW_DATA
 
  // Use averaging if needed.
  float Vo = VoRaw;
  float Vo2 = VoRaw2;
  #ifdef USE_AVG
  VoRawTotal += VoRaw;
  VoRawTotal2 += VoRaw2;
  VoRawCount++;
  VoRawCount2++;
  if ( VoRawCount >= N ) {
    Vo = 1.0 * VoRawTotal / N;
    VoRawCount = 0;
    VoRawTotal = 0;
  } else {
    return;
  }
 
 
  if ( VoRawCount2 >= N2 ) {
    Vo2 = 1.0 * VoRawTotal2 / N2 ;
    VoRawCount2 = 0;
    VoRawTotal2 = 0;
  } else {
    return;
  }
  #endif //

  // Compute the output voltage in Volts.
  Vo = Vo / 1024.0 * 5.0;
  printFValue("Vo", Vo*100.0, "mV");
 
  Vo2 = Vo2 / 1024.0 * 5.0;
  printFValue("Vo2", Vo2*100.0, "mV");
 

  // Convert to Dust Density in units of ug/m3.
  float dV = Vo - Voc;
  if ( dV < 0 ) {
    dV = 0;
    Voc = Vo;
  float dV2 = Vo2 - Voc2;
  if ( dV2 < 0 ) {
    dV2 = 0;
    Voc2 = Vo2;
  }
  float dustDensity = (dV / K) * 100.0;
  printFValue("DustDensity", dustDensity, "ug/m3", true);
  float dustDensity2 = (dV2 / K2) * 100.0;
  printFValue("DustDensity2", dustDensity2, "ug/m3", true);
  Serial.println();

 
}
}// END PROGRAM

pert

This will happen when dV < 0. From the indentation in your code, it looks like maybe you don't expect the if statement to end here:
Code: [Select]

  if ( dV < 0 ) {
    dV = 0;
    Voc = Vo;

But if you do a Tools > Auto Format you can see the actual program structure:
Code: [Select]
  if ( dV < 0 ) {
    dV = 0;
    Voc = Vo;
    float dV2 = Vo2 - Voc2;
    if ( dV2 < 0 ) {
      dV2 = 0;
      Voc2 = Vo2;
    }
    float dustDensity = (dV / K) * 100.0;
    printFValue("DustDensity", dustDensity, "ug/m3", true);
    float dustDensity2 = (dV2 / K2) * 100.0;
    printFValue("DustDensity2", dustDensity2, "ug/m3", true);
    Serial.println();


  }

ronaldogm6

Hi @pert,
It worked very well but i find 2 sensors have different value, could you please give me suggestion ?
I will attach the wiring of my circuit and the results of the serial monitor.

TomGeorge

Hi,
OPs pictures;


Thanks for the info, however can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What are you using for a power supply?

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

ronaldogm6

hi @tomgeorge ,
Sorry for the late replay because iam quite busy recently.
I figure out and trying to make own pcb then the wiring should be not mess up .
But the problem is i cant put the sensor together at one time, because the serial monitor wont come out.
If i plug the dust sensor just one the serial monitor will work, then i plug second sensor .
What do you think ?
By the way i attach your request.

TomGeorge

Hi,
Thanks for the PCB.


Why do you have C1 and C2, filter/bypass caps in series with R1 and R2?
What you posted here is not the same basic connection.

Can you post a circuit diagram please, CAD or picture of a hand drawn circuit.
The LED on your PCB has no series current limit.
Did you use a DMM to check voltages  to the modules?

Thanks.. Tom.. :)
Everything runs on smoke, let the smoke out, it stops running....

ronaldogm6

Hi Tom,
Quote
Why do you have C1 and C2, filter/bypass caps in series with R1 and R2?
What you posted here is not the same basic connection.
If this not series, how it could be paralel ?
I dont understand why my PCB led have current limit ? do you mean led in the dust sensor ?
Yes i check with DMM for testing the pin connection but if i test the connection between the ground with the cover of the sensor it will be connected.
By the way i try to make the hand drawn connection between the arduino and the sensor.
Sorry for the bad hand writing.
Thanks,

TomGeorge

Hi,
Thanks for the circuit;

It doesn't show all the components you have on your PCB.
Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

TomGeorge

Hi Tom, If this not series, how it could be paralel ?
I dont understand why my PCB led have current limit ? do you mean led in the dust sensor ?
Yes i check with DMM for testing the pin connection but if i test the connection between the ground with the cover of the sensor it will be connected.
By the way i try to make the hand drawn connection between the arduino and the sensor.
Sorry for the bad hand writing.
Thanks,
If you look at your basic diagram, you have R1 and C1 in series with pin1 of the sensor connected to the joint of R1 and C1, which is fine as it be a current limit for the LED in the sensor.
On your PCB pin 1 of the sensor is NOT connected to the join of R1 and C1.
The same for R2 and C2.
The sensor LEDs in your PCB do not have current limiting, so the LEDs are probably failing or being overdriven.
Tom.. :)

Everything runs on smoke, let the smoke out, it stops running....

ronaldogm6

Hi Tom,
Because the component was the part of ESP component and i thought its not related to the dust sensor but if you want all the wiring diagram i will draw it for you. Hereby i attach the wiring diagram the sensor to arduino and lcd. By the way, i tried to connect with capacitor and R1 , then the result its diffrent thank you for your advice ( i will attach the results). By the way i found the new problem the lcd only displaying the black box ( no character ), do you have suggestion for me ?

Go Up