UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« on: December 13, 2012, 10:34:31 am » |
1. I want to add this code into my existing code but with out delay() 2. Or can I use multiple loops? like; void loop() void loop1() void loop2() Also, can I trigger each loop to start and stop independently with diffident delays in each but also let others run? int delaylegnth = 1000;
void setup() { //establish motor direction toggle pins pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards??? pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards??? //establish motor brake pins pinMode(9, OUTPUT); //brake (disable) CH A pinMode(8, OUTPUT); //brake (disable) CH B
}
void loop(){ digitalWrite(9, LOW); //ENABLE CH A digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, HIGH); //Sets direction of CH A analogWrite(3, 255); //Moves CH A delay(delaylegnth); digitalWrite(9, HIGH); //DISABLE CH A digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, LOW); //Sets direction of CH B analogWrite(11, 255); //Moves CH B delay(delaylegnth); digitalWrite(9, LOW); //ENABLE CH A digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, LOW); //Sets direction of CH A analogWrite(3, 255); //Moves CH A delay(delaylegnth); digitalWrite(9, HIGH); //DISABLE CH A digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, HIGH); //Sets direction of CH B analogWrite(11, 255); //Moves CH B delay(delaylegnth);
}
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35518
Seattle, WA USA
|
 |
« Reply #1 on: December 13, 2012, 10:46:14 am » |
1. I want to add this code into my existing code but with out delay() You need to wait until the stepper has moved before you tell it to step again. How do you propose to do that? 2. Or can I use multiple loops? like; void loop() void loop1() void loop2() Sure, but you will need to call the others from inside loop(). Also, can I trigger each loop to start and stop independently with diffident delays in each but also let others run? No. You need to read, understand, and embrace the blink without delay example.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 71
Posts: 6624
Arduino rocks
|
 |
