Hello all, I am currently working on a project that requires some tasks to be performed halfway through a different task, without stopping either one. I'd like to have the code working as if function A were run halfway through function B without pausing function B.
My goal for this code is to have function makeBeep happen halfway through function turnServo, without pausing turnServo. Here's the code I've got.
#include <Servo.h>
int speakerPin = 3;
int beepTone = 493;
int beepLength = 50;
int ledPin = 6;
int pos = 0; //initial position of servo
int servoDelayTime = 5;
Servo turnHead; //declare servo
void turnServo() {
for(pos = 0; pos < 180; pos += 1)
{
turnHead.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
turnHead.write(pos);
delay(15);
}
}
void makeBeep() {
tone(speakerPin, beepTone, beepLength);
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
}
void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
turnHead.attach(9);
}
void loop() {
turnServo();
}
Does anybody know of a good way to do this? I am very new to Arduino, so please do your best to make this easy to understand. Thank you for your time!