The sensor i use is infrared module. I would like to make the code to run the motor for 1.705s when the sensor detected the black line then stop. How can i make the program run the process during sensor detect the black line? Can I use "for loop" for millis?
Problem: When the sensor detected the black line, the motor continue to run and will not stop. Only when i made the sensor==0, millis() start counting 0 - 1705 then motor stop.
int OnTime = 1705; // milliseconds of on-time
int OffTime = 2000; // milliseconds of off-time
unsigned long currentTime;
unsigned long n;
#define M1_DIR 8
#define M1_PWM 9
void setup() {
pinMode(M1_DIR,OUTPUT);
pinMode(M1_PWM,OUTPUT);
pinMode(7,INPUT);
Serial.begin(9600);
}
void loop() {
int sensor=digitalRead(7);
n=millis()-currentTime;
Serial.print(n);
Serial.print(" ");
Serial.print(sensor);
Serial.print(" ");
Serial.println(millis());
if(sensor==1){ //anticlockwise
digitalWrite(M1_DIR, LOW);
analogWrite(M1_PWM, 255);
Serial.print("start");
currentTime = millis(); // Remember the time
}
if((millis()-currentTime>=OnTime)){ //stop
analogWrite(M1_PWM, 0);
Serial.print("stop");
}
}
You don't need a for loop, indeed you should avoid using for loops when timing things.
When the black line is detected save the value of millis() as the start time and set a boolean to true to flag that timing is taking place. Then, each time through loop() if the boolean is true subtract the start time from the current value of millis() and compare it with the required period. Once the period has elapsed set the boolean to false and take whatever other actions you require.
Note that there must be no blocking code in loop() such as whiles, for loops and certainly no delay()s
I have change some of the code below, from the serial monitor show the millis()-startingTime = value changes between 12 to 14. The period cannot elapses.
How can i fixed the value of "startingTime" after getting value from "millis()"?
int OnTime = 1705; // milliseconds of on-time
int OffTime = 2000; // milliseconds of off-time
unsigned long currentTime;
unsigned long startingTime=0;
unsigned long n;
bool motor;
#define M1_DIR 8
#define M1_PWM 9
void setup() {
pinMode(M1_DIR,OUTPUT);
pinMode(M1_PWM,OUTPUT);
pinMode(7,INPUT);
Serial.begin(9600);
}
void loop() {
int sensor=digitalRead(7);
Serial.print(startingTime);
Serial.print(" ");
Serial.print(millis()-startingTime);
Serial.print(" ");
Serial.print(sensor);
Serial.print(" ");
Serial.println(millis());
if(sensor==1){ //anticlockwise
startingTime=millis();
motor = true;
}
if(motor==true){
if(millis()-startingTime<=OnTime){ //true until the period elapses.
digitalWrite(M1_DIR, LOW);
analogWrite(M1_PWM, 255);
Serial.print("start");
Serial.print(" ");
}
else{
motor==false;
analogWrite(M1_PWM, 0);
Serial.print("stop");
Serial.print(" ");
}
}
}
Not positive I'm understanding exactly what you want. However, the way you have it coded now, startingTime is getting reset as long as the sensor is high. If I'm thinking correctly, you need to keep that from happening until your motor is finished doing what you want it to do. So
if (sensor == 1 && motor != true) { //anticlockwise
startingTime = millis();
motor = true;
}
And it appears you have an extra "=" in the line "motor == false" of the else statement
You only need to do this once when the timing period starts, not every time through loop(). Then all you need to test for is whether the period has elapsed and, if so, stop the motor