it did not help because my motor is off than turns on in 5 seconds turns of in another 5 seconds and repeats this. I want the motor to run for an amount than stop. I have wrote the code using that example. I see the relevance to my problem to some extent. I don t want it to turn on off. I want it to turn on right away and run for a period of time than turn off permanently.
const unsigned X_AXIS_PIN = 0;
const unsigned Y_AXIS_PIN = 1;
const unsigned Z_AXIS_PIN = 2;
const int motor1 = 3;
const int motor2 = 5;
const int motor3 = 10;
int motorState = LOW;
long previousMillis = 0;
long interval = 5000;
void setup()
{
Serial.begin(9600);
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(motor3, OUTPUT);
}
int x,
y,
z;
void loop()
{
Serial.print(analogRead(X_AXIS_PIN));
Serial.print(" ");
x = analogRead(X_AXIS_PIN);
Serial.print(analogRead(Y_AXIS_PIN));
Serial.print(" ");
y = analogRead(Y_AXIS_PIN);
Serial.println(analogRead(Z_AXIS_PIN));
z = analogRead(Z_AXIS_PIN);
delay(5000);
if( x < 700 && y < 700 && z < 700)
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
previousMillis = currentMillis;
if (motorState == LOW)
motorState = HIGH;
else
motorState = LOW ;
digitalWrite(motor1, motorState);
digitalWrite(motor2, motorState);
digitalWrite(motor3, motorState);
}
}