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 Need a challange? - Can someone help with some code?
  • 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 30 replies
  • Answers 6 answers
  • Subscribers 394 subscribers
  • Views 2128 views
  • Users 0 members are here
Related

Need a challange? - Can someone help with some code?

nealbopper
nealbopper over 8 years ago

Hello Everyone, image

Well, I am just about ready to start constructing my Chicken coop automatic doors. (I am building 2 automatic doors) I got away from the DeWalt drill motors

and just bought 2 12v 30a step motors. I needed to combine some code into another code I am using from Seth's code from his coop door project. Seth has

a site called, "Land to House" I am wanting to add the Helloworld code to his code because Helloworld is how I got my LCD and RTC (real time clock) to work

together. Do I post the code here or do I zip it and send it to you so I'm not going overboard here. Is someone willing to take a look at it? How do you wish I present

the code to you? Thanks in advance. Everyone has been so helpful so far. It will not let me attach a file.

  • Sign in to reply
  • Cancel

Top Replies

  • COMPACT
    COMPACT over 8 years ago +2
    I don't think I can coop with it!
  • nealbopper
    nealbopper over 8 years ago in reply to jlangbridge +1
    Good morning, I didn't work on the doors yesterday. I got some new LED shop lights I wanted to wire into the garage. That's done. I have a question for you. As you can see, this code is for 1 motor. I…
  • jlangbridge
    jlangbridge over 8 years ago in reply to nealbopper +1 suggested
    My sincerest condolences for your loss. Take the time you need, of course. I'll be here, adjusting code and fiddling with devices. From what I can tell, you have here the basics; motors will activate according…
