Go Down

Topic: Help combining 2 sketches so they run at once (Read 797 times) previous topic - next topic

dannyc09

I'm trying to combine the following sketches:

Code: [Select]
int outPin = 1;              // output on digital pin 1

void setup() {
pinMode(outPin, OUTPUT);
Serial.begin(38400);
}

void loop()
{

Serial.write('t');          // lowercase "t" must be in single quotes…
Serial.write(001);           // followed by the track number without quotes
delay(10000);

Serial.write('t');
Serial.write(002);
delay(9000);

Serial.write('t');
Serial.write(006);
delay(15000);

Serial.write('t');
Serial.write(003);
delay(5000);

Serial.write('t');
Serial.write(004);
delay(10000);

Serial.write('t');
Serial.write(005);
delay(9000);

Serial.write('t');
Serial.write(006);
delay(8000);

Serial.write('t');
Serial.write(007);
delay(6000);

Serial.write('t');
Serial.write(8); 
delay(9000);

Serial.write('t');
Serial.write(9); 
delay(6000);

Serial.write('t');
Serial.write(010);
delay(11000);

Serial.write('t');
Serial.write(18);
delay(15000);

Serial.write('t');
Serial.write(011);
delay(10000);

Serial.write('t');
Serial.write(012);
delay(12000);

Serial.write('t');
Serial.write(013);
delay(8000);

Serial.write('t');
Serial.write(014);
delay(9000);

Serial.write('t');
Serial.write(015);
delay(11000);

Serial.write('t');   
Serial.write(016);
delay(7000);

Serial.write('t');
Serial.write(017);
delay(6000);

Serial.write('t');
Serial.write(18);
delay(10000);

Serial.write('t');
Serial.write(006);
delay(12000);

Serial.write('t');
Serial.write(19);
delay(9000);

Serial.write('t');
Serial.write(18);
delay(30000);

}


Code: [Select]

void setup() {
 
  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
 
}

void loop(){
 
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(200);
 
  digitalWrite(9, HIGH); //Engage the Brake for Channel A

  delay(200);
 
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Left
 
  delay(400);
 
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(200);
 
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(400);
 
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(10000);          //Pause
 
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Left
 
  delay(400);
 
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(4000);
 
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(400);

  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(8000);

  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(1000);

  digitalWrite(9, HIGH); //Engage the Brake for Channel A
   
  digitalWrite(12, LOW); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Left
 
  delay(2000);

  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(2000);
 
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(1000);

  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(20000);
}


I'm using a sparkfun mp3 trigger for the first code and the official motor control shield for the second.  I've followed a tutorial and got this so far:

Code: [Select]
void setup(){
   setupMotor();
   setupSound();
  }
  void loop(){
    loopMotor();
    loopSound();
  }

void setupMotor() {

  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
 
}

void loopMotor(){
 
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(200);
 
  digitalWrite(9, HIGH); //Engage the Brake for Channel A

  delay(200);
 
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Left
 
  delay(400);
 
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(200);
 
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(400);
 
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(10000);          //Pause
 
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Left
 
  delay(400);
 
  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(4000);
 
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(400);

  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(8000);

  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(1000);

  digitalWrite(9, HIGH); //Engage the Brake for Channel A
   
  digitalWrite(12, LOW); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Left
 
  delay(2000);

  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(2000);
 
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins Right
 
  delay(1000);

  digitalWrite(9, HIGH); //Engage the Brake for Channel A
 
  delay(20000);
}

int outPin = 1;              // output on digital pin 1

void setupSound() {
pinMode(outPin, OUTPUT);
Serial.begin(38400);
}

void loopSound()
{

Serial.write('t');          // lowercase "t" must be in single quotes…
Serial.write(001);           // followed by the track number without quotes
delay(10000);

Serial.write('t');
Serial.write(002);
delay(8000);

Serial.write('t');
Serial.write(003);
delay(5000);

Serial.write('t');
Serial.write(004);
delay(10000);

Serial.write('t');
Serial.write(005);
delay(9000);

Serial.write('t');
Serial.write(006);
delay(8000);

Serial.write('t');
Serial.write(007);
delay(6000);

Serial.write('t');
Serial.write(8); 
delay(7000);

Serial.write('t');
Serial.write(9); 
delay(6000);

Serial.write('t');
Serial.write(010);
delay(11000);

Serial.write('t');
Serial.write(011);
delay(10000);

Serial.write('t');
Serial.write(012);
delay(12000);

Serial.write('t');
Serial.write(013);
delay(5000);

Serial.write('t');
Serial.write(014);
delay(4000);

Serial.write('t');
Serial.write(015);
delay(7000);

Serial.write('t');   
Serial.write(016);
delay(4000);

Serial.write('t');
Serial.write(017);
delay(6000);

Serial.write('t');
Serial.write(18);
delay(10000);

Serial.write('t');
Serial.write(19);
delay(6000);

Serial.write('t');
Serial.write(005);
delay(6000);

Serial.write('t');
Serial.write(021);
delay(6000);

Serial.write('t');
Serial.write(022);
delay(6000);

Serial.write('t');
Serial.write(023);
delay(5000);

Serial.write('t');
Serial.write(024);
delay(6000);

Serial.write('t');
Serial.write(025);
delay(30000);


}


