Having trouble trying to run 3 different loops at the same time

I am creating a project for school that is based on making different parts of an ambulance.
I have a buzzer, a stepper motor and an LED set that I have already coded and found the separate parts for.
For it to actually work for me I need to have all three parts to run at the same time without a delay.

I had the code working perfectly for me before and everything ran in turns (LED then buzzer siren then stepper motor[repeated]) and now it fails to verify.
The code that does the before mentioned task now has ceased to function and the error messages aren't very clear in pointing out what's wrong with the code.

If I could at least get the code to work again it would be great but if anyone could help me with getting all of the functions to run at the same time I would be very grateful

siren.ino (3.38 KB)

Gristinator:
run at the same time without a delay.

Then why is you're code full of them? :wink:

Delay() stops everything dead in it's tracks, it's called blocking code. And because a micro can only do one thing at a time (even if that single thing is waiting) you are stuck.

Thing to do, start over! And this time don't use delay() anywhere! Have a look at Blink without delay.

Just say NO to delay() and use millis() for timing instead

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

septillion:
Then why is you're code full of them? :wink:

Delay() stops everything dead in it's tracks, it's called blocking code. And because a micro can only do one thing at a time (even if that single thing is waiting) you are stuck.

Thing to do, start over! And this time don't use delay() anywhere! Have a look at Blink without delay.

I have an UNO R3 and the delays I have in it are needed for the LED blink pattern and the buzzer. Is there any way around just not using it? I'll try and use blink without delay for the LED pattern but it doesn't seem to explain the reason that my code stopped working.
The code already uploaded to the board when it worked and is still on it but the code that I uploaded to it now just gives errors.

Error messages:
Arduino: 1.8.5 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/Louis/Documents/Arduino/siren/Ambulance.ino:93:11: error: redefinition of 'const int stepsPerRevolution'
const int stepsPerRevolution = 300;
^
/Users/Louis/Documents/Arduino/siren/siren.ino:93:11: note: 'const int stepsPerRevolution' previously defined here
const int stepsPerRevolution = 300;
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:95:19: error: redefinition of 'Stepper myStepper'
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
^
/Users/Louis/Documents/Arduino/siren/siren.ino:95:9: note: 'Stepper myStepper' previously declared here
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:97:5: error: redefinition of 'int speakerPin'
int speakerPin = 12; //Speaker or Piezo buzzer on pin 11
^
/Users/Louis/Documents/Arduino/siren/siren.ino:97:5: note: 'int speakerPin' previously defined here
int speakerPin = 12; //Speaker or Piezo buzzer on pin 11
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:98:5: error: redefinition of 'int bluePin'
int bluePin = 3;
^
/Users/Louis/Documents/Arduino/siren/siren.ino:98:5: note: 'int bluePin' previously defined here
int bluePin = 3;
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:99:5: error: redefinition of 'int redPin'
int redPin = 4;
^
/Users/Louis/Documents/Arduino/siren/siren.ino:99:5: note: 'int redPin' previously defined here
int redPin = 4;
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino: In function 'void setup()':
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:101:6: error: redefinition of 'void setup()'
void setup()
^
/Users/Louis/Documents/Arduino/siren/siren.ino:101:6: note: 'void setup()' previously defined here
void setup()
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino: In function 'void loop()':
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:110:6: error: redefinition of 'void loop()'
void loop()
^
/Users/Louis/Documents/Arduino/siren/siren.ino:110:6: note: 'void loop()' previously defined here
void loop()
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino: In function 'void beep(unsigned char, int, long int)':
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:115:6: error: redefinition of 'void beep(unsigned char, int, long int)'
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds) //code for working out the rate at which each note plays and the frequency.
^
/Users/Louis/Documents/Arduino/siren/siren.ino:115:6: note: 'void beep(unsigned char, int, long int)' previously defined here
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds) //code for working out the rate at which each note plays and the frequency.
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino: In function 'void song()':
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:129:6: error: redefinition of 'void song()'
void song()
^
/Users/Louis/Documents/Arduino/siren/siren.ino:129:6: note: 'void song()' previously defined here
void song()
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:149:25: error: too many arguments to function 'int digitalRead(uint8_t)'
if (digitalRead (3, HIGH)
^
In file included from sketch/siren.ino.cpp:1:0:
/private/var/folders/b5/n22h59893d5dcjx41y7d_y_m0000gp/T/AppTranslocation/D985ACC2-02C4-472E-BD40-959C4826594D/d/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Arduino.h:135:5: note: declared here
int digitalRead(uint8_t);
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:150:3: error: expected ')' before '{' token
{
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:152:1: error: expected primary-expression before '}' token
}
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino: At global scope:
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:155:5: error: expected constructor, destructor, or type conversion before '(' token
beep(speakerPin, NOTE_C5, 300); // beep( -PIN OF SPEAKER-, -THE NOTE WANTING TO BE PLAYED-, -DURATION OF THE NOTE IN MILISECONDS- )
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:156:6: error: expected constructor, destructor, or type conversion before '(' token
delay(100);
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:157:5: error: expected constructor, destructor, or type conversion before '(' token
beep(speakerPin, NOTE_A5, 300);
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:158:6: error: expected constructor, destructor, or type conversion before '(' token
delay(100);
^
/Users/Louis/Documents/Arduino/siren/Ambulance.ino:159:1: error: expected unqualified-id before '{' token
{
^
exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Did you actually read what UKHeliBob and I posted? We both pointed you to the answer.

And yes it does, like I said, a micro only does ONE thing at a time. Even if that single thing is waiting.

And if you say you have error, share them with us. With this cloudy weather my crystal ball just won't work :confused:

septillion:
And if you say you have error, share them with us. With this cloudy weather my crystal ball just won't work :confused:

Errors posted

That's great! Now please post the errors of THIS program :wink: Aka the one in the start post aka siren.ino. Because in your post is no Ambulance.ino...

septillion:
That's great! Now please post the errors of THIS program :wink: Aka the one in the start post aka siren.ino. Because in your post is no Ambulance.ino...

Sorry for the confusion. I renamed the siren code to ambulance but it still shows up as siren in the header bar and ambulance in the error mesages. I doofed

ahhh, now we're talking. How did you try to rename it? Both the ino AND the folder need to have the same name.