element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum HELP PLEASE with sketch
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 10 replies
  • Subscribers 394 subscribers
  • Views 679 views
  • Users 0 members are here
  • help
  • sketch
Related

HELP PLEASE with sketch

Former Member
Former Member over 10 years ago

I have a short 15 line sketch for a UNO.  It is to read a ultra sonic sensor and out put the results to the computer.  I will not compile. and has an error that I cannot find.  Is there some one who would be willing to help me find the problem.

 

If I knew how I would put it on the forum here if that would be OK.  I am not sure how to do that though.

  • Sign in to reply
  • Cancel

Top Replies

  • dtsartsanis
    dtsartsanis over 10 years ago in reply to Former Member +2
    Your code miss the void setup section put Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); to void setup section and i believe will work
  • dmaruska
    dmaruska over 10 years ago in reply to clem57 +1
    I just did a search and found the HC-SR04 which used the Trig and Echo pins. I presume this is the sensor that he is using.
Parents
  • Former Member
    0 Former Member over 10 years ago

    #define trigPin 3

    #define echoPin 2

     

    void loop(){

      Serial.begin (9600);

      pinMode(trigPin, OUTPUT);

      pinMode(echoPin, INPUT);

     

      int duration, distance;

      digitalWrite(trigPin, HIGH);

      delayMicroseconds(1000);

      digitalWrite(trigPin, LOW);

      duration = pulseIn(echoPin, HIGH);

      distance = (duration/2) / 29.1;

      Serial.print(distance);

      Serial.print(" cm");

      delay(500);

    }

     

     

    it says there is an  undefined reference to 'setup'

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to Former Member

    You need the following:

     

    void setup(){

      }

    above void loop.

    This is code that runs one time! I would expect some commands like

      Serial.begin (9600);

      pinMode(trigPin, OUTPUT);

      pinMode(echoPin, INPUT);

    inside!

     

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dmaruska
    0 dmaruska over 10 years ago in reply to clem57

    Hi,  I have been looking at the code, and wondering what sensor are you using.  I have the Parallax sensor and could not get your code to work.  I made some changes and it works with this new code.  I have noticed that you have a trigger pin and a echo pin, this is the reason of my question. But with the Parallax this code works, the Parallax unit only has 3 pins, +5, GND and SIG. I also made a change on the Serial.print(" cm"); to include the "ln" to give it a line feed or return and start a new line.

     

    int trigPin = 3;

    //int echoPin = 2;

     

    void setup()

    {

      Serial.begin (9600);

    }

     

    void loop()

    {

      pinMode(trigPin, OUTPUT);

      //pinMode(echoPin, INPUT);

      long duration = 0;

      long distance = 0;

      digitalWrite(trigPin, LOW);

      delayMicroseconds(2);

      digitalWrite(trigPin, HIGH);

      delayMicroseconds(5);

      digitalWrite(trigPin, LOW);

      pinMode(trigPin, INPUT);

      duration = pulseIn(trigPin, HIGH);

      distance = (duration/2) / 29.1;

      Serial.print(distance);

      Serial.println(" cm");

      delay(500);

    }

     

    Regards,

     

    Dave M.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 10 years ago in reply to dmaruska

    This topic was for Arduino IDE using UNO from bobscnc. He said:

    It is to read a ultra sonic sensor and output the results to the computer

    The code may or may not work on any other environment. So your mileage may vary dmaruska

    Cheers,

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • dmaruska
    0 dmaruska over 10 years ago in reply to clem57

    I just did a search and found the HC-SR04 which used the Trig and Echo pins. I presume this is the sensor that he is using.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • dmaruska
    0 dmaruska over 10 years ago in reply to clem57

    I just did a search and found the HC-SR04 which used the Trig and Echo pins. I presume this is the sensor that he is using.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • clem57
    0 clem57 over 10 years ago in reply to dmaruska

    These are very common, so I think so. Here is a good view of one of these:

    Ultra01_01

    http://www.elecfreaks.com/244.html


    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube