No output produced

// This uses Serial Monitor to display Range Finder distance readings

// Include NewPing Library
#include "NewPing.h"
#include <Servo.h>
// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10
#define TRIGGER_PIN 3
#define ECHO_PIN 4
int gunPIN = 9;
// Maximum distance we want to ping for (in centimeters).
#define MAX_DISTANCE 400

// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
float duration, distance;

Servo myServo;
void setup()
{
  Serial.begin(9600);
  myServo.attach(5); // Defines on which pin is the servo motor attached
  pinMode(gunPIN, OUTPUT);
}

void loop()
{

  // rotates the servo motor from 15 to 165 degrees
  for (int i = 0; i <= 180; i++) {
    myServo.write(i);
    distance = sonar.ping_cm();

    // Send results to Serial Monitor
    Serial.print("Distance = ");

    if (distance >= 200 || distance <= 10)
    {
      Serial.println("Out of range");
    }
    else
    {
      Serial.print(distance);
      Serial.println(" cm");
      digitalWrite(gunPIN, true);
      delay(1500);
      digitalWrite(gunPIN, false);

    }

    delay(30);
  }
  // Repeats the previous lines from 165 to 15 degrees
  for (int i = 180; i > 0; i--) {
    myServo.write(i);
    distance = sonar.ping_cm();

    // Send results to Serial Monitor
    Serial.print("Distance = ");

    if (distance >= 200 || distance <= 10)
    {
      Serial.println("Out of range");
    }
    else
    {
      Serial.print(distance);
      Serial.println(" cm");
      digitalWrite(gunPIN, HIGH);
      delay(1500);
      digitalWrite(gunPIN, LOW);

    }


    delay(30);
  }
}

so my issue is this part doesn't give me any output however it does the delay

      digitalWrite(gunPIN, true);
      delay(1500);
      digitalWrite(gunPIN, false);

Do you mean that the pin does not change state ?
How are you monitoring it ?
Why are you using true and false when HIGH and LOW would be more correct ?

Bear in mind that the pin will spend most of its time HIGH because as soon as you set it LOW, if the target is still in range the pin will go HIGH again almost immediately. As an experiment, put another delay(1500); after you set the pin LOW

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.