2 tasks at the same time

Hi all, I'm trying to get my arduino duemilanuove to output a clock (ledpin high and low) while it is moving three steppers with the stepper library.

So this steppers are moving in sequence and I would like arduino to continuously output a usable clock, without waiting for the void loop to finnish each time.
here's my code:

#include <Stepper.h>

#define motorSteps 48 // change this depending on the number of steps
// per revolution of your motor
#define motorPin1 7
#define motorPin2 8
#define motorPin3 9
#define motorPin4 10
#define motorPin5 11
#define motorPin6 12

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2);
Stepper myStepper2(motorSteps, motorPin3,motorPin4);
Stepper myStepper3(motorSteps, motorPin5,motorPin6);
int ledPin = 1; // the number of the LED pin

void setup() {
// set the motor speed at 60 RPMS:
myStepper.setSpeed(120);
myStepper3.setSpeed(120);
myStepper2.setSpeed(120);
pinMode(ledPin, OUTPUT);

// Initialize the Serial port:
Serial.begin(9600);

}

void loop() {
// Step forward 100 steps:
Serial.println("Forward");
myStepper2.step(70);
myStepper3.step(70);
myStepper.step(70);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
}

i've tried this (in void loop) with the same results

void loop() {
// Step forward 100 steps:
Serial.println("Forward");
myStepper2.step(70);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
myStepper3.step(70);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
myStepper.step(70);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
digitalWrite(ledPin, HIGH); // set the LED on
delay(200); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(200);
}

Any help would be fantastic, thanks!

http://www.arduino.cc/playground/Code/TimedAction
Take a look at the ThreeExamplesAtOnce :slight_smile:

You should create a function for 'ticking the clock' (every second) and a function for positioning the servos.
Think incrementally, that is, instead of servo.step(10); do servo.step(1); ten times :slight_smile:

Can you explain again, in detail, what the current code does?
Then explain, in detail, what you want it to do :slight_smile:

As the code is, one could describe it like this:

Every half second, an LED should change its state and three steppers should 'tick' one step.

While writing that I noticed you use pin 1 as a led, and at the same time using it for Serial.
That will mess things up.
Try a pin from 2 to 19 :slight_smile:

-Description: This project is a puppet factory, the three steppers step in a row, like 100 steps each time, they move conveyor belts, and a mechanism, besides that, I'm using a CMOS configuration to trigger bends (sounds) on hacked toys and move dc motors through transistors,
it all works fine, when I supply the clock with an IC, but I need to get arduino to supply a usable clock, in order to be able to get it all working in sync,

what I want: at the same time the steppers are doing their thing, a clock is generated to move the sequencers etc..

right now I only get a very slow clock once every void loop, which is good for some motors but not for music

-I'm changing right now the ledpin to pin 4, I'll let u know if there is any change

Here it goes the code with more explanations:

#include <TimedAction.h>

#include <Stepper.h>
// initialize of the Stepper library: // using the 2-wire circuit from Tom Igoe so: 6 pins to move 3 steppers
;
#define motorSteps 48 // how many steps have the steppers
#define motorPin1 7
#define motorPin2 8
#define motorPin3 9 // which pins are motor pins
#define motorPin4 10
#define motorPin5 11
#define motorPin6 12

Stepper myStepper(motorSteps, motorPin1,motorPin2); // Describes the steppers and which pins are used for each one of them
Stepper myStepper2(motorSteps, motorPin3,motorPin4);
Stepper myStepper3(motorSteps, motorPin5,motorPin6);

TimedAction blinkAction = TimedAction(100,blink);
TimedAction stepAction = TimedAction(NO_PREDELAY,500,stepper); // copying your example I put No predelay and without it, no difference the word
//stepaction I chose it, and stepper worked fine(it's a function of stepper lib.)
//I tried to use the step function but the word changed color and could'nt compile

//pin / state variables
#define ledPin 4 // changed to pin num.4
boolean ledState = false;

void setup() {

pinMode(ledPin,OUTPUT);
digitalWrite(ledPin,ledState);

myStepper.setSpeed(120);//sets the speed of the steppers
myStepper3.setSpeed(120);//idem
myStepper2.setSpeed(120);//idem

// Initialize the Serial port:
Serial.begin(9600);

}

