Codice Arduino (Soglia analogica)
// constants won't change
const int POTENTIOMETER_PIN = A0; // Arduino pin connected to Potentiometer pin
const int RELAY_PIN = 3; // Arduino pin connected to Relay's pin
const int ANALOG_THRESHOLD = 500;
void setup() {
pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
}
void loop() {
int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin
if(analogValue > ANALOG_THRESHOLD)
digitalWrite(RELAY_PIN, HIGH); // turn on Relay
else
digitalWrite(RELAY_PIN, LOW); // turn off Relay
}
Soglia di tensione
// constants won't change
const int POTENTIOMETER_PIN = A0; // Arduino pin connected to Potentiometer pin
const int RELAY_PIN = 3; // Arduino pin connected to Relay's pin
const float VOLTAGE_THRESHOLD = 2.5; // Voltages
void setup() {
pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode
}
void loop() {
int analogValue = analogRead(POTENTIOMETER_PIN); // read the input on analog pin
float voltage = floatMap(analogValue, 0, 1023, 0, 5); // Rescale to potentiometer's voltage
if(voltage > VOLTAGE_THRESHOLD)
digitalWrite(RELAY_PIN, HIGH); // turn on Relay
else
digitalWrite(RELAY_PIN, LOW); // turn off Relay
}
float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}