Hay all, new here and this is my first post. If I have made any mistakes in posting please provide corrections and be gentle ;).
So here is my question... or problem I am experiencing. I am trying to make a L0LA droid. It has an accelerometer which triggers wing movement via 2 sg90 servos, this just fine. I also want it to play some sounds (via tones) while it is moving its wings. I have found that I cannot simultaneously flap wings while generating sounds, so I have introduced an additional nano to operate sound while the main nano operates its wing. I have done this through the serial.write/read function on the tx/rx side of things. I have an idle function that works great, however, while it is moving i am trying to trigger the slave nano to "speak" but it keeps getting interrupted (starts to play a tone and then gets cut off). I have been working on this problem for two days now and i am no closer to figuring this out.
So, my question is, why do my tones keep getting interrupted and how do I go about fixing this?
Master Nano code
void balance(){ //function called when movement is detected by accelerometer
float accX = mpu.getAccX(); // setting a variable to acc measurement
if(accX>.01||accX<-.01){ //if measurement is detected outside -.01 to .01
balanceTimer++; // add to my balance timer
}
if(balanceTimer >= balanceTimerSet){ //if balance timer is greater than the set
talk(); // do the talk function
delay(10); // tried to see if delay would help and it does not
balanceTimer = 0; //reset timer and balance timer (originally set to 0)
balanceTimerSet = 15;
}
if(accX>0.1){ //this is all wing movement and LEDs
lWing.easeTo(lwaClosed, sensitivity);
rWing.easeTo((accX)*rwaRange,sensitivity);
leds[0] = CRGB :: Purple;
FastLED.show();
} else if(accX<-0.1){
rWing.easeTo(rwaClosed, sensitivity);
lWing.easeTo(lwaClosed+(accX*lwaRange), sensitivity);
leds[0] = CRGB :: Blue;
FastLED.show();
} else{
idle();
}
Serial.println(balanceTimer);
}
void idle(){ // while just sitting with no movement same as above
rWing.easeTo(rwaClosed, sensitivity);
lWing.easeTo(lwaClosed, sensitivity);
idleTimer++;
if (idleTimer >= 50){
// Serial.println("sending idle Byte");
delay(10);
talk();
idleSetTime = random(50,1000);
idleTimer = 0;
}
}
void talk(){ //talk function
motion = 1; //eventually will have different case and this will be set when called
Serial.write(motion); //send signal to slave nano
delay(10); //though a delay would help, it did not
// Serial.println("byte sent");
}
slave nano
void loop() {
Serial.println(Serial.available());
if(Serial.available()>0){
if(!soundPlaying){ //tried adding a bool while it was playing but it still doesn't work
soundPlaying = true;
playSound();
}
}
}
void playSound(){ //this is all just the sounds
int K = 2000;
switch (random(1,7)) {
case 1:phrase1(); break;
case 2:phrase2(); break;
case 3:phrase1(); phrase2(); break;
case 4:phrase1(); phrase2(); phrase1();break;
case 5:phrase1(); phrase2(); phrase1(); phrase2(); phrase1();break;
case 6:phrase2(); phrase1(); phrase2(); break;
}
for (int i = 0; i <= random(3, 9); i++){
tone(speakerPin, K + random(-1700, 2000));
delay(random(70, 170));
noTone(speakerPin);
delay(random(0, 30));
}
noTone(speakerPin);
delay(random(2000, 4000));
soundPlaying = false;
}
void phrase1() {
int k = random(750,1500);
for (int i = 0; i <= random(100,2000); i++){
tone(speakerPin, k+(-i*2));
delay(random(.9,2));
}
for (int i = 0; i <= random(100,1000); i++){
tone(speakerPin, k + (i * 10));
delay(random(.9,2));
}
soundPlaying = false;
}
void phrase2() {
int k = random(750,2000);
for (int i = 0; i <= random(100,2000); i++){
tone(speakerPin, k+(i*2));
delay(random(.9,2));
}
for (int i = 0; i <= random(100,1000); i++){
tone(speakerPin, k + (-i * 10));
delay(random(.9,2));
}
}
Again the idle works as expected plays a series of beeps and boops every few seconds, but as soon as i start moving it, it will start to play but seems to get cut off. Again, any help is greatly appreciated!
-JMo
