Beginner´s question ahead...
Hello, I want to control the on/off state of a 4 ch. relay module (velleman) with 4 potentiometers.
I found a code to control the 4 relays at the same time in a non-blocking fashion.
// Which pins are connected to which RELAY
const byte RELAY01 = 2;
const byte RELAY02 = 3;
const byte RELAY03 = 4;
const byte RELAY04 = 5;
// Time periods of blinks in milliseconds.
const unsigned long RELAY01interval = 1000;
const unsigned long RELAY02interval = 1000;
const unsigned long RELAY03interval = 1000;
const unsigned long RELAY04interval = 1000;
// Variable holding the timer value so far. One for each "Timer"
unsigned long RELAY01timer;
unsigned long RELAY02timer;
unsigned long RELAY03timer;
unsigned long RELAY04timer;
void setup ()
{
pinMode (RELAY01, OUTPUT);
pinMode (RELAY02, OUTPUT);
pinMode (RELAY03, OUTPUT);
pinMode (RELAY04, OUTPUT);
RELAY01timer = millis ();
RELAY02timer = millis ();
RELAY03timer = millis ();
RELAY04timer = millis ();
} // end of setup
void toggleRELAY01 ()
{
if (digitalRead (RELAY01) == LOW)
digitalWrite (RELAY01, HIGH);
else
digitalWrite (RELAY01, LOW);
// remember when we toggled it
RELAY01timer = millis ();
} // end of toggleRELAY01
void toggleRELAY02 ()
{
if (digitalRead (RELAY02) == LOW)
digitalWrite (RELAY02, HIGH);
else
digitalWrite (RELAY02, LOW);
// remember when we toggled it
RELAY02timer = millis ();
} // end of toggleRELAY02
void toggleRELAY03 ()
{
if (digitalRead (RELAY03) == LOW)
digitalWrite (RELAY03, HIGH);
else
digitalWrite (RELAY03, LOW);
// remember when we toggled it
RELAY03timer = millis ();
} // end of toggleRELAY03
void toggleRELAY04 ()
{
if (digitalRead (RELAY04) == LOW)
digitalWrite (RELAY04, HIGH);
else
digitalWrite (RELAY04, LOW);
// remember when we toggled it
RELAY04timer = millis ();
} // end of toggleRELAY04
void loop ()
{
// Handling the blink of one relay. The other Relays are controlled the same way. Repeat for more Relays
if ( (millis () - RELAY01timer) >= RELAY01interval)
toggleRELAY01 ();
if ( (millis () - RELAY02timer) >= RELAY02interval)
toggleRELAY02 ();
if ( (millis () - RELAY03timer) >= RELAY03interval)
toggleRELAY03 ();
if ( (millis () - RELAY04timer) >= RELAY04interval)
toggleRELAY04 ();
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */
} // end of loop
Now, instead of a set value of time period in blinking relays, I want to read that from analog input from 4 potentiometers. There is where I got stucked, I tried with analogread with no luck...maybe is very simple but I can´t find the way..
here is the code I have been using:
// Which pins are connected to which RELAY
const byte RELAY01 = 2;
const byte RELAY02 = 3;
const byte RELAY03 = 4;
const byte RELAY04 = 5;
// Time periods of blinks in milliseconds.
const unsigned long RELAY01interval = analogRead(A0);
const unsigned long RELAY02interval = analogRead(A1);
const unsigned long RELAY03interval = analogRead(A2);
const unsigned long RELAY04interval = analogRead(A3);
// Variable holding the timer value so far. One for each "Timer"
unsigned long RELAY01timer;
unsigned long RELAY02timer;
unsigned long RELAY03timer;
unsigned long RELAY04timer;
void setup ()
{
pinMode (RELAY01, OUTPUT);
pinMode (RELAY02, OUTPUT);
pinMode (RELAY03, OUTPUT);
pinMode (RELAY04, OUTPUT);
RELAY01timer = millis ();
RELAY02timer = millis ();
RELAY03timer = millis ();
RELAY04timer = millis ();
} // end of setup
void toggleRELAY01 ()
{
if (digitalRead (RELAY01) == LOW)
digitalWrite (RELAY01, HIGH);
else
digitalWrite (RELAY01, LOW);
// remember when we toggled it
RELAY01timer = millis ();
} // end of toggleRELAY01
void toggleRELAY02 ()
{
if (digitalRead (RELAY02) == LOW)
digitalWrite (RELAY02, HIGH);
else
digitalWrite (RELAY02, LOW);
// remember when we toggled it
RELAY02timer = millis ();
} // end of toggleRELAY02
void toggleRELAY03 ()
{
if (digitalRead (RELAY03) == LOW)
digitalWrite (RELAY03, HIGH);
else
digitalWrite (RELAY03, LOW);
// remember when we toggled it
RELAY03timer = millis ();
} // end of toggleRELAY03
void toggleRELAY04 ()
{
if (digitalRead (RELAY04) == LOW)
digitalWrite (RELAY04, HIGH);
else
digitalWrite (RELAY04, LOW);
// remember when we toggled it
RELAY04timer = millis ();
} // end of toggleRELAY04
void loop ()
{
// Handling the blink of one relay. The other Relays are controlled the same way. Repeat for more Relays
if ( (millis () - RELAY01timer) >= RELAY01interval)
toggleRELAY01 ();
if ( (millis () - RELAY02timer) >= RELAY02interval)
toggleRELAY02 ();
if ( (millis () - RELAY03timer) >= RELAY03interval)
toggleRELAY03 ();
if ( (millis () - RELAY04timer) >= RELAY04interval)
toggleRELAY04 ();
/* Other code that needs to execute goes here.
It will be called many thousand times per second because the above code
does not wait for the LED blink interval to finish. */
} // end of loop```