user57854: I have 2 sketches that control a fan using Arduino UNO, that I try to combine.
The 1st is a timer, that turns a pin ON and OFF for a determined period of time.   The pin signal drives a MOSFET that in turn switches a small fan ON and OFF.
Since I need to control the speed of the motor, I would rather use the PWM control, however I am not sure how I can apply the timer since analogWrite command refers to a pin and value.
Thanks
timer code:
const byte relayPin = 6; // relay module on pin 6
const byte monitorPin = 13; // builtin LED output
const unsigned long interval_1 = 1000UL * 15, interval_2 = 1000UL * 15; // ON timer + OFF timer in milliseconds
unsigned long currentMillis, previousMillis = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // relay OFF
  pinMode(monitorPin, OUTPUT);
  digitalWrite(monitorPin, LOW); // LED off
}

void loop() {
  // put your main code here, to run repeatedly:
  currentMillis = millis();
  if (currentMillis - previousMillis >= interval_1)
  {
    digitalWrite(relayPin, LOW); // relay ON
    digitalWrite(monitorPin, LOW); // LED on
  }
  if (currentMillis - previousMillis >= (interval_1 + interval_2))
  {
    digitalWrite(relayPin, HIGH); // relay OFF
    digitalWrite(monitorPin, HIGH); // LED off
    previousMillis = currentMillis; // reset
  }

PWM code:
int PWMControl= 6;
int PWM_Input = A0;

int PWM_Value = 0 ;

void setup() {
  pinMode(PWMControl, OUTPUT);
  pinMode(PWM_Input, INPUT);
}

void loop() 
{
  PWM_Value = analogRead(PWM_Input);
  PWM_Value = map(PWM_Value, 0, 1023, 0, 255);
  analogWrite(PWMControl, PWM_Value);
}


user56694: If I understand correctly, this is what you want to do:

Fan + led off for 15s
Fan on (speed determined by A0) + led on for 15s
Repeat

clearly defining your goal in english can help with coding, so I hope I guessed right.
here's my suggestion for your code
 const byte controlPin = 6; // MOSFET on pin 6
 const byte monitorPin = 13; // builtin LED output
 const unsigned long interval_1 = 1000UL * 15, interval_2 = 1000UL * 15; // ON timer + OFF timer in milliseconds
 unsigned long currentMillis, previousMillis = 0;
 int PWM_Input = A0;
 int PWM_Value = 0 ;

 // setting pin direction and default state
 void setup() {
   pinMode(controlPin, OUTPUT);
   digitalWrite(controlPin, HIGH); // MOSFET OFF
   pinMode(monitorPin, OUTPUT);
   digitalWrite(monitorPin, LOW); // LED off
 }

 // run fan for a while, then stop fan for a while
 void loop() {

   currentMillis = millis();

   //fan on, led on, both signals active low
   if (currentMillis - previousMillis >= interval_1){
       PWM_Value = analogRead(PWM_Input);
       PWM_Value = map(PWM_Value, 0, 1023, 0, 255);
       analogWrite(PWMControl, PWM_Value);
       digitalWrite(monitorPin, LOW);
   }

   //fan off, led off, reset timing
   if (currentMillis - previousMillis >= (interval_1 + interval_2)){
      digitalWrite(controlPin, HIGH); // MOSFET OFF
      digitalWrite(monitorPin, HIGH); // LED off
      previousMillis = currentMillis; // reset
   }
 }

There are still issues I can with this code:

You did not tell us what type of MOSFET you are using, so I can't tell if it is actually active low
I believe that the LED on most arduino boards (pin 13) is active high, so that's probably wrong
Don't forget comments. I find comments more useful for a block of code than for a single line, but you can do both if you want ;)
I find your code convoluted regarding millis logic, for me it would be more logical to have something along the lines of:
 current = millis()-previous
 if current < interval1 
    fan off
 elseif current < inteval1+interval2
    fan on
 else
    previous = millis() 


It would be more readable for a human, the result on the arduino would be similar.

57854: <p>I have 2 sketches that control a fan using Arduino UNO, that I try to combine.
The 1st is a timer, that turns a pin ON and OFF for a determined period of time.   The pin signal drives a MOSFET that in turn switches a small fan ON and OFF.
Since I need to control the speed of the motor, I would rather use the PWM control, however I am not sure how I can apply the timer since analogWrite command refers to a pin and value.
Thanks</p>

<p>timer code:</p>

<pre class="lang-cpp prettyprint-override"><code>const byte relayPin = 6; // relay module on pin 6
const byte monitorPin = 13; // builtin LED output
const unsigned long interval_1 = 1000UL * 15, interval_2 = 1000UL * 15; // ON timer + OFF timer in milliseconds
unsigned long currentMillis, previousMillis = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, HIGH); // relay OFF
  pinMode(monitorPin, OUTPUT);
  digitalWrite(monitorPin, LOW); // LED off
}

