[SOLVED]Serial monitor println suddenly change position

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.

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?

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?

  } else {
    return;
  }

Thanks..Tom... :slight_smile:

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?

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

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).

sharp_gp2y1014au0f_circuit[1].png

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

/////////////////////////////////////////////////////////////////////////////
// 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

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:

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

But if you do a Tools > Auto Format you can see the actual program structure:

  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();


  }

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.

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... :slight_smile:

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.

solved.png

Hi,
Thanks for the PCB.
solved.png

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.. :slight_smile:

solved.png

sharp_gp2y1014au0f_circuit[1].png

Hi Tom,

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,

Hi,
Thanks for the circuit;


It doesn't show all the components you have on your PCB.
Tom... :slight_smile:

ronaldogm6:
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.. :slight_smile:

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 ?

ronaldogm6:
Hi Tom,
Because the component was the part of ESP component

What ESP?
Tom.. :slight_smile:

Hi,
In your latest circuit would have a drawing fault.
circ11.jpg
You have a signal line shorted to gnd.

Can I suggest in your code;
Instead of starting both sensors, then reading both sensors, then closing both sensors.

You open read and close the first sensor THEN open read and close the second sensor.

Also when you read the inputs, do it twice, one line after the other like this.

 int VoRaw = analogRead(sharpVoPin);
 int VoRaw = analogRead(sharpVoPin);

This to ensure the ADC has switched cleanly from one input to the next.
You have to remember there is only one ADC and it is switched to the analog inputs.
The reading of one channel charges up a capacitor in the ADC. When switched to next input, the capacitor may have some charge on it from the previous input.

Tom.. :slight_smile:

circ11.jpg

Hi Tom,
Thank you for your reply.

You have a signal line shorted to gnd.

Sorry iam such clumsy guy and make stupid mistake, i think my drawn is the mistake.

What ESP?
Tom.. :slight_smile:

ESP stands for Electro Static Precipitator it was used for filtering air, with principle electro static. We used 10 kv power supply DC voltage, for the collecting plate and charging plate. The basic principle was the charging section (coronoa electrode) will be charged with 8 kv that will charged the ion and later on the charged ion will be collected in the collecting plate. So thats why i asking to you do i need draw it for you? by the way you saw in the pcb we are using digital potensiometer but i think we need gonna used that things .
Okay i will try your suggestion, and i will connect the cable to the capacitor again.

int VoRaw = analogRead(sharpVoPin);
   int VoRaw = analogRead(sharpVoPin);
int VoRaw2 = analogRead(sharpVoPin2);
   int VoRaw2 = analogRead(sharpVoPin2);

By the way last we try to turn on the LCD 20x4 and its work really well but suddenly when we trying to turn it on again the LCD wont show the characters only black box only, do you have suggestion for this problem ? Sometimes its working but suddenly its happen again , and we try test the connection with the multimeter and it seemed its really fine.

Thanks tom for your help.

Hi,
Thanks for the answer.
If you LCD is blocks, try adjusting the pot on the adapter board on the back of the LCD PCB.

Tom.. :slight_smile:

I tried but its not working. By the way i tried your code i mean declare two times but theres problem.

int VoRaw = analogRead(sharpVoPin);
   int VoRaw = analogRead(sharpVoPin);
  int VoRaw2 = analogRead(sharpVoPin2);
   int VoRaw2 = analogRead(sharpVoPin2);

The eror like this

Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\ASUS\Documents\Arduino\dust_sensor_dua\dust_sensor_dua.ino: In function 'void loop()':

dust_sensor_dua:111:8: error: redeclaration of 'int VoRaw'

    int VoRaw = analogRead(sharpVoPin);

        ^

C:\Users\ASUS\Documents\Arduino\dust_sensor_dua\dust_sensor_dua.ino:110:7: note: 'int VoRaw' previously declared here

   int VoRaw = analogRead(sharpVoPin);

       ^

dust_sensor_dua:113:8: error: redeclaration of 'int VoRaw2'

    int VoRaw2 = analogRead(sharpVoPin2);

        ^

C:\Users\ASUS\Documents\Arduino\dust_sensor_dua\dust_sensor_dua.ino:112:7: note: 'int VoRaw2' previously declared here

   int VoRaw2 = analogRead(sharpVoPin2);

       ^

exit status 1
redeclaration of 'int VoRaw'

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

Thanks for your respons Tom,