I wish the switch to rapidly open when the R2 resistor is added to the circuit. But I need the digitalwrite high signal to remain constant on the EN connection of the stepper motor for 20 seconds to keep the stepper motor stopped. The switch works on the attached code, but the HIGH from D6 is not working. Also, I eventually will need a digitalwrite HIGH from D9 to reverse my steeper motor for about 30 seconds after the 20-minute digitalwrite HIGH from D6 ends.
Here is my setup:
```cpp
const int sensorPin = A0; // Analog input pin
const int MOSPin = 2;
const int enPin = 8;
const int dirPin = 9;
const int stepPin = 10;
const int sensorPinb = A5; // Replace with the actual pin number for your sensor
const int outputPinb = 6; // Replace with the actual pin number for your output
unsigned long startTimeb;
unsigned long durationb = 20000; // 20 seconds in milliseconds
int sensorValue = 0; // sensorPin default value
float Vin = 5; // Input voltage
float Vout = 0; // Vout default value
float Rref = 1000000000; // Reference resistor's value in ohms (you can give this value in kiloohms or megaohms - the resistance of the tested resistor will be given in the same units)
float R = 0; // Tested resistors default value
void setup()
{
Serial.begin(9600); // Initialize serial communications at 9600 bps
pinMode(stepPin,OUTPUT);
pinMode(MOSPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
pinMode(sensorPinb, INPUT);
pinMode(outputPinb, OUTPUT);
digitalWrite(outputPinb, LOW); // Ensure the output pin is initially LOW
startTimeb = 0;
}
void loop () {
digitalWrite(dirPin,HIGH); // put your main code here, to run repeatedly:
sensorValue = analogRead(sensorPin); // Read Vout on analog input pin A0 (Arduino can sense from 0-1023, 1023 is 5V)
if (sensorValue < 800){
digitalWrite(enPin, HIGH);
digitalWrite(MOSPin, HIGH);}
if (sensorValue > 800){
digitalWrite(enPin, LOW);
digitalWrite(MOSPin, LOW);}
for(int x = 0; x < 400; x++)
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin,LOW);
if (digitalRead(sensorPinb) == HIGH) {
// If yes, set the output pin to HIGH and record the start time
digitalWrite(outputPinb, HIGH);
startTimeb = millis();
}
// Check if the desired duration has elapsed
if (digitalRead(outputPinb) == HIGH && millis() - startTimeb >= durationb) {
// If yes, set the output pin back to LOW
digitalWrite(outputPinb, LOW);
}
}
