Problem with Serial.println

#include <Servo.h>   // include the servo library to control servo easier
Servo servo1;        // naming the servo so the arduino recognize our servo
int LED1 = 10;         // pin 2 for first led
int LED2 = 11;         // pin 3 for second led
int LED3 = 12;         // pin 4 for third led
int LED4 = 13;         // pin 5 for forth led
int trigPin = 6;      // trig pin for ultrasound emiter goes to pin 6
int echoPin = 7;      // echo pin for ultrasound receiver goes to pin 7
int distance;        // creating variables for distance
long duration;       // creating variables for large number using long function
int motionSensor = 8; // signal pin of motion sensor goes to pin 8
int state;
int secondState;

void setup() {
  // put your setup code here, to run once:
  servo1.attach(9);           // servo is attach in pin 9
  pinMode(motionSensor,INPUT);// setting motion sensor for input to receive data    
  pinMode(LED1,OUTPUT);       // setting led for output to generate voltage
  pinMode(LED2,OUTPUT);       // setting led for output to generate voltage
  pinMode(LED3,OUTPUT);       // setting led for output to generate voltage
  pinMode(LED4,OUTPUT);       // setting led for output to generate voltage
  pinMode(trigPin,OUTPUT);    // setting trig pin for output to emit ultrasound wave
  pinMode(echoPin,INPUT);     // setting echo pin for input to receive ultrasound wave
}

void loop() {
  ///////////ULTRASOUND DISTANCE DETECTOR////////////////
  digitalWrite(trigPin,LOW);        // giving low voltage to trig pin for 2 microseconds
  delay(20);
  digitalWrite(trigPin,HIGH);       // giving voltage to trig pin to emit ultrasound wave
  delay(100);
  digitalWrite(trigPin,LOW);        // turning off trig pin again
  duration = pulseIn(echoPin,HIGH); // read the time taken for echo pin to receive ultrasound wave
  distance = duration *0.034/2;     // velocity of sound is 0.034cm/µs,hence the distance = velocity*time /2(time needed for signal to bounce back)
  Serial.begin(9600);
  
  //////////LED SHOWING CAPACITY OF DUSTBIN/////////////

for (int i = 0; i <= 4; i++) {
  state = i;
  
  while (state == 0) {
    if (distance > 20) {
      digitalWrite(LED4,LOW);
      digitalWrite(LED3,LOW);
      digitalWrite(LED2,LOW);
      digitalWrite(LED1,LOW);
      getStateValue ();
    } 
    break;
  }

  while (state == 1) {
    if (distance > 15 && distance <= 20){
      digitalWrite(LED4,HIGH);
      digitalWrite(LED3,LOW);
      digitalWrite(LED2,LOW);
      digitalWrite(LED1,LOW);
      getStateValue ();
    }
    break;
  }
  
  while (state == 2) {
    if (distance > 10 && distance <= 15){
      digitalWrite(LED4,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED2,LOW);
      digitalWrite(LED1,LOW);
      getStateValue ();
    }
    break;
  }

  while (state == 3) {
    if (distance > 5 && distance <= 10){
      digitalWrite(LED4,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED1,LOW);
      getStateValue ();
    }
    break;
  }

  while (state == 4) {
    if (distance <= 5){
      digitalWrite(LED4,HIGH);
      digitalWrite(LED3,HIGH);
      digitalWrite(LED2,HIGH);
      digitalWrite(LED1,HIGH);
      getStateValue ();
    }
    break;
  }
  
  secondState = getStateValue ();
  if (secondState != state) {
    Serial.println (secondState);
    state = secondState;
  }
}

  

  //////////////////MOTION SENSOR////////////////////
  int sensorValue = digitalRead(motionSensor);
  if(sensorValue == 1){
    servo1.write(180);
    if(distance<10){
      servo1.write(180);
    }
  }
  else {
    servo1.write(0);
  }

}

int getStateValue () {
  if (state == 0 && distance > 20) {
    //Serial.println(0);
    state = 0;
  }
  else if (state == 1 && distance > 15 && distance <= 20) {
    //Serial.println(25);
    state = 1;
  }
  else if (state == 2 && distance > 10 && distance <= 15) {
    //Serial.println(50);
    state = 2;
  }
  else if (state == 3 && distance > 5 && distance <= 10) {
    //Serial.println(75);
    state = 3;
  }
  else if (state == 4 && distance <= 5) {
    //Serial.println(100);
    state = 4;
  }
  return state;
}

I wanted my code to print
0
25
50
75
100
in the serial monitor

But it seems to be not printing
I'm still quite new to this

Can someone pls take a look what's wrong with my code?

Not on a PC at the moment but moving Serial.begin from loop to setup might solve the issue.

I tried it and it is still the same

Pls take a look at the
getStateValue () function that i created

I commented out those serial.println lines bcuz
When i tested it
it keeps on printing non-stop when a state is true

For example when the ultrasonic distance detector
doesn't detect an object
which means the distance > 20
it keeps on printing 0 in the serial monitor endlessly

I only wanted it to print 0 once
until when the 'state' changes

For instance,
when it detects an object at this range which is
'distance > 15 && distance <= 20'
it will print 25 ONCE only

I just don't know what to do with my code to achieve that goal

Hi,
Have you got the IDE monitor set to 9600?

Put a Serial.println("Starting Program");

As the last line in your setup.

It should print each time you reset the Arduino.

What model Arduino are you using?

Sorry you have got print out my mistake.

Tom... :slight_smile:

Hi,
You only need to print when the distance changes.

So store the distance as olddistance and compare it with the next ping.
If they are the same, DON'T print.
If they are not the same, PRINT and update the olddistance as the pinged distance you are printing.

I hope that makes sense.

Tom... :slight_smile:

Awesome0309:
But it seems to be not printing

Awesome0309:
When i tested it
it keeps on printing non-stop when a state is true

It seems to me you are not describing the problem accurately - both of those statements can't be true.

In this code

int getStateValue () {
  if (state == 0 && distance > 20) {
    //Serial.println(0);
    state = 0;
  }

you have nothing to identify the fact that the message has been printed and should not be repeated.

As a more general comment you have a lot of blocking code in loop() with FOR and WHILE. I suspect both could be replaced by IF and then loop() would do the repetition for you.

...R

Sorry
I didn't made myself clear

I just wanted it to print 0 when it detects an object with a distance > 20
But I wanted it to print once only until it detects another object with a distance that is less than 20

When it detects another object
it will print 25 50 75 and so on

But, now I commented the serial.println for the time being to stop the program to print 0 endlessly when it detects an object with a distance > 20

the statement that i made earlier which is
'but it seems to be not printing'
was a misunderstanding

Sorry

Tom

I'm using an Arduino Uno

I'm still quite new to coding

Can you pls further explain your suggestion
I would like to learn :grinning:

Tom
I think I just figured it out your suggestion

Robin
Your advice on removing on the for and while loops helped a lot

Thx for helping