void loop() {
// Step forward 100 steps:
blinkAction.check();
stepAction.check(); //timedaction protocol

}

void blink(){
ledState ? ledState=false : ledState=true;
digitalWrite(ledPin,ledState);
}
void stepper(){ //kind of copying the void blink, using the word stepper here and in timedaction worked fine and could compile
Serial.println("Forward");

myStepper2.step(1); //(x70 times)
//step is a function of the stepper library, tells each stepper to move one step (or 100 or whatever)
// I think I don't understand completely what I am telling arduino to do,I thought the timedaction check command would be retriggering
// all the time at the rate its established (TimedAction stepAction = TimedAction(NO_PREDELAY,500,stepper); ) maybe the fact that its
// three steppers one after the other one makes void blink have to wait?
//essentially here the steppers are told to advance one step every time in a row, don't know when voidblink is retriggering.
myStepper3.step(1); //(x 70 times)
myStepper.step(1); //(x70 times)

}

Please, when you're posting code, use the "#" (Code) button on the posting editor.
My scrolling finger hurts.

Ok I'll use the code Button. (didn't know)

Changing to pin 4 didn't change anything, every void loop changes state.

In fact what I want is: every half a second an led should change its state, and one stepper moves one step till it has moved say, 70 steps, then the led continues to change state every half second and the second stepper moves one step till 70 steps and then the third one.

I hope thats clearer

Uri

Try this loop:

void loop() {
  // Step forward 100 steps:
  blinkAction.check();
  stepAction.check();    //timedaction  protocol
  Serial.println("looping");
}

I suspect that you will find that the led does not blink at the end of the loop. But rather (approx.) every 100 milliseconds. ? :slight_smile:

It doesn't do it, if it's meant to be done using pin 1, the thing with pin 1 is that it doesn't get completely off in the low state.

Anyway, through pin four, its doing its thing if the stepping action its short. Personally, I'd prefer to be able to run each stepper for a longer
length, but that would imply a longer stepping timedaction,
With this code It works great but I have to restrict the lenght of the stepping beacause if the actions overlap then the blinking is delayed.
I also separated a stepper into another timedaction

[#include <TimedAction.h>

#include <Stepper.h>
// initialize of the Stepper library:
;
#define motorSteps 48     // change this depending on the number of steps
                           // per revolution of your motor
#define motorPin1 7
#define motorPin2 8
#define motorPin3 9
#define motorPin4 10
#define motorPin5 11
#define motorPin6 12


Stepper myStepper(motorSteps, motorPin1,motorPin2); 
Stepper myStepper2(motorSteps, motorPin3,motorPin4); 
Stepper myStepper3(motorSteps, motorPin5,motorPin6); 



TimedAction blinkAction = TimedAction(500,blink);
TimedAction stepAction = TimedAction(NO_PREDELAY,2000,stepper); 
TimedAction stepAction2 = TimedAction(NO_PREDELAY,2000,stepper2);

//pin / state variables
#define ledPin 4
boolean ledState = false;




void setup() {
  // set the motor speed at 60 RPMS:
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin,ledState);

 
  myStepper.setSpeed(180);
  myStepper3.setSpeed(240);
 myStepper2.setSpeed(180);

 
  // Initialize the Serial port:
  Serial.begin(9600);


}

void loop() {

  blinkAction.check();
stepAction.check();
stepAction2.check();
Serial.println("looping");
}
 
void blink(){
  ledState ? ledState=false : ledState=true;
  digitalWrite(ledPin,ledState);
 }
void stepper(){
Serial.println("Forward");
 myStepper3.step(40);
 myStepper3.step(-40);
  }

void stepper2(){
Serial.println("Forward"); 

  myStepper2.step(30);
 myStepper.step(30);

 
   }code]

[/code]
Maybe if we could count the times the stepaction is made, and using an if statement, then swap to the next stepper and so on.
that's beyond my knowledge though,
I'll research a bit of math.

Thanx a lot!!