So, what I'm trying to achieve is to turn off the motor A, wait for a second, and then turn on the motor B. What happens with the code above is that the motor A turns off with a one second delay and motor B turns on immediately after the motor A turned off? I can see that the motor A turns off with delay since my LED is fired properly by the ultrasonic sensor.
Follow up question, is there a max delay time? Any help much appreciated!
How does th evariable motorA control the motor? Simply setting a variable to false doesn't impact anything other than the variable. Also, please read Nick Gammon's "How to Use" post at the top of this Forum on the proper way to post code here using code tags. Also, you will see that there's really not enough code shown here for us to understand your problems.
You need to post a complete program to get any useful advice.
Unless the overall program is very simple you probably should not use delay() at all as it blocks the Arduino from doing other things. Have a look at how millis() is used to manage timing without blocking in several things at a time
And it is much easier to take the trouble to use millis() right from the start of developing a program compared to the work required to replace lots of delay()s with millis() when the program is nearly finished - but not performing properly.
OK, sorry again I thought my questions should be addressed in relevant forums even though they are related to the same project. Again, many apologies for sloppy start
may not produce the result you want because distance1 is an int, so dividing by a floating point number is going to truncate something. Also, if you want to divide a float by a float, the line should be:
distance1 = (int) ( (duration1 / 2.0) / 29.1);*
Now the parenthesized expression resolves to a float before casting to an int because you have used floating point constants, thus telling the compiler both data items should be floats. Also, what does pulseIn() do?
It's almost exactly the same as the way you use the clock on the wall in your kitchen. When something starts you save the value of millis() (make a note of the time on the clock) and at regular intervals you compare the latest value of millis() (the current time on your clock) with the value you saved. If the right amount of time has passed you know to do whatever (switch off the motor or take the chicken from the oven).
is really debugging code to help you see what's going on. If so, try this:
#define DEBUG // Put this as the very first line in your program
// the rest of your code up to:
#ifdef DEBUG
Serial.print("Sensor 1: ");
Serial.print(distance1);
Serial.print("cm ");
Serial.print("PWM A: ");
Serial.print(speed1);
#endif
// The remainder of your code...
Now suppose your code is stable and you don't need the debug Serial.print() calls. Rather than erase them, simply comment out the top line:
// #define DEBUG // Put this as the very first line in your program
and recompile your program. Because DEBUG is no longer defined, the debug calls to Serial.print() are no longer ciompiled into the program. However, if a bug appears down the road, uncomment the first line again, recompile, and the debug statements are back in the program. It's a lot easier than retyping the debug statements.
Wow! Thnx for this great tip! Yes, I used the Serial.print to see what's going on, compare the measures, etc. This is something I'd take out at the end but your tip of commenting it out is really awesome! Y'all are amazing!
bullmiletic:
OK, sorry again I thought my questions should be addressed in relevant forums even though they are related to the same project. Again, many apologies for sloppy start
I think I'll leave the question here. It is dissimilar enough to the other one to be OK in this place.
However you should not open multiple threads on virtually the same question in different places.
I'm back and in need for some help Namely, I have made the functions following the Robin2 code. They toggle the motors fine but the pause (the interval) between turning the motor A off and turning the motor B on doesn't work? BTW, even though the interval value is currently the same (5000), I want to be able to control these two intervals independently, i.e. switching from A to B motor may take longer than switching from B to A motor. I know I'm doing something wrong. This is the new code:
#define DEBUG
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousMotorA_Millis = 0;
unsigned long previousMotorB_Millis = 0;
const int motorAtoB_Interval = 5000;
const int motorBtoA_Interval = 5000;
const int potPin1 = A2;
const int potPin2 = A3;
const int ledPin1 = A4;
const int ledPin2 = A5;
// const int limitPin1 = 4;
// const int limitPin2 = 2;
const int trigPin1 = 5;
const int echoPin1 = 6;
const int trigPin2 = 10;
const int echoPin2 = 7;
int duration1 = 0;
int distance1 = 0;
int duration2 = 0;
int distance2 = 0;
int speed1 = 0;
int speed2 = 0;
byte motorA_State = HIGH;
byte motorB_State = LOW;
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// pinMode(limitPin1, INPUT);
// pinMode(limitPin2, INPUT);
// Setup Channel A
pinMode(12, OUTPUT); // Direction A pin
pinMode(3, OUTPUT); // PWM A pin
pinMode(9, OUTPUT); // Brake A pin
// Setup Channel B
pinMode(13, OUTPUT); // Direction B pin
pinMode(11, OUTPUT); // PWM B pin
pinMode(8, OUTPUT); // Brake B pin
}
void loop() {
currentMillis = millis();
updateMotorA_State();
updateMotorB_State();
if (motorA_State == HIGH){
digitalWrite(12, HIGH);
analogWrite(3, speed1);
}
if (motorA_State == LOW) {
analogWrite(3, 0);
}
if (motorB_State == HIGH){
digitalWrite(13, HIGH);
analogWrite(11, speed2);
}
if (motorB_State == LOW) {
analogWrite(11, 0);
}
#ifdef DEBUG
Serial.println("\n");
sensor1();
sensor2();
delay(1000);
#endif
}
void sensor1(){ // Sensor 1 function
digitalWrite (trigPin1, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin1, LOW);
duration1 = pulseIn (echoPin1, HIGH);
distance1 = (int) ( (duration1 / 2.0) / 29.1);
speed1 = analogRead (potPin1) / 4;
#ifdef DEBUG // Printing values for debuging purposes
Serial.print("Sensor 1: ");
Serial.print(distance1);
Serial.print("cm ");
Serial.print("PWM A: ");
Serial.print(speed1);
#endif
if (distance1 < 10) {
digitalWrite (ledPin1, HIGH);
motorA_State = LOW;
} else {
digitalWrite (ledPin1, LOW);
}
}
void sensor2(){ // Sensor 2 function.
digitalWrite (trigPin2, HIGH);
delayMicroseconds (10);
digitalWrite (trigPin2, LOW);
duration2 = pulseIn (echoPin2, HIGH);
distance2 = (int) ( (duration2 / 2.0) / 29.1);
speed2 = analogRead (potPin2) / 4;
#ifdef DEBUG // Printing values for debuging purposes
Serial.print(" Sensor 2: ");
Serial.print(distance2);
Serial.print("cm ");
Serial.print("PWM B: ");
Serial.print(speed2);
#endif
if (distance2 < 10) {
digitalWrite (ledPin2, HIGH);
motorB_State = LOW;
} else {
digitalWrite (ledPin2, LOW);
}
}
void updateMotorA_State() {
if (motorA_State == LOW) {
if (currentMillis - previousMotorA_Millis >= motorAtoB_Interval) {
motorB_State = HIGH;
previousMotorA_Millis += motorAtoB_Interval;
}
}
}
void updateMotorB_State() {
if (motorB_State == LOW) {
if (currentMillis - previousMotorB_Millis >= motorBtoA_Interval) {
motorA_State = HIGH;
previousMotorB_Millis += motorBtoA_Interval;
}
}
}
if (motorA_State == HIGH){
digitalWrite(12, HIGH);
analogWrite(3, speed1);
}
if (motorA_State == LOW) {
analogWrite(3, 0);
}
What other possible values can motorA_State contain? If the state isn't HIGH, is there ANY possibility that it won't be LOW?
but the pause (the interval) between turning the motor A off and turning the motor B on doesn't work?
What pause? Immediately after diddling with motor A, you start diddling with motor B. There is NO delay/pause/time elapsed, except for a few machine instruction cycles (at 62.5 nanoseconds each) between the motor A diddling and the motor B diddling.
Robin2, your suggestion unfortunately doesn't work Not only that there is still no pause between the switching off motor A and switching on motor B but switching off motor B and switching on motor A also stopped working. In other words, only one action happens ATM, which is switching off motor A and switching on motor B. As you know, motor A is automatically switched on by powering the circuit, i.e.
I'm really struggling to understand the concept of capturing the current millis and previous millis, i.e. how does it really work? Also, in the followup messages on your SeveralThingsAtTheSameTimeRev1.ino I came across CrossRoads's suggestion for using micros() instead of millis()? I know that this is not directly related to my pause issue but just curious what's your take on this? Again, super appreciative of your efforts in helping me out with backflipping into Arduino!