However I can't seem to get them both running at the same time.  Please help!

BetaDelta

Writing a flow chart of your final sketch may help you see what you need to happen when. Also, putting different pieces of code into functions will help you write the code using the flowchart as a guide and should result in cleaner and better code.
"C++ : Where friends have access to your private members." - Gavin Russell Baker

PaulS

Quote
However I can't seem to get them both running at the same time.  Please help!

It's pretty simple, really. Use Find to find all occurrences of delay(). Delete them.

Since that will probably have adverse impacts on your sketch, using Ctrl-A and Ctrl-X is probably a better idea. When you start the new sketch, pay attention to the structure in the blink without delay example, and don't even think about using delay().

dannyc09


Quote
However I can't seem to get them both running at the same time.  Please help!

It's pretty simple, really. Use Find to find all occurrences of delay(). Delete them.

Since that will probably have adverse impacts on your sketch, using Ctrl-A and Ctrl-X is probably a better idea. When you start the new sketch, pay attention to the structure in the blink without delay example, and don't even think about using delay().


But what if I want a delay between certain actions?  For example if the motor turns to the left then waits 4 secs before returning to the original position, how would I achieve this without the delay?  Also I want there to be delays between the different sounds being triggered.

I effectively want these two sketches to be running at the same time and don't need to be dependent on each other.

Thanks

PaulS

Quote
But what if I want a delay between certain actions?

Don't want one.

What you want is an interval between two actions. How would YOU do that? You have a watch, not an egg-timer.

The blink without delay example shows how to make things happen after an interval of time has passed, without stopping processing of other actions, like delay() does.

dannyc09

I found the blink without delay code.  So do I simply delete all the instances of delay() and replace with interval?

PaulS

Quote
So do I simply delete all the instances of delay() and replace with interval?

No. As I mentioned, you need to completely re-write your sketch once you have understood and embraced the philosophy espoused by the blink without delay example.

Jimmy60


I found the blink without delay code.  So do I simply delete all the instances of delay() and replace with interval?


Sorry no. You have to read it, understand it and then implement the concepts contained therein.

Think of it this way; delay() is like setting an alarm clock and going to sleep until you need to do the next thing.

While awake you don't do that. You keep checking the clock to see if it's time to do something. You wouldn't go to work or school and set an alarm clock for lunch time and then go to sleep. (Although that would be a nice gig.)

holmes4

Quote
I found the blink without delay code.  So do I simply delete all the instances of delay() and replace with interval?


No you study the concept shown in blink without delay then apply it.

Mark


dannyc09

Thanks guys.

I think this is too advanced for me.  I think I'm better off having two UNOs, one for the motor and one for the sound.


Cheers
Dan

AWOL

Quote
I think I'm better off having two UNOs

Better off in all but the monetary sense.
Most of the time, your UNOs will be sitting twiddling their silicon thumbs.
There is really nothing in the blink without delay example, and when you take it apart, there are about three of four important lines in it.
It really is that simple, but it open the door to much better ways of doing stuff.
I urge you to persist - there'll be help here if you need it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

dannyc09


Quote
I think I'm better off having two UNOs

Better off in all but the monetary sense.
Most of the time, your UNOs will be sitting twiddling their silicon thumbs.
There is really nothing in the blink without delay example, and when you take it apart, there are about three of four important lines in it.
It really is that simple, but it open the door to much better ways of doing stuff.
I urge you to persist - there'll be help here if you need it.


Thanks.

I just can't seem to get my head around how to get the two functions running together.  My original individual codes were working fine but now trying to combine them and get the interval function working is frying my little mind!

PaulS

Quote
I urge you to persist

Me too!

The benefits of getting rid of that crutch are enormous.

Jimmy60

Imagine that I'm a carpenter and you're my helper. I glue two pieces together and ask you to clamp them for at least 15 minutes. What would you do?

Like most of us you'd look at the clock and make note of the start time when you clamped the pieces. Periodically you look at the clock to get the current time. Mentally you could subtract the start time from the current time and see if 15 minutes has gone by.

if (current time - start time > 15 minutes) stop the clamping

Now, imagine that a few minutes later I hand you another piece and say clamp it for at least 20 minutes. So you do the same thing.

So now you are doing two simultaneous tasks (and more) with the clock as your guide.

When you ask the Arduino for the time you don't get HH:MM:SS that we are used to, you get a count of ticks since last reset. Each tick represents 1/1000 of a second. So in Arduino land if you want the current time you'll do something like:

Code: [Select]
unsigned long currentTime = millis();

millis() is a function that returns an unsigned long value that represents the count of milliseconds since reset.

Is this starting to make sense?

PaulS

Quote
Now, imagine that a few minutes later I hand you another piece and say clamp it for at least 20 minutes. So you do the same thing.

What you don't do, if you want to continue working, is say "No, I can't do that. I'm busy watching this other glue dry". That is what delay() does.

Go Up