fokiassociates.blogg.se

Simple desktops sine
Simple desktops sine










As the value rises, "in" is incremented faster and faster, up to a total of 2 times the normal amount.įor me, this produced a more even looking fade in and fade out effect. 001 * out /255 īasically, that makes the loop step through the sinusoid quicker as the PWM value gets higher, so at the bottom, with the led dim or off, "in" is incremented by a very small amount or zero, in addition to the increment provided by the FOR loop. What I found satisfactory for my purposes, was to modify the variable "in" within the FOR loop.Īfter the analogWrite command, I simply added the line: in = in +. Since I had very limited resources with the attiny45, I had to figure out something small and easy. Searching through the internet, I found various ways to compensate for this, for example (), shows several possible ways to compensate, as well as another link to another method (using logarithmic functions). What appears is that the LED remains in the region of brightness, for about twice as long as in the region of darkness. While mathematically correct, it didn't completely LOOK sinusoidal. But if your friends are anything like us, they'll definitely be impressed!īecause of the non-linearity of LEDs when driven by PWM, and also due to non-linearity in how the human eye percieves brightness, I found this fading effect to be not quite optimal. * This statement strongly depends on the exact friends involved. Let us know if you have any questions or comments, and we always love to hear about your projects! We hope that this tutorial adds to your toolkit for making better looking Arduino programs. By adding this modifier, you're telling the compiler to not keep initializing that variable every time we go through the loop. If we didn't use this, "in" would be initialized to 4.712 each time loop() loops, which would keep the LED output permanently at 0. One thing to notice is the "static" modifier in the above declaration of "in". (This is a great way to simulate "multitasking" on a small computer).

#Simple desktops sine code

Copy and paste this into the Arduino of your choice, and hook up a LED and resistor to pin 11: (This example requires a pin capable of PWM, hence pin 11)Īs long as your other code doesn't pause and prevent the LED code from running, the LED will keep pulsing. Instead of boring you with theory, let's jump right in with an example. I used this technique on the Elevator TARDIS to generate the pulsing blue light on top of Doctor Who's time machine, but it's also great for warning lights, art projects, and just plain looking cool. If you don't know what that means, or DO know and are scared, don't worry - it's very simple. It turns out there's an easy and effective way to do this using trigonometry. There are numerous ways you could go about this: use separate "for" loops to ramp the brightness up and down, retrieve values from a look-up table, etc. But you'll really impress your friends * if you can pulse a LED!










Simple desktops sine