Skip to main content
Code formatting.
Source Link
Edgar Bonet
  • 45.2k
  • 4
  • 42
  • 81
**************** 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 }

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); }

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);
}
**************** 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); }

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);
}
added 1330 characters in body
Source Link

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); }

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

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); }

Source Link

how to apply a timer to an analogWrite command for Arduino

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