Timing in code issue

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

[quote="kfccucumber, post:1, topic:1219992"]

Is this "for" correct?

for(int x = 0; x < 400; x++)
digitalWrite(stepPin,HIGH);
digitalWrite(stepPin,LOW);

@kfccucumber if you don't yet automatically enforce a good coding style as far as little formatting details is concerned, which make no never mind to the compiler but can mislead a person be she the author or the reader, you should get in the habit of using the IDE Auto Format tool.

It can make some errors obvious.

Here's some of your code after I did, and tweaked it a bit beyond what Auto Format does:


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

HTH

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.