anyone here good with arduino code?

Ask and answer questions, share tips and resources for installing lighting and other electronics in your models.

Moderators: Sparky, Moderators

Post Reply
User avatar
ashrond
Posts: 1144
Joined: Mon Jan 07, 2008 12:24 pm
Location: Seattle, Wa

anyone here good with arduino code?

Post by ashrond »

hi all,
my most recent project has led me to attempt to light the ship with an Arduino so i can do a non static lighting setup.
unfortunately i am having allot of difficulty in getting the code to work correctly beyond the basics.
i wanted to see if i could get anyone here to lend me their knowledge in troubleshooting my code or perhaps telling me a better/ easier way to implement it.

at present i am using a Qduino (it has a built-in LiPo battery charging circut!)
when starting it, my lights come on in sequence, perhaps a bit too quickly though.
https://1drv.ms/v/s!AucOFvCkv1nCkb8jfblfHhbkRmnk_A

running light,
strobe,
nav,
deflector,
impulse,
nacelle,

the major issues im having at the moment are that, the deflector, impulse and nacelle should all be fading on. i think my issue is that i am not implementing the PWM correctly.

additionally, i have a glass reed switch connected to the board, the intended purpose is to be able to change up the lighting state by waving a magnet over the model EG. startup and dock idle, Impulse, Warp modes.

both of these issues essentially tie together because i had wanted to use PWM to control the lighting levels on the Deflector, Impulse and Warp Nacelles to indicate the different modes.
EG. the Nacelles and impulse engines would only be very dimly light in dock - just barely viable as if warming up

at the moment i have attempted to import the code that i need to my script, and just focus on the startup progress.
everything lights in correct order, but i would like to achieve the fading on effect, after that i should be able to copy the startup state and make the changes i want for the next "mode"

then i need to get the switch to trigger a mode change.

my code is a mess at the moment from late night attempts to get it working and my brain is kind of fried at this point so if anyone could help me out, i would appreciate it allot!

Arduino code :https://1drv.ms/u/s!AucOFvCkv1nCkb8kcR3EtF4BVGQ7EQ

a note if anyone opens this, the only script that is getting uploaded is the primary one marked Celestia, the rest are samples of the original code snippets.

-ash
Boldly being “that guy” since 2001.
NX-17000
Posts: 498
Joined: Fri Nov 16, 2012 1:05 pm
Location: Halifax, Nova Scotia

Re: anyone here good with arduino code?

Post by NX-17000 »

I've never programmed with Arduino (although I do have experience with the Atmel chips used on them), so I may be overlooking something, but I think the issue is the PWM as you believed.

As I understand it, PWM allows you to control the brightness of LED's without implementing any circuitry to control the current into the LED. Different duty cycles (Time Hi/Lo) lets you manipulate the brightness. So, simply setting a single duty cycle value means that you'll only have a single brightness visible. The lights will in fact fade in as the current rises, but at a speed that we simply can't perceive.

Try using a few different values for the PWM in sequence - Say 20%, 30%, 40%, 50% etc over the span of a few seconds. Spend maybe 500 ms to a second in each state and adjust until you get the effect that you're looking for. That should give you the fade-in that you're looking for. If you find that it jumps in brightness too quickly, you'll either have to compress the time you spend in each state, or increment the duty cycle in smaller amounts, say 5%.

Hope this helps - When you get it working, send a video! I'd like to see how it looks, I've thought about implementing similar electronics in my 350 refit when I get around to building her.
User avatar
ashrond
Posts: 1144
Joined: Mon Jan 07, 2008 12:24 pm
Location: Seattle, Wa

Re: anyone here good with arduino code?

Post by ashrond »

alright, second attempt to post this, the site keeps logging me out before the composition is complete :x

so, in an accident i realized that the pins that i was using to connect the leds i wanted to dim were NOT PWM pins.
i only realized the error while i was reading up on other similar projects and one person was using code that called out the led not by pin but by OC#x, which caused me do a double take back to my board diagram

https://cdn.sparkfun.com/datasheets/Dev ... MiniV1.pdf

and sure enough for the intended dimmable lights i was using every pin EXCEPT those that were PWM enabled.

so with that oversight corrected i now have my LEDS dimming for the most part.

figuring out this issue allowed me to clean up allot of junk code that got pulled in while trying to get fading to work, as it turns out that the fader library does in fact work as i was the bug.

the issue i am currently contending with is that i have 4 lights i want to dim and i am having trouble with 2 of my 5 PWM pins. D12 does not seem to be working at all while i am getting a near constant 3.74volts from D3, i know its not the code in this case because i get the same issue no matter what script i upload, if i can figure out why D3 is in effect "always on" i can live with D12 not working.

i suppose that the issue is also something glaringly obvious, but for now it causes issues when trying to dim leds or turn them off entirely.

also of note and possibly related to the odd behavior of the pins is that i cannot seem to get my reed switch to trigger a state change. i have repeatedly successfully tested the switch as a circuit interrupt but when i try to use it as a state change switch i get nothing.

if your curious, i have been following this wiring setup:
https://lh3.googleusercontent.com/5SRJO ... INCPjxVrRn

i should also note, that i am beginning to suspect my breadboard is causing some issues, so i might be rebuilding the circut on another breadboard tonight if i can motivate myself to bother.

if that does eliminate even one of the above mentioned issues, and i am able to get the switch to read, all i need to do is write new states for impulse and warp modes and integrate the switch code which is living in a separate script entirely right now.
Boldly being “that guy” since 2001.
Ant
Posts: 517
Joined: Tue Nov 19, 2002 8:02 am
Location: Oxford UK
Contact:

Re: anyone here good with arduino code?

Post by Ant »

Check all the pins you are using for the LEDs are set to OUTPUT with the pinMode command - that's a common issue when outputs don't appear to be behaving.
User avatar
ashrond
Posts: 1144
Joined: Mon Jan 07, 2008 12:24 pm
Location: Seattle, Wa

Re: anyone here good with arduino code?

Post by ashrond »

so small update,
https://1drv.ms/v/s!AucOFvCkv1nCkb89IVMScl5d4iiaew
we are now somewhat functional and all electronic issues have been figured out, the only remaining issues are in code.

the breadboard was in fact causing some issues, so i replaced it.
the lighting was not dimming was due to not being on PWM pins,
the constant voltage was resolved by using a pull down resister

once all of these above were resolved things started working correctly, the code got allot cleaner as a result.

long term, i think i may change the reed switch to a hall effect sensor, as i am not seeing the responsiveness that i want and much like the the reed i MAY happen to have a hall sensor burred in a drawer somewhere around here.

the major issue i am having at the moment is in change states.
put simply, things are not handing off correctly when changing the variables for the led dimming and when you try to go past "dock mode" the lights start going crazy like they are trying to be in 2 settings at once. i have tried setting things up in such a way that there is an intermediate state that turns off the led faders between changing to the next state as well as many variations of that.

i suspect that the issue is just me not understanding the syntax of the ledfader.h library. unfortunately, its got next to no comments in it to instruct me on its implementation so my guides are other examples of its usage.

oh well, at least the hard work is mostly over.
Boldly being “that guy” since 2001.
Post Reply