Go Down

### Topic: who can blink LED  like sine wave ??? (Read 9769 times)previous topic - next topic

#### 875584189

##### Jun 20, 2010, 12:02 am
Can you give me any code??

#### 875584189

#1
##### Jun 20, 2010, 12:06 am
my model is duemilanove

#### Jeff K

#2
##### Jun 20, 2010, 01:12 am
I am going to assume you don't actually mean you want to blink an LED but make it's intensity (brightness) change following the curve of a sine wave.

I don't have any code examples.  But you should probably do some research on just generating sine waves first.  It seems some people use lookup tables that have been pre generated.  Essentially you would find the equation for your sine wave. Select 255 (or other depending on resolution) evenly divided points across the sine wave.  Get values for these points (should be 0 - 255)... . then store these in the memory of the Arduino.

You would then just loop through these values and apply them to the  output the LED is on.
Jeff K - JKDevices.com - home of the MegaMini

#### raron

#3
##### Jun 20, 2010, 02:28 am
Or just use the sin() function..

Code: [Select]
`void loop(){  float something = millis()/1000.0;  int value = 128.0 + 128 * sin( something * 2.0 * PI  );  analogWrite(ledPin,value);}`
Where something is from 0 to 1 (ideally, or a multiple of), and is whatever you base the sin on. Note the 1000[glow].0[/glow], to make it a float calculation (since we need the decimals).

You might have to restrict the int value in the 0 - 255 range to prevent wrap to 0 instead of 256.

And you must use a PWM output pin for analogWrite to work.

#### 875584189

#4
##### Jun 20, 2010, 03:10 am
so , how can I generate a sine wave

#### 875584189

#5
##### Jun 20, 2010, 03:12 am
raron , the led doesn't blink like sine wave

#### raron

#6
##### Jun 20, 2010, 01:55 pmLast Edit: Jun 20, 2010, 02:01 pm by raron Reason: 1
Did you use a PWM output? It works for me.
I don't have a duemilanove, but it should'nt matter.

I forgot to say that the millis() / 1000.0 was for a 1000 ms (as in 1 second) period.

#### Groove

#7
##### Jun 20, 2010, 04:31 pm
Don't forget to compensate for the eye's logarithmic respose, or you won't perceive a true sine brightnesss curve.
Per Arduino ad Astra

#### 875584189

#8
##### Jun 20, 2010, 11:55 pm
raron, no I haven't connected to pwm,how can I connect it and I'm a beginner

#### raron

#9
##### Jun 21, 2010, 01:09 am
The Duemilanove:
http://arduino.cc/en/Main/ArduinoBoardDuemilanove

As you can see, not all the pins can provide a PWM output. Only pins 3, 5, 6, 9, 10 and  11. You must use one of these for analogWrite() to work.

That is for a hardware PWM (built-in the arduino). Software PWM is certainly possible too, on any pin, but the program would be more complex and use more processor time. I thought I saw a software PWM library somewhere, but I can't find it at the moment.

Btw, PWM (Pulse Width Modulation) is really a trick, it's not an analogue signal. It's digital, but as the name implies, its pulsing and the width of these pulses are what varies. The frequency on the arduino is about 450-500 Hz, so you wont see any flicker is you connect it to a LED.
http://www.arduino.cc/en/Tutorial/PWM

Oh and you use a resistor in series with the LED I hope?

Groove:
Quote
Don't forget to compensate for the eye's logarithmic respose, or you won't perceive a true sine brightnesss curve.

True, but I think we can forgo that for now?

#### 875584189

#10
##### Jun 21, 2010, 02:28 am
so  what is the complete code I can upload to the board??? and how can I connect it ??

#### raron

#11
##### Jun 21, 2010, 08:30 pm
Well the only thing missing from the code above is basically the setup() routine, like this: http://arduino.cc/en/Reference/Setup
Modify as required (little exercise for you there ). Then just follow it with the loop() function, as in any arduino sketch: http://arduino.cc/en/Tutorial/Sketch

As for how to connect the LED, do it like this: http://www.makingthings.com/documentation/how-to/connect-an-led-to-a-digital-output
Doesn't matterJust remember to use a PWM output.

#### 875584189

#12
##### Jun 27, 2010, 12:35 am
int Pin = 3;

void setup()
{
Serial.begin(9600);
pinMode(Pin, INPUT);
}

void loop()
{
float something = millis()/5000.0;
int value = 128.0 + 128 * sin( something * 2.0 * PI  );
analogWrite(Pin,value);
}

I got the code, so amazing , thx raron so much

#### xl97

#13
##### Jun 27, 2010, 04:57 pm
just to comment..

raron-

awesome work !

SO many time do I see beginners talked down to.. or just put off with tech babel or random links with no help explaining what they are being linked to.

KUDOS man!

Delivery of good info..

links to help understand what was just explained to him..

example code

and even a quick 'homework' assignment for him to not be spoon fed as much.

A+ help!!!!!

#### 875584189

#14
##### Jun 27, 2010, 07:06 pm
raron
, how can i contact you???

Go Up

Please enter a valid email to subscribe