Pages: [1]   Go Down
Author Topic: How to replace two diffrent delay() functions (total of four) with millis()  (Read 389 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is what I have so far.


Code:
// give the motor control pins names:
const int PMWA = 3;
const int PMWB = 11;
const int BRAKEA = 9;
const int BRAKEB = 8;
const int DIRA = 12;
const int DIRB = 13;

unsigned long currentTime;
unsigned long loopTime;

void setup() {

//Setup Channel A
pinMode(DIRA, OUTPUT); //Initiates Motor Channel A pin
pinMode(BRAKEA, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B
pinMode(DIRB, OUTPUT); //Initiates Motor Channel A pin
pinMode(BRAKEB, OUTPUT); //Initiates Brake Channel A pin
currentTime = millis();
loopTime = currentTime;

}

void loop(){


//Motor A forward @ full speed
digitalWrite(DIRA, HIGH); //Establishes forward DIRection of Channel A
digitalWrite(BRAKEA, LOW); //Disengage the Brake for Channel A
analogWrite(3, 1023); //Spins the motor on Channel A at full speed - THIS LINE IS INCORRECT - SHOULD BE LIKE NEXT LINE
analogWrite(3, 255); //Spins the motor on channel A at full speed


//Motor B backward @ half speed
digitalWrite(DIRB, LOW); //Establishes backward DIRection of Channel B
digitalWrite(BRAKEB, LOW); //Disengage the Brake for Channel B
analogWrite(11, 512); //Spins the motor on Channel B at half speed - THIS LINE IS INCORRECT - SHOULD BE LIKE NEXT LINE
analogWrite(11, 128);

delay(3000);


digitalWrite(BRAKEA, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B
// the BRAKEB line above is also incorrect, it should refer to pin 8 - should be:
digitalWrite(BRAKEB, HIGH);//Engage the Brac


delay(1000);


//Motor A forward @ full speed
digitalWrite(DIRA, LOW); //Establishes backward DIRection of Channel A
digitalWrite(BRAKEA, LOW); //Disengage the Brake for Channel A
analogWrite(3, 128); //Spins the motor on Channel A at half speed

//Motor B forward @ full speed
digitalWrite(DIRB, HIGH); //Establishes forward DIRection of Channel B
digitalWrite(BRAKEB, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed


delay(3000);


digitalWrite(BRAKEA, HIGH); //Engage the Brake for Channel A
//digitalWrite(9, HIGH); //Engage the Brake for Channel B
//the BRAKEB line above is also incorrect, it should refer to pin 8 - should be:
digitalWrite(BRAKEB, HIGH);


delay(1000);

}


As you can see I have unsigned long currentTime;
                                 unsigned long loopTime;
and underneath the void setup the currentTime = millis();
                                              and loopTime = currentTime;.
To replace the four delay(), do I replace each delay() function with                currentTime = millis();
if(currentTime >= (loopTime + 1000)).

Thanks for the help.






Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46149
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
analogWrite(3, 1023); //Spins the motor on Channel A at full speed - THIS LINE IS INCORRECT - SHOULD BE LIKE NEXT LINE
analogWrite(3, 255); //Spins the motor on channel A at full speed
Then, why don't you fix it?

Before replacing the delay() calls, explain why you want to do that.

If you understand that calling millis() is like looking at your watch, and that the if test is comparing now with then, it is pretty easy to understand how to remove all the delay() calls.

Knowing what to do if now minus then is greater than some interval is the tricky part.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code is not mine and it was lot worse. I didn't see that thanks.
Logged

Offline Offline
Sr. Member
****
Karma: 0
Posts: 339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As i said on the other post start studying similar project codes, cause first you will need to understand where you will have more problems with time keeping( ill leave you one, you will have to research for others if needed)
http://obscurereality.org/?p=45
He has a nice simple-ish code for you to start understanding.
Implement it to whatever way you want and fell more comfortable with.
But i can tell you will have a hell of a hard time if you expect anyone to do it all for you, lol.
Maybe start with a block diagram of the logic you will want it to perform

Logged

10 LET Loop=Infinite
20 GO TO 10

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help.
Logged

Pages: [1]   Go Up
Jump to: