element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 Need a little help with my sketch please..
  • 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 Verified Answer
  • Replies 1 reply
  • Subscribers 416 subscribers
  • Views 324 views
  • Users 0 members are here
Related

Need a little help with my sketch please..

e14 Contributor
e14 Contributor over 12 years ago

Hello peoples,

 

     Looking for a little help here with my limited programming knowledge. I combined a ping sketch and a laser tripwire sketch. Basically what I have is a laser tripwire connected to a motor. When the beam is broken it triggers a relay connected to a 12v motor with a winch that picks stuff up. In case of some sort of malfunction (laser craps out on me giving the sensor a low reading) I hooked a ping sensor on top of what I am picking up with the motor so the motor doesn't burn out once it reaches the ceiling. Now with the way it is written if I hold my hand over the ping sensor within 10cm and break the beam of the laser the motor will not turn on which is fantastic. If the ping sensor is over 10cm away from my hand and I break the beam the motor turns on until the beam hits the photo resistor and then turns back off which again is fantastic. The problem comes in when the beam is broken and the motor is picking up and comes within 10cm of my hand it doesn't turn off which is kinda the whole point of the ping. I need the ping to act as a master shutoff for the whole program. I would love if someone could please tell me where I am going wrong in 'new to this' terms.

Thanks

Mech1

 

 

int pingPin = 7; //ping
int motorRelay = 9; //relay for motor
int laserRelay = 11; //relay for laser


void setup() {
  pinMode(9, OUTPUT);
  pinMode(11,OUTPUT);
 
}

void loop()
{
  long duration, inches, cm; 
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
 
  digitalWrite(laserRelay, HIGH); //turn on laser
 
  if(cm>10) //if distance is more than 10cm continue
  {
    if(analogRead(0)<750) //if light reading is less than 750
    {
      digitalWrite(motorRelay, HIGH); //turn on motor
  } else {
   digitalWrite(motorRelay, LOW); //otherwise do nothing
  }
  }
}

 

long microsecondsToCentimeters(long microseconds) //ping
{
  return microseconds / 29 / 2; //ping
}

  • Sign in to reply
  • Cancel
  • e14 Contributor
    0 e14 Contributor over 12 years ago

    Well if anyone has the same problem I fixed it with a little old fashion research and some good old Boolean statements.

     

    Changed out this...

     

      if(cm>10) //if distance is more than 10cm continue

      {

        if(analogRead(0)<750) //if light reading is less than 750

        {

          digitalWrite(motorRelay, HIGH); //turn on motor

      } else {

       digitalWrite(motorRelay, LOW); //otherwise do nothing

      }

      }

    }

     

    For this and everything works perfectly.

     

    if(cm>10 && analogRead(0)<750) //if distance is more than 10cm & beam is broken continue

      {

       digitalWrite(motorRelay, HIGH); //turn on motorRelay

       delay(2000);

      } else {

       digitalWrite(motorRelay, LOW); //otherwise do nothing

      }

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Reject 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 © 2026 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.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube