Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9190135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:23:33+00:00 2026-06-17T20:23:33+00:00

I was writing a script for the ardunio so that it would print how

  • 0

I was writing a script for the ardunio so that it would print how far away something was, and I was trying to make it so that if it was equal to the default length (when the script first started) it would not work, and if the distance between the two numbers is greater than 3 inches to print again. Not sure why it isn’t working. At first I tried to make it so that it would not print, also, if it was the same as the last printed length, so if anyone could figure that out instead, that would be amazing. Also, sorry if I sound stressed, I’ve been working on this probably super-simple script for at least 3 hours now.

#include <Ping.h>
Ping ping = Ping(13,0,0);
int defaultlength = 0;
int length = 0;
int afterlength = 0;
void setup(){
  Serial.begin(9600);
  ping.fire();
  defaultlength = ping.inches();
}

void loop(){
  ping.fire();
  length = ping.inches();
  delay(100);
  afterlength = length - defaultlength;
  sqrt(afterlength^2);
  if (afterlength >= 3) {
    Serial.print(afterlength);
    ping.fire();
    Serial.print("Inches: ");
    Serial.print(ping.inches());
    Serial.print(" | Centimeters: ");
    Serial.print(ping.centimeters());
    Serial.print(" | Light: ");
    if (analogRead(A0) >= 1000) {
      Serial.print("ON");
      Serial.println();
    }
    else {
      Serial.print("OFF");
      Serial.println();
    }

  }

  delay(1000);

}

Also, It is not printing anything at all ever. I’m not sure if its not going through the loop or what.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:23:34+00:00Added an answer on June 17, 2026 at 8:23 pm

    Your line sqrt(afterlength^2); doesn’t do anything useful. Did you mean to take the absolute value by writing

    afterlength = sqrt(afterlength*afterlength);
    

    The ^ operator is the bitwise XOR — not at all what you were trying to do.

    Does that make it better?

    As for your other question:
    “At first I tried to make it so that it would not print, also, if it was the same as the last printed length, so if anyone could figure that out instead, that would be amazing.” – here is what you can do:

    1) define another variable in the head of the script – call it lastlength and initialize it to defaultlength (right after you did your first ping in setup())

    2) in the loop, change the if statement to

    if ((afterlength >= 3) && (abs(length - lastlength) > 0.1)) {
    

    3) finally, at the end of your if{} statement, add:

    lastlength = length;
    

    The reason to put that in the if{} block is to make sure that you only update it if things have changed sufficiently – otherwise, you keep the same “don’t tell me again until it’s different than the number you told me before” value. Of course, the > 0.1 value can be replaced with whatever tolerance you want. Note also use of abs() – a bit more compact than the square root of the square.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script that will constantly scan iTunes for new dialog boxes and
So I'm writing a script in python that dumps a renderlog to a text
Im writing a script that should do this... chroot /chroot_dir/ su - ./startup.sh (This
I'm writing a script that opens a text file and loops through each line
I am writing a script that prompts the logged in user for their password
I'm writing a script that uses some CPAN modules. What can I do so
I'm writing a script that logs errors from another program and restarts the program
I am writing a script, where I need to set memory limit to something
I'm currently writing a script that aims to write Google Calendars for 50 people
I am writing a script where I would like to handle mouse events only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.