I'm creating a Halloween project: a skull that turns as people walk by it. I'm using three PIR sensors at different angles to sense approaching people, and a servo to position the skull. I've written the code and breadboarded the circuit; everything works fine.
Even though it works, is there a more elegant way to write my positioning code? I'm an I&C hardware guy by training; coding is not my normal or natural skillset. I'd appreciate any advice or recommendations on how to clean up the positioning code, it seems clunky as written. I'm sure that there's a better and cleaner way to write it.
Please tell us you're not using the Arduino as the +5V for the servo...please?
What's clunky about it? Like, takes too long to react, or jumps from point to point instead of a smooth transition...?
You could, instead of jumping between 30 degree points at at time, use some speed control and write to the servo across the ranges in between the 20, 55, 90 etc desired endpoints.
Also, from a practical perspective, if the servo isn't holding anything in place between calls to move the servo, it's a good idea to servo.attach(9);, move the servo, then servo.detach(); to save wear and tear on the servo and help prevent it jittering while it's idle. For this, you might find it useful to keep track of the last states of the PIR sensors, and only perform the Position(); and Move(); functions if something changed.
If OP does decide to change something, call the existing, working code myCode_v1 or something like that, add in a little Serial printing right after void setup() like
@hallowed31 For my breadboarding, I'm using the Arduino to power the servo. I have a 100 microF capacitor across the servo power wiring to minimize momentary power sag. For the final version, I might power the servo directly from a 5V power supply. The skull is light, so I'm not expecting a lot of torque on the servo.
By clunky I mean: not an efficient use of code lines. I figure there has to be a way to code it better, I just don't have enough coding experience to know how best to approach it. Operationally, the motion is jumpy. I'll have to figure out how to slow the servo motion down without disturbing the scan time. Future additions to this project will use more void loops to perform additional tasks, so a function can't bog down the whole program.
Didn't realize that I could attach and detach the servo. Good idea!
To all: I've learned the hard way to save my work. It's easy to get lost when revising code as to what worked, and what didn't. Apparently, once you compile and upload code, you can't get it back. Wish I knew that when I started...
PS A question about variables: int versus bool versus byte. For this project I'm not doing anything fancy. Is there anything inherently wrong about using bool for TRUE/FALSE inputs versus byte or int? Or using byte versus int for a 0-255 variable?
@tampadavid I'll leave it to you to put the right angles in the right places in the 3D array. You only have 5 angles but there will be 8 values in the 3D array, so just put that centre angle in as a default for any unwanted positions.
Then this function would become extremely "elegant"!
You might as well sort this in prototyping. Were you planning on having it plugged into mains? If so, a 5V wall wart of 2A rating will probably do, but check the datasheet for all the devices it will be powering, multiply the sum of the amperage requirements by 1.5 at least. (for the servo, that's the stall current).
let variables and function names start with low letters (see setup() and loop())
why these double line breaks in Position()?
line breaks in general, why are there so many empty lines - and not even consistent. there is a line feed after loop() but not after Sense()
code wise:
why do you hardcode LED pins and not using constants for the LED Pins?
I don't think you will need CurrentTime in global namespace, may be you don't need it at all.
Position() contains lot of repeated lines of code. If you can't solve this in another way, at least think about, whether you need single if statements or if you could use else if to finish the function earlier.
check your int variables ... ask yourself, is there any need of a signed two byte type needed for what you need?
learn something new:
learn about structures. For examples take the PIR Sensor. for the PIR sensor you need at least one pin and you store its state in the status variable. Sounds like a structure for me.
From functional point of view (just my opinion)
slow down the movement of the servo the get smoother movements
Thanks for all the advice. It will take me a week or two to digest it, write new code, and try it out. Arrays are a particular challenge - the Arduino starter kit book wasn't real clear about them. When I get this project done, I'll post my results and improved code.
@hallowed31: I do plan to use a 5V wall wart for the Arduino and a 12V wart for my LED strips (to be added), probably controlled through a MOSFET or relay card.
@noiasca: these are the kind of comments I was looking for. I wrote this post because I don't know what I don't know. Elegant may have been the wrong adjective, but its all I could think of. You all have been doing this for much longer than me, and any wisdom is appreciated.
@xfpd If I could make my code look like that, I would!