« Reply #2 on: December 13, 2012, 10:47:56 am » |
You need to use the technique exemplified in the BlinkWithoutDelay example sketch - ie avoid using delay completely.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« Reply #3 on: December 13, 2012, 10:59:39 am » |
Brilliant! Thanks chaps
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« Reply #4 on: December 13, 2012, 12:42:14 pm » |
Using an Mega2560 with Arduino Motor ShieldV3 I have a 8 wire stepper motor and all working fine with this code, http://docs-europe.electrocomponents.com/webdocs/001c/0900766b8001c018.pdfint delaylegnth = 60;
void setup() { pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards??? pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards??? pinMode(9, OUTPUT); //brake (disable) CH A pinMode(8, OUTPUT); //brake (disable) CH B }
void loop(){ digitalWrite(9, LOW); //ENABLE CH A digitalWrite(8, HIGH); //DISABLE CH B digitalWrite(12, HIGH); //Sets direction of CH A analogWrite(3, 255); //Moves CH A delay(delaylegnth); digitalWrite(9, HIGH); //DISABLE CH A digitalWrite(8, LOW); //ENABLE CH B digitalWrite(13, LOW); //Sets direction of CH B analogWrite(11, 255); //Moves CH B delay(delaylegnth); digitalWrite(9, LOW); //ENABLE CH A digitalWrite(8, HIGH); //DISABLE CH B digitalWrite(12, LOW); //Sets direction of CH A analogWrite(3, 255); //Moves CH A delay(delaylegnth); digitalWrite(9, HIGH); //DISABLE CH A digitalWrite(8, LOW); //ENABLE CH B digitalWrite(13, HIGH); //Sets direction of CH B analogWrite(11, 255); //Moves CH B delay(delaylegnth); }
Trying the millies() and not having much luck. int delaylegnth = 1000; long previousMillis = 0; long interval = 1000;
void setup() { pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards??? pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards??? pinMode(9, OUTPUT); //brake (disable) CH A pinMode(8, OUTPUT); //brake (disable) CH B } void loop(){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; } digitalWrite(9, LOW); //ENABLE CH A digitalWrite(8, HIGH); //DISABLE CH B digitalWrite(12, HIGH); //Sets direction of CH A analogWrite(3, 255); //Moves CH A millis(); digitalWrite(9, HIGH); //DISABLE CH A digitalWrite(8, LOW); //ENABLE CH B digitalWrite(13, LOW); //Sets direction of CH B analogWrite(11, 255); //Moves CH B millis(); digitalWrite(9, LOW); //ENABLE CH A digitalWrite(8, HIGH); //DISABLE CH B digitalWrite(12, LOW); //Sets direction of CH A analogWrite(3, 255); //Moves CH A millis(); digitalWrite(9, HIGH); //DISABLE CH A digitalWrite(8, LOW); //ENABLE CH B digitalWrite(13, HIGH); //Sets direction of CH B analogWrite(11, 255); //Moves CH B millis(); } What have I not done?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35518
Seattle, WA USA
|
 |
« Reply #5 on: December 13, 2012, 12:58:59 pm » |
if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; }
This is where you also need to take another step. millis(); Call a function to get the time. Discard the time that you just got. Doesn't seem very useful to me. Somewhere above, I said you needed to read, understand, and embrace the blink without delay example. You seem to have managed only one of those three tasks. You need a step() function that causes the stepper motor to take a step. That function needs to know which step it is at, so it can bang the right set of pins to make the next step happen. The rest of the code should NOT know that. Hint: a static variable in the function. You need to call that method when the required amount of time, since the last step, has elapsed. That function needs to record when the last step happened.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« Reply #6 on: December 13, 2012, 01:14:22 pm » |
Could you please give me an example Paul? I need to see the full code to work it out so I can understand it and learn more you.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35518
Seattle, WA USA
|
 |
« Reply #7 on: December 13, 2012, 01:19:41 pm » |
Could you please give me an example Paul? if(currentMillis - previousMillis > interval) { // Moved down here where it belongs step(); previousMillis = currentMillis; // indented properly } void step() { static int lastStep = 0; switch(lastStep) { case 0: // First step code break; case 1: // Second step code break; case 2: // Third step code break; case 3: // Fourth step code break; } lastStep++; if(lastStep > 3) lastStep = 0; } You already have the first, second, third, and fourth step code. First step: digitalWrite(9, LOW); //ENABLE CH A digitalWrite(8, HIGH); //DISABLE CH B digitalWrite(12, HIGH); //Sets direction of CH A analogWrite(3, 255); //Moves CH A You can find and move other three step stuff.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« Reply #8 on: December 13, 2012, 01:28:25 pm » |
Brilliant, thank you Paul. I'll return the full code in a sec for you moderation
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« Reply #9 on: December 13, 2012, 01:38:00 pm » |
And here it is! It works perfectly, thanks. int delaylegnth = 1000; long previousMillis = 0; long interval = 2;
void setup() { pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards??? pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards??? pinMode(9, OUTPUT); //brake (disable) CH A pinMode(8, OUTPUT); //brake (disable) CH B } void step() { static int lastStep = 0; switch(lastStep) { case 0: digitalWrite(9, LOW); digitalWrite(8, HIGH); digitalWrite(12, HIGH); analogWrite(3, 255); break; case 1: digitalWrite(9, HIGH); digitalWrite(8, LOW); digitalWrite(13, LOW); analogWrite(11, 255); break; case 2: digitalWrite(9, LOW); digitalWrite(8, HIGH); digitalWrite(12, LOW); analogWrite(3, 255); break; case 3: digitalWrite(9, HIGH); digitalWrite(8, LOW); digitalWrite(13, HIGH); analogWrite(11, 255); break; } lastStep++; if(lastStep > 3) lastStep = 0; } void loop(){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { // Moved down here where it belongs: Got ya. step(); previousMillis = currentMillis; // indented properly: Thanks Paul } }
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« Reply #10 on: December 13, 2012, 02:06:04 pm » |
Question? How dose myDelay work?
Would it be the same as delay();
so would,
delay(1000); be the same as, myDelay(1000);
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« Reply #11 on: December 13, 2012, 02:13:38 pm » |
I have now added an LED just see how the code would look. How would I blink the LED in diffident intervals to the speed of the stepper motor, I have try'd but I'm stumped... here is the same code but what I have try'd. const int ledPin = 40; int ledState = LOW; int delaylegnth = 1000; long previousMillis = 0; long interval1 = 30; long interval2 = 30; void setup() { pinMode(ledPin, OUTPUT); pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards??? pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards??? pinMode(9, OUTPUT); //brake (disable) CH A pinMode(8, OUTPUT); //brake (disable) CH B } void step() { static int lastStep = 0; switch(lastStep) { case 0: digitalWrite(9, LOW); digitalWrite(8, HIGH); digitalWrite(12, HIGH); analogWrite(3, 255); break; case 1: digitalWrite(9, HIGH); digitalWrite(8, LOW); digitalWrite(13, LOW); analogWrite(11, 255); break; case 2: digitalWrite(9, LOW); digitalWrite(8, HIGH); digitalWrite(12, LOW); analogWrite(3, 255); break; case 3: digitalWrite(9, HIGH); digitalWrite(8, LOW); digitalWrite(13, HIGH); analogWrite(11, 255); break; } lastStep++; if(lastStep > 3) lastStep = 0; } void loop(){ unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval1) if(currentMillis - previousMillis > interval2) { // Moved down here where it belongs: Got ya. step(); previousMillis = currentMillis; // indented properly: Thanks Paul if (ledState == LOW) ledState = HIGH; else ledState = LOW;
// set the LED with the ledState of the variable: digitalWrite(ledPin, ledState); } } Thanks,
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35518
Seattle, WA USA
|
 |
« Reply #12 on: December 13, 2012, 03:50:41 pm » |
if(currentMillis - previousMillis > interval1) if(currentMillis - previousMillis > interval2)
If you ALWAYS used curly braces after an if statement, you'd see the problem.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Jr. Member
Karma: 0
Posts: 75
Adam
|
 |
« Reply #13 on: December 14, 2012, 04:12:06 am » |
As in, if(currentMillis - previousMillis > interval1); if(currentMillis - previousMillis > interval2); ?? 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 314
Posts: 35518
Seattle, WA USA
|
 |
« Reply #14 on: December 14, 2012, 10:47:00 am » |
These are curly braces: { and } This is not: ;
|
|
|
|
|
Logged
|
|
|
|
|
|