Go Down

Topic: LEDS - pulsating siren (Read 232 times) previous topic - next topic

emerson1308




Good night friends,


I'm a beginner in arduino, self-taught, learning with videos and some books.


After learning about the doors ~ PWR, and based on an exercise suggested by the book, I decided to try something else:

a pulsating siren.

Instead of the LEDs simply blinking and then flashing at maximum brightness, I made the code so that the LEDs lit and went off gradually.

for that, I used a sine function for the red led, and a cosine function for the led white.

I modeled the functions so that they were between 0 and 1, and so that exactly opposite, while one was at most, the other at the minimum, as shown in the attach.

and below, the code I used:

int redLed = 6 , whiteLed = 3 ;
int redValue , whiteValue ;
float sino , cosino ; 

void setup() {
  pinMode (redLed , OUTPUT);
  pinMode (whiteLed , OUTPUT);
 
 
}

void loop() {
  for (int x=0; x<180; x++){
    sino = 0.5 + 0.5*sin(x*(3.1416/180));
    redValue = seno*250;
    cosino = 0.5 + 0.5*cos((x*(3.1416/180))+1.5);
    whiteValue= cosino*250 ;
    analogWrite (redLed, redValue);
    analogWrite (whiteLed, whiteValue);
    delay(5);
   }
 
  }






now the problem:


The LEDs are perfectly synchronized, as planned, however, the red led oscillates very little, it alternates between maximum and minimum perfectly synchronized with the white, however the minimum and maximum brightness of it are very close.

While the white led swings perfectly between off and on with maximum brightness, red goes from maximum to "middle" on and then back, always in sync with white.

I thought the problem could be in the led, however, I changed the LEDs and it did not work. Then I thought it might be the ~ PWR port problem, I changed the port and the problem persisted.

Everything leads me to believe that the error is in the code. But I've reviewed it hundreds of times and can not find the damn mistake.

Can someone help me?

Edgar1

I would add Serial.print() lines in your code to display in the Serial Monitor the values.
I had similar problem with multiplying and dividing numbers with int, float, etc.
I.e. Arduino code a * b / c might have a different result than a /c * b
If you show with Serial.print() your values then you are sure they are what you think they should be.

PaulRB

#2
Jul 10, 2018, 07:23 am Last Edit: Jul 10, 2018, 07:31 am by PaulRB
Your code is not completing a full cycle.

In this line
Code: [Select]

for (int x=0; x<180; x++){

try changing 180 to 360.

septillion

Also, a sine will give a very rapid change, at least to our eyes. At low brightness our eyes are more sensitive and low brightness is the part the (co)sine changes the fastest. You get nicer results with gamma correction.

And if you want dead easy, FadeLed-library ;)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

emerson1308

Yes,

thanks man!

That was really the problem, I traded for 360 and it worked.


Thanks to everyone else who responded too!


Your code is not completing a full cycle.

In this line
Code: [Select]

for (int x=0; x<180; x++){

try changing 180 to 360.

Go Up