analogWrite need to hold value for 1s

From my tx **ch4_value **received value 0-255. Need me to change my code:

void loop(){
.
.
.
analogWrite(5,ch4_value); // need hold this received, example 220 for 1s and after 1 change to now received value
delay (1000); // without this! This is stop all loop cod to 1s its not good for another receiving data.

Use millis() instead. Look at the following tutorials:

BlinkWithoutDelay

Arduino Multiple Things

Several Things at a Time

This code work bt no full PWM out:

if (millis()-lastPeriodStart>=periodDuration)
  {
    lastPeriodStart+=periodDuration;
    tone(5,ch4_value, onDuration); 
  }

problem is:
analogWrite(5,ch4_value); no working like this: analogWrite(5,ch4_value, onDuration);

Don't post little code snippets, they're more or less useless.

analogWrite(pin, value) will run continuously until you write a different value e.g. analogWrite(pin, 0). I can't see you ever doing that.

Steve

The following code should implement what you want. All you have to do is insert code that receives the new value. You didn't say how you are receiving the new value.

const unsigned long interval = 1000; // 1 second
unsigned long previousMillis = 0;
byte ch4_value = 0;

void setup() 
{
  analogWrite(5,ch4_value);
}

void loop() 
{
  unsigned long currentMillis = millis();

  //***********************************************************
  //
  // TBD: Get new ch4_value if available
  //
  //***********************************************************
  
  if (currentMillis - previousMillis >= interval) 
  {
    // Interval has passed.  Update the PWM with a new value.
    previousMillis = currentMillis;
    analogWrite(5,ch4_value); // write the latest PWM value
  }
}

See How to use millis() instead of delay()

ToddL1962:
The following code should implement what you want. All you have to do is insert code that receives the new value. You didn't say how you are receiving the new value.

const unsigned long interval = 1000; // 1 second

unsigned long previousMillis = 0;
byte ch4_value = 0;

void setup()
{
 analogWrite(5,ch4_value);
}

void loop()
{
 unsigned long currentMillis = millis();

//***********************************************************
 //
 // TBD: Get new ch4_value if available
 //
 //***********************************************************
 
 if (currentMillis - previousMillis >= interval)
 {
   // Interval has passed.  Update the PWM with a new value.
   previousMillis = currentMillis;
   analogWrite(5,ch4_value); // write the latest PWM value
 }
}

This is done.Tnx.