LED fade not working in program

Hello guys,

I'm trying to implement this fade in-out function for LED

int value;
int ledpin = 9;                           // light connected to digital pin 10
long time=0;

void setup()
{
 // nothing for setup
}

void loop()
{
 value = 128+127*cos(2*PI/10000*millis());
 analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
}

When it runs just how it is, it works great, but when I implement this code into my program, it dont works at all. I've tryied put this code into WHILE but it's also didn't worked out.

My program looks like this:

int value;
int ledpin = 9;                           // light connected to digital pin 10
long time=0;
int periode = 10000;

void setup()

{
 // some setting up stuff
}
    void loop()
{
    if (true) 
    {
      if(buf[0]=='2')
      {
        {
          value = 128+127*cos(2*PI/10000*millis());
          analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
      }  
      if(buf[0]=='3')
      {
        digitalWrite(9, LOW);
      } 
}  else  {
        digitalWrite(12, HIGH);
        }
}

I spended on this several hours with no result. Any help will be gladly appreciated! Thank you! ;)

 if (true)

Why bother testing?

 if(buf[0]=='2')

Where is buf defined?

If you're going to use digitalWrite on those pins, then you need to have a pinMode call in setup to set them as outputs.

buf[0] comes from wireless... that part is okay as when I have there digitalWrite(13, HIGH), led attached on 13 light, only problem is when I replace it with this code what I posted here.

Now I tested it with

     if(buf[0]=='2')
      {
        while (1 == 1)
        {
        value = 128+127*cos(2*PI/10000*millis());
        analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
        digitalWrite(13, HIGH);
        }
      }

And when condition is true, led 13 nicely flash, but led on "ledpin" which is now 8 just very slowly blicking, like it show only value 255 and 0 but not fading on-off as I want. in setup I have pinMode(ledpin, OUTPUT);

And when condition is true, led 13 nicely flash,

What? Can we stop playing guessing games now and SEE YOUR CODE?

no prob.
I didn’t posted it before because I modifing it all time. This is last version - sadly didn’t working
When signal(2) come, LED13 light, but fade-ing not working also when I send signal(3), program don’t react and LED13 still light

#include <VirtualWire.h>

int value;
int ledpin = 8;                           
long time=0;
int periode = 10000;

void setup()

{
    Serial.begin(9600);
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_set_rx_pin(0);
    vw_setup(2000);  // Bits per sec
    pinMode(13, OUTPUT);
    pinMode(12, OUTPUT);
    pinMode(ledpin, OUTPUT);
    pinMode(7, INPUT);
    digitalWrite(7, HIGH);
    vw_rx_start();       // Start the receiver PLL running
}
    void loop()
{
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
    if (vw_get_message(buf, &buflen)) // Non-blocking
    {
      if(buf[0]=='2')
      {
        while (buf[0]=='2')
        {
        value = 128+127*cos(2*PI/10000*millis());
        analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
        digitalWrite(13, HIGH);
        }
      }  
      if(buf[0]=='3')
      {
        digitalWrite(13, LOW);
      } 
}  else  {
        digitalWrite(12, HIGH);
        }
   if (!digitalRead(7))
   {
    digitalWrite(13, LOW);
   }   
}

Thank you guys

        while (buf[0]=='2')
        {
        value = 128+127*cos(2*PI/10000*millis());
        analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
        digitalWrite(13, HIGH);
        }

What will change the value of buf[0] in this while loop to enable it to terminate ?

in buffer can be always only one value, so when another value come trought wireless, it will overwrite previous one and another condition should be executed. Anyway, that's not my entire problem My problem is that this fade effect don't working at all, with or without while loop. I'm just trying different variations how to make it work

when another value come trought wireless, it will overwrite previous one and another condition should be executed.

Where is the value read from wireless ?

yes yes, but this working good

I am trying to understand your code but I don't know anything about the VW library and how it works. What I do know is that

        while (buf[0]=='2')
        {
        value = 128+127*cos(2*PI/10000*millis());
        analogWrite(ledpin, value);           // sets the value (range from 0 to 255)
        digitalWrite(13, HIGH);
        }

will not exit the while loop unless the value of buf[0] changes. Does it ever change whilst that while loop is running ?

What do you see if you print value inside the while loop ?

First I would like to Thank you for your attention and help. Code itself works perfect in way I need it. I just wanted to implement function, who will turn static lighting led into nice fade in, fade out effect. The code for fade in-out led I've already pasted above, and when it runs standalone from my device sourcecode, it works perfectly. However, when I implement this fade in-out code into my code, it won't work - code works, but led just blick and not doing any fade effect what I desire for. The way, that led blick means, that rest of the code, also vw libary is ok and we don't have to talk about this I think. This WHILE I've just putted there because I somehow thought that there might be a problem with function loop(), as incase condition is true, code in condition gets executed only once(I'm still not sure how it works actually)

I am sorry to bang on about that while loop but either I don't understand how the VW library works (quite possible) or you don't understand how a while loop works

Try this code but before you run it can you predict the outcome ?

byte x = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println();
}

void loop()
{
  x = x + 1;
  while (x == 1)
  {
    Serial.println("x equals 1");
    delay(500);
  }
}

To me it is the equivalent of your while loop.

I think I already founded out what it's causing. I go trought every line of code, comment it and try what's works, and I founded out that when I deleted/commented line: vw_setup(2000); // Bits per sec Led start to fade!!!

but ofcourse wireless stopped work... Unfortunatelly I have no idea how to fix it. I think it might be something with clock/timer, but I thougt that arduino software can somehow handle this. Also another issue can be connected with, that I'm using just ATmega328 on solderless board, and with arduinoISP i'm just programming it. This is also think, why I can not test your code.

...

wolfking: Also another issue can be connected with, that I'm using just ATmega328 on solderless board, and with arduinoISP i'm just programming it. This is also think, why I can not test your code.

If you can load your code then you can load his code. The process is the same.

@Delta_G

  1. Please read my posts again, whole source code is in above post

  2. Yes, I can load his code, but will be not able to see serial monitor

  3. Commenting line also mean that compiler will ignore it

  4. Are you able to reply on my last question ? --

Thank you

What is being sent over the VW? If you could do some Serial you could print it and see if you're receiving what you think you are.

What is doing the sending?

yes, I'm reciving what I need Again, please read my posts. I'm reciving what I need

my problem is DIFFERENT

and this is why even my source code is not importand(despite is here already)

No matter what code I have in loop() no matter how many IF, WHILE, BUF or whatever else is there ... it will not work. This not work means, that either wireless work, or led fade effects work

and this is because wireless use this command

vw_setup(2000);

and when this is in code in setup(), fade will not work when this is not in code in setup(), fade work! (but of course wireless not)

thats my problem :/