void loop() {
  // put your main code here, to run repeatedly:
  currentMillis = millis();
  if (currentMillis - previousMillis &gt;= interval_1)
  {
    digitalWrite(relayPin, LOW); // relay ON
    digitalWrite(monitorPin, LOW); // LED on
  }
  if (currentMillis - previousMillis &gt;= (interval_1 + interval_2))
  {
    digitalWrite(relayPin, HIGH); // relay OFF
    digitalWrite(monitorPin, HIGH); // LED off
    previousMillis = currentMillis; // reset
  }
</code></pre>

<p>PWM code:</p>

<pre class="lang-cpp prettyprint-override"><code>int PWMControl= 6;
int PWM_Input = A0;

int PWM_Value = 0 ;

void setup() {
  pinMode(PWMControl, OUTPUT);
  pinMode(PWM_Input, INPUT);
}

void loop() 
{
  PWM_Value = analogRead(PWM_Input);
  PWM_Value = map(PWM_Value, 0, 1023, 0, 255);
  analogWrite(PWMControl, PWM_Value);
}
</code></pre>
 56694: <p>If I understand correctly, this is what you want to do:</p>

<ol>
<li>Fan + led off for 15s</li>
<li>Fan on (speed determined by A0) + led on for 15s</li>
<li>Repeat</li>
</ol>

<p>clearly defining your goal in english can help with coding, so I hope I guessed right.</p>

<p>here's my suggestion for your code</p>

<pre><code> const byte controlPin = 6; // MOSFET on pin 6
 const byte monitorPin = 13; // builtin LED output
 const unsigned long interval_1 = 1000UL * 15, interval_2 = 1000UL * 15; // ON timer + OFF timer in milliseconds
 unsigned long currentMillis, previousMillis = 0;
 int PWM_Input = A0;
 int PWM_Value = 0 ;

 // setting pin direction and default state
 void setup() {
   pinMode(controlPin, OUTPUT);
   digitalWrite(controlPin, HIGH); // MOSFET OFF
   pinMode(monitorPin, OUTPUT);
   digitalWrite(monitorPin, LOW); // LED off
 }

 // run fan for a while, then stop fan for a while
 void loop() {

   currentMillis = millis();

   //fan on, led on, both signals active low
   if (currentMillis - previousMillis &gt;= interval_1){
       PWM_Value = analogRead(PWM_Input);
       PWM_Value = map(PWM_Value, 0, 1023, 0, 255);
       analogWrite(PWMControl, PWM_Value);
       digitalWrite(monitorPin, LOW);
   }

   //fan off, led off, reset timing
   if (currentMillis - previousMillis &gt;= (interval_1 + interval_2)){
      digitalWrite(controlPin, HIGH); // MOSFET OFF
      digitalWrite(monitorPin, HIGH); // LED off
      previousMillis = currentMillis; // reset
   }
 }
</code></pre>

<p>There are still issues I can with this code:</p>

<ol>
<li>You did not tell us what type of MOSFET you are using, so I can't tell if it is actually active low</li>
<li>I believe that the LED on most arduino boards (pin 13) is active high, so that's probably wrong</li>
<li>Don't forget comments. I find comments more useful for a block of code than for a single line, but you can do both if you want ;)</li>
<li><p>I find your code convoluted regarding millis logic, for me it would be more logical to have something along the lines of:</p>

<pre><code> current = millis()-previous
 if current &lt; interval1 
    fan off
 elseif current &lt; inteval1+interval2
    fan on
 else
    previous = millis() 
</code></pre></li>
</ol>

<p>It would be more readable for a human, the result on the arduino would be similar.</p>