Parents
  • nealbopper
    0 nealbopper over 8 years ago

    Good afternoon people,

    I have the automatic coop door constructed and everything is in place. I have an issue though. Everything works great and I will post a YouTube video showing everything I have done when I get the door operating correctly. My Issue is when I power the unit up, the door rises as it should. I put my finger on the light sensor and the door lowers. It closes as it should but about 5 seconds later it wants to open the door again. (I still have my finger on the light sensor so no light is getting in) Can someone please look at the code and see why. One more observation I am not sure matters is, there's a 2 second delay after the door down switch activates. The limit switch goes past the trigger and is no longer closed. Is this the reason the door tries to open again? Any help would be greatly appreciated as I'm leaving on vacation and I would like to door in operation before I leave on Thursday. Thank you in advance for helping me out if you decide to help. Peace and God bless all of you.

     

    [C] //Locking, Automatic Chicken Coop Door //By Seth Johnson Land To House llc - Pastebin.com

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • vandia
    0 vandia over 8 years ago in reply to nealbopper

    While I honestly haven't read ALL the comments here (read half them a few weeks back), I may be wrong or missing things. If so, please forgive me!
    I just took a look at the code, and while most of my C knowledge is from C++ in 2004, and C# for Unity, I was hoping I could at least help SOME, since you didnt' get a response yet today.

    there's a 2 second delay after the door down switch activates

    Line 108 of your code linked in your last comment gives a 2 second pause before running "motor up" (open).

     

    The limit switch goes past the trigger and is no longer closed

    I'm not really just what you mean by "limit switch", but it looks like you are checking each sensor NOT just to run motor up/down (line 101/106), but also within those functions. I'd personally just rewrite those, though I understand why some people would want to check further before opening.... it just seems redundant to not check on a timer instead for something such as this. That's why you have the 2 second timer before opening/closing. Even 2 seconds apart seems like extra drain.

     

    int reed1Pin = 2;


    Either way, I'd check that reed1pin (line 22) is set to read the proper door switch, and that this door switch is connected properly.
    Otherwise, your problem is elsewhere is in the conditions for calling "motorup()" too soon... IE: the sensor for light is not working properly, or needs to be set higher than the current value to trigger it to open.

     

    In an afterthought, if it's not been discussed: Make sure you still have somewhere safe for your pets to go if they don't get into your little project before the door may trigger... and you may want to add an extra sensor to detect motion before it closes, to ensure nothing gets stuck in there. Either one when you're away may lead to you being disappointed in your project. ^_^;

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • nealbopper
    0 nealbopper over 8 years ago in reply to vandia

    Hello and thank you,

    I was able to fix the problem by moving the lower (down) switch lower so it's closed when the door is down. I will remove the 2 second delay. My chickens do have another safe place to go if they do not make it in time. My chickens are going in way before it gets dark. My friends chickens stay out until it's dark. weird.

    Can you suggest something I can do to keep the motor running if one of the switches does not work properly?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • vandia
    0 vandia over 8 years ago in reply to nealbopper

    I figured it was either a wire not connected right, or the switch wasn't held, lol.

     

    For "keeping the motor running", I presume you mean "remove the requirement for the switch if it malfunctions"? If so, there's an easy one, and a complex way.

     

    In example, on line 121, you have:

    if (switchState2 = HIGH && lightVal < 200)

    You can remove "switchState2 = HIGH && " from the equation, so it will  check the triggers if the lightVal is under 200.
    You would also need to remove the corresponding "switchState# = " from any other if statements, and remove the "if" statement altogether if that switch number is the only condition.

     

    Also, if switch 1 is bad, you have to remove all statements statement says:

    if (switchState2 = [HIGH/LOW])

    and anything between the { and } directly after, or your code won't run at best.... or you'll have a coo-coo chicken coop at worse. XD

     

    The "harder way" is actually a time saver if you can get help writing the code (because I'm honestly better at DEBUGGING than writing, and don't want to steer you wrong since I can't test it).

     

    Write a condition that checks if both sensors or high or low. I can give you a guestimate example:

    if (switchState1 = HIGH && switchState2 = HIGH) {

         #means both sensors are read as being pushed, so one is malfunctioning

         #(command to jump to routine checking sensors and ignoring "if(switch pressed)"

     

    if (switchState1 = LOW && switchState2 = LOW) {

         #means neither sensor is read as being pushed, so one is malfunctioning or motor stopped

         #(command to jump to routine checking sensors and ignoring "if(switch pressed)"

     

    else while(#run regular routines including switch requirements)

     

    If you need me to write the complete script, I can help help later.... but prefer to save time ATM in case you can do it yourself, since I have to post updates for an Edison project I'm making, and my phone broke so I lost many pictures of it. @_@


    Let me know how it goes!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Reply
  • vandia
    0 vandia over 8 years ago in reply to nealbopper

    I figured it was either a wire not connected right, or the switch wasn't held, lol.

     

    For "keeping the motor running", I presume you mean "remove the requirement for the switch if it malfunctions"? If so, there's an easy one, and a complex way.

     

    In example, on line 121, you have:

    if (switchState2 = HIGH && lightVal < 200)

    You can remove "switchState2 = HIGH && " from the equation, so it will  check the triggers if the lightVal is under 200.
    You would also need to remove the corresponding "switchState# = " from any other if statements, and remove the "if" statement altogether if that switch number is the only condition.

     

    Also, if switch 1 is bad, you have to remove all statements statement says:

    if (switchState2 = [HIGH/LOW])

    and anything between the { and } directly after, or your code won't run at best.... or you'll have a coo-coo chicken coop at worse. XD

     

    The "harder way" is actually a time saver if you can get help writing the code (because I'm honestly better at DEBUGGING than writing, and don't want to steer you wrong since I can't test it).

     

    Write a condition that checks if both sensors or high or low. I can give you a guestimate example:

    if (switchState1 = HIGH && switchState2 = HIGH) {

         #means both sensors are read as being pushed, so one is malfunctioning

         #(command to jump to routine checking sensors and ignoring "if(switch pressed)"

     

    if (switchState1 = LOW && switchState2 = LOW) {

         #means neither sensor is read as being pushed, so one is malfunctioning or motor stopped

         #(command to jump to routine checking sensors and ignoring "if(switch pressed)"

     

    else while(#run regular routines including switch requirements)

     

    If you need me to write the complete script, I can help help later.... but prefer to save time ATM in case you can do it yourself, since I have to post updates for an Edison project I'm making, and my phone broke so I lost many pictures of it. @_@


    Let me know how it goes!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
Children
  • nealbopper
    0 nealbopper over 8 years ago in reply to vandia

    OK,

    My coop is small and hard for me to climb in and out of and the reed switches were a major problem. They would work a couple times then they wouldn't or they wouldn't click and the motor would run right by the switch and run until it quit and that would mean I had to crawl inside the coop and fix it. I'M DONE WITH THE REEDS! I have purchased a Linear Actuator as mentioned above that has the limit switches built in. I am going to search for some code that will work utilizing a sun sensor / Light module and see if I cant get this thing to work. Less parts should mean less headaches, right? If anybody knows of existing code, please forward the address and I will have a look at it. I want to thank everyone for all your help. I wanted to make a video but although my coop door looks sweet, even on metal drawer slides, I could never get the cheap reed switches to work consistently. Thank you in advance for any code for the linear actuator. PEACE!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • vandia
    0 vandia over 8 years ago in reply to nealbopper

    I could write a mockup code for you, that you'd just need to probably fix the syntax for, when I get the chance.
    Are you controlling the actuator with a DPDT relay, or something else?

     

     

    Also, if you're experienced with hardware and whatnot, please consider having a look at this thread I'm having trouble with?
    Help on connecting 24v Motors

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • nealbopper
    0 nealbopper over 8 years ago in reply to vandia

    Hello Vandia,

    I'm sorry, I can't be of much help with your project. I wish I could help. :0(

     

    The only problem I see with my existing code is figuring out how to change my reedswitch 1 and 2 code to

    the Linear actuator code for high and low as I am no longer using pins 2 and 4 for my reedswitches. I do not

    understand how to tell the code the Linear Actuator is up or down to trigger the L298N using the photo/light sensor.

     

    Also a quick question.

    James helped me with my initial code below. In the LCD 6 x 24 code, he put pin 3 as my light

    high. I am not using a pin 3. All I am using is a GND, VCC, SDA and the SCL. They have designated pins on the MEGA.

    could this be why my LCD isn't working?

     

    https://www.element14.com/community/external-link.jspa?url=http%3A%2F%2Fpastebin.com%2F8LaQA61j

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • vandia
    0 vandia over 8 years ago in reply to nealbopper

    he put pin 3 as my light high. I am not using a pin 3

    I see where he is using 3 on line 54.

     

    I'm using an I2C screen for my project, but am writing it in Python, so I can't help with libraries. I'm guessing he thought you were using a different screen type than you are. In short, you may be using the wrong library for that screen, or you may be have intended you to connect VCC to Pin3.

    These are guesses though, as I don't know what model screen you have. I also am unsure why you'd NEED a screen.

     

    For controlling the actuator, I did ask before how you're controlling it. I can't help you too much without knowing this.
    Since my knowledge of Arduino-designed items is rather minimum, I would assume there to be a shield to control one, but I do not know. I have my own method, but removed it to shorten this post until I know what you have, and your question wasn't about controlling it, just reading it.

     

    For how to read it.... I use a linear actuator without feedback. I would guess, unless it has (or you have) it's own controller, I'd presume it to have the red/black, then two other wires. I do not know the current or voltage these wires would be, but you should be able to directly or indirectly connect those lines, one meaning closed and one meaning opened, to the pins the switches were on.

     

    Again, this is just lose information, not knowing your parts, what you're working with, etc.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • nealbopper
    0 nealbopper over 8 years ago in reply to vandia

    I'm sorry,

    I am using Arduino Uno.

    I have a 2 wire 16 inch Linear Actuator.

    L298N - H-Bridge Motor Controller

    (I got it to run the actuator open and closed by switching the wires around on the existing code. It just wouldn't retract when I closed of light to the light sensor. )

    I have a 6 x 24 LCD screen (with piggy back for ease of wiring.)

    DS3231 RTC (real time clock)

    Sun light sensor or a photoresistor

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • vandia
    0 vandia over 8 years ago in reply to nealbopper

    I won't be able to help for sure, without knowing what model/part# the screen is... For the bridge/motor controller, I've never used it, but presume it only goes one direction. To move the actuator back and forth, you of course, need to be able to switch polarities.

     

    My option, while it may not be the best, is what I do for most of my projects: 1 DPDT relay wired to switch polarities when it get a signal from your pin, then another (regular) relay, which turns on and off power, to reserve power once it reaches it's end (give an extra second time out to ensure). Doing it this way, allows you to also jump a wire to bypass things, for prototyping, testing, or bypassing a malfunction.

     

    With this, let's say pin 1 = on means it's closing, while pin 1 = off means it's opening. This also means, so long as the relay has power, but if the control line from the board splits/malfunctions, the coop will open without you needing to jump through hoops, as a safety.

     

    If this is something you'd want to do, I can help you rewrite code for it. Just know, 1 pin to control open/close, then 1 pin to turn power off, after the motor cycle open/close finished, to save power/motor life.

    • 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