resistance()
[Simulation]
void resistance(unsigned short pin_from, unsigned short pin_to, float resistance, unsigned short flags = LINEAR);Description
Creates a resistance between the specified pins.
Parameters
pin_from: the Custom Element pin where the resistance
starts.
pin_to: the Custom Element pin where the resistance
ends.
resistance: the resistance value in ohms.
flags: NON_LINEAR or LINEAR to
set the resistance as non-linear or linear, respectively, this parameter
is optional.
Returns
Nothing.
Example Code
The code creates a resistance of 1k ohm between the pins 3 and 4, and creates a non-linear resistance between the pins 5 and 6 and increases the resistance value by 100 ohms every 100ms (resets at 1M ohm).
static unsigned long lastTime = 0;
static float r = 100;
void setup() {
pinLabel(3, "IN"); // sets the pin 3 label to "IN" (input by default)
pinLabel(4, "OUT"); // sets the pin 4 label to "OUT" (input by default)
pinLabel(5, "IN"); // sets the pin 5 label to "IN" (input by default)
pinLabel(6, "OUT"); // sets the pin 6 label to "OUT" (input by default)
resistance(3, 4, 1000); // creates a resistance of 1k ohm between the pins 3 and 4
resistance(5, 6, r, NON_LINEAR); // creates a non-linear resistance between the pins 5 and 6
}
void loop() {
if (millis() - lastTime >= 100) { // checks if the number of miliseconds is greater than 100
lastTime = millis();
r += 100; // increases the resistance value by 100 ohms
if (r >= 1000000) { // checks if the resistance value is greater than or equal to 1M ohm
r = 100;
}
resistance(5, 6, r); // updates the resistance value between the pins 5 and 6 (the flag is not needed)
}
}Notes and Warnings
- The
resistance()function can be used with all the pins, even for the power and ground pins. - The resistance value must be greater than 0 ohms.
- The
flagsparameter is optional, if not specified, the resistance will be set as linear. - If the resistance is set as linear, it can only be set during the setup.
- If the resistance is set as non-linear, it can be changed during the setup or the loop, but needs to be initialized during the setup.
- The
resistance()function can be used to create a resistance between any two pins, and won’t change the pin mode, so be careful when using it with the pins defined as outputs.
