No error in sketch but nothing happened.

Hi, everyone.

There’s no error with my sketch, but my project doesn’t seem to work as intended. My hardware works just fine when I do the functions separately (one file for each function), but when I put them together (like the code below), nothing happened. So, I don’t know what I might have missed. I’ve read the “Demonstration code for several things at the same time” but I don’t understand it and I’m not really sure if it has anything to do with what I am doing.

#define echoPin 11
#define trigPin 10
#define servo 9
#define IR 8
#define relay 7
#define motor 6
#include <Servo.h>

Servo myservo;
int pos = 0;
float duration, distance;

void setup() 
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(motor, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(IR, INPUT);
  myservo.attach(servo);
  Serial.begin (9600);
}

void loop() 
{
  motionMotor();
  waterRelay();
  servoIR();
}

void motionMotor() 
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  distance = (duration / 2) * 0.0343;

  Serial.print("Distance = ");
  if (distance >= 100 || distance <= 2) 
  {
    digitalWrite(motor, LOW);
    Serial.println("Out of range");
  }
  else {
    digitalWrite(motor, HIGH);
    Serial.print(distance);
    Serial.println(" cm");
    delay(500);
  }
  delay(500);
}

void waterRelay() 
{
  int val = analogRead(A0);
  if (val >= 400 && val <= 499) 
  {
    digitalWrite(relay, LOW); 
    {
      Serial.print("water pump off  ");
      Serial.println(val);
      delay(500);
    }
  }
  if (val >= 500 && val <= 599) 
  {
    digitalWrite(relay, HIGH); 
    {
      Serial.print("water pump on  ");
      Serial.println(val);
      delay(500);
    }
  }
  if (val >= 600 && val <= 650) 
  {
    digitalWrite(relay, LOW); 
    {
      Serial.print("water pump off  ");
      Serial.println(val);
      delay(500);
    }
  }
}

void servoIR() 
{
  myservo.write(0);
  if (digitalRead(IR) == HIGH) 
  {
    myservo.write(180);
    delay(3000);
    myservo.write(0);
    delay(1000);
  }
}

With this, I attached a screenshot of the serial monitor (5 seconds interval between the lines),

Please help me. Thank you.

Untitled.png

The code you posted is incomplete

AWOL:
The code you posted is incomplete

Thank you for pointed that out. Fixed it.

hafiziation:
Thank you for pointed that out. Fixed it.

Great! Can you mark the thread as "solved" and tell us what fixed it?

Thanks.

AWOL:
Great! Can you mark the thread as "solved" and tell us what fixed it?

Thanks.

Oh no, it's not the problem that has been fixed, but the post. Sorry for the confusion.

You have a sketch that reads proper values from the ping sensor?

Copy that sketch, and add ONE new capability at a time. What breaks the ability to read from the ping sensor?

PaulS:
You have a sketch that reads proper values from the ping sensor?

Copy that sketch, and add ONE new capability at a time. What breaks the ability to read from the ping sensor?

I’m sorry, but I don’t quite understand you. What’s a capability?

hafiziation:
What's a capability?

If you don't speak English then that's OK. But if you don't speak English and you want to use the English part of the forum then you have to at least be willing to look a word up if you don't know it.

Delta_G:
If you don't speak English then that's OK. But if you don't speak English and you want to use the English part of the forum then you have to at least be willing to look a word up if you don't know it.

Capability | Definition of Capability by Merriam-Webster

I do understand the definition of that word. But, I thought the "capability" used by PaulS is like some kind of a programming term. And yes, English is not my first language so pardon my lack of understanding.

But, I thought the "capability" used by PaulS is like some kind of a programming term

Even if it was, you could still look it up and find out what it means. There's no reason to expect someone here to have to write out a definition for you when so many are available everywhere.

Do you understand now what he was trying to say? Make it do one thing at a time. Solve one issue at a time. Add one thing at a time. That way you know what you did that broke it. That can give you the best clue as to what is wrong.

Delta_G:
Even if it was, you could still look it up and find out what it means. There's no reason to expect someone here to have to write out a definition for you when so many are available everywhere.

Do you understand now what he was trying to say? Make it do one thing at a time. Solve one issue at a time. Add one thing at a time. That way you know what you did that broke it. That can give you the best clue as to what is wrong.

Thank you for the reminder/advice.

I do now.

Try putting a few Serial.prints immediately after the calculation/reads so you know what values "distance" and "val" actually have before they get to the if statements and perhaps one in servoIR() to show the state of the IR pin. That will give you some rather more useful information about what is going on.

Steve

Solved. Turns out there's something loose with my Uno. Just had to give it a slight press. Thank you, everyone.