Hey guys,
I have a (what I believe to be) simple code here where I am try to use millis() to my advantage. The loop area is where I am having my issue on the 1st if statement. I was trying to see if it is even possible to have the variable "T1" = millis() while continuing to equal millis() after the if statement is true. Then in another if statement, have that variable become a constant after that new if statement becomes true. So, to have it loop like that would be great. I might be approaching this in the wrong way so any advice would be great.
int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 4;
int motor2pin2 = 6;
int EN1 = 9;
int EN2 = 10;
int sensor = 12;
unsigned long T1;
unsigned long T5 = 5000;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(sensor, INPUT);
}
void loop() {
if (digitalRead(sensor) == LOW)
{
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(EN1, HIGH);
delay(3000);
T1 = millis();
T5 = T1 + 5000;
}
if (T5 < T1)
{
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
digitalWrite(EN2, HIGH);
delay(3000);
T1 = 0;
T5 = 5000;
}
if (digitalRead(sensor) == HIGH)
{
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
}
Serial.println(T1);
Serial.println(T5);
delay(1000);
}
Once you have set T1 equal to millis() it will retain the value given to it unless/until you explicitly change it. The value of T1 will not continue to update to equal the current value of millis(). If you need to now the value of millis() later in the program then simply call the function again
Please describe in English what your sketch is intended to do
I'll try and describe it as well as I can. The sketch is supposed to move a motor (motor 2) after an adjustable period of time from when another motor (motor 1) is activated by a sensor. I want the timer on the motor 2 to be interrupted and reset if the motor 1 activates before that timer for motor 2 reaches its end (somewhere around 15-30 seconds) so, if motor one continues to activate before motor 2's timer activates, motor 2 will not activate.
I would draw one but it would be my first one ever drawn and might contain ambiguity as well. Sorry I could try if it comes to it. I'll do some research but let me try to explain it again in a more clear way.
I am trying to get a sensor to initialize a loop. Everything is idle until that sensor is activated. Once that sensor is activated, a loop begins. Initially a motor 1 is activated once the sensor is activated and is turned clockwise for 3 seconds. After the 3 seconds of motor 1 running, a 30 second timer is started which will activate a 2nd motor. If the sensor is activated within that timer's 30 seconds, the timer will reset. Once the timer reaches 30 seconds without interruption from the sensor, the 2nd motor will activate and the timer will cease to exist. The timer will only come up again if the sensor is activated.
So, what my serial monitor is showing me is that the 1st if statement is giving T1 the current millis() time and T5 the current millis() time + 5000. I need the T1 to continue to be represented as a millis() time or continuing to grow and the T5 to be represented as the current millis() time + 5000 and not continuing to grow.
I know my error could be corrected if I leave T1= millis() out of the if statement but inside of the loop statement. Unfortunately this creates a new issue. T1 at one point will always be higher than T5 so, my 2nd if statement will always be activated and even if I put inside of the 2nd if statement for T1 to be 0, the original T1=millis() statement that is now inside of the loop will go back to being the current millis() time.
Mixing millis() and delay() won't work out for you.
Don't be intimidate by lofty jargon like "state diagram".
Google
Arduino blink without delay
And
Arduino two things at once
And
Arduino finite state machine
And
Arduino finite state machine traffic lights
Slow your roll and settle in for a bit of the old learning curve. It is hard until it is easy, so this first project might take a bit longer to finish.
But you will be on much more solid ground for everything you do with the Arduino in the future.
I finally got it. I just had to make the T1 increase in increments. I think a "for" statement would have been better fit but I've manipulated it to work exactly how I wanted it to. Thanks guys for making me think outside the box.
int motor1pin1 = 2;
int motor1pin2 = 3;
int motor2pin1 = 4;
int motor2pin2 = 6;
int EN1 = 9;
int EN2 = 10;
int sensor = 12;
int x = 0;
unsigned long T1;
unsigned long T5 = 3000;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(motor1pin1, OUTPUT);
pinMode(motor1pin2, OUTPUT);
pinMode(motor2pin1, OUTPUT);
pinMode(motor2pin2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(sensor, INPUT);
}
void loop() {
if (digitalRead(sensor) == LOW)
{
digitalWrite(motor1pin1, HIGH);
digitalWrite(motor1pin2, LOW);
digitalWrite(EN1, HIGH);
delay(3000);
T1 = 0;
T5 = 3000;
}
if (T5 < T1)
{
digitalWrite(motor2pin1, HIGH);
digitalWrite(motor2pin2, LOW);
digitalWrite(EN2, HIGH);
delay(3000);
T1 = 0;
x = 0;
T5 = 500000;
digitalWrite(motor2pin1, LOW);
digitalWrite(motor2pin2, LOW);
}
if (digitalRead(sensor) == HIGH)
{
digitalWrite(motor1pin1, LOW);
digitalWrite(motor1pin2, LOW);
T1 = x++;
}
Serial.println(T1);
Serial.println(T5);
}
Nice. I never argue with success. You happy, good.
You never set EN1 or EN2 to anything but HIGH. What is the roll of these signals?
You never set either motor1pin2 or motor2pin to anything but LOW. What are the 1 and 2 motor pins for?
Is
T5 = 500000;
just a way for you to say "forever"? It would take a long time, longer than you've probably tested your program, but sooner or later T1 would catch up.
What is your actual sensor? What does mean HIGH and LOW with the sensor? Are you convinced that the sensor will not be in a state that allows T1 to reach 500000?
What kind of activity does the sensor create? As in how frequent and how long are the active signals from the sensor?
TBH I can't make heads or tails of your logic and I cannot relate it to your prose specifications.
Playing with the code I find it to be dependent for timing on the serial baud rate… and very fragile when I tried to reform it into a logical structure that would clarify along common patterns the actions you want the system to take.
So, good if it does exactly what you want. But please now take a moment away from any project and do some reading about how something like this is done in a less personal and creative way - you will find that your approach, successful this time, will not be a good way to accomplish anything more than a bit more complicated.
And doing things that are from "the book" will always make it easier for ppl to help, not just easier on you.