Ultrasonic and Backup code not working

I am working on an rc car project and I have hooked up a ultrasonic sensor to my arduino with a buzzer and esc. What I need is if the TXValue is < 1400 (PWM) then the buzzer should start playing. The issue im getting is that the motor keeps running until the if() statment stops. Here is my code:

/*
 Ruban Clare
 Rc car project with arduino, Flysky FS-i6x, Flysky Reciver Module, ect.
*/
 
// Define Input Connections
#include <Servo.h> //Servo librarey to support ESC
Servo ESC; //ESC as Servo
#include "pitches.h"

  int melody1[] = {
  NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5,
  NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_C6,
  NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5, NOTE_AS5, NOTE_C6,
  NOTE_AS5, NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6,
  NOTE_GS5, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_DS5,
  NOTE_F5, NOTE_FS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_DS5, NOTE_FS5, NOTE_F5
};

int noteDurations[] = {
  4,8,4,8,8,8, 4,8,8,4,8,8, 4,8,4,8,8,8, 4,8,8,2, 4,8,4,8,8,8, 4,8,8,4,8,8, 4,8,8,4,8,8, 4,8,2
};


#define ONE_FOOT (12 * 2.54)
#define FOUR_FEET (ONE_FOOT * 3)
#define SIX_FEET (ONE_FOOT * 5)

#define CH1 2 //****************************done***********************************
#define TX 3 //CH2****************************done***********************************
#define CH7 4 //****************************done***********************************
#define CH8 5 //****************************done***********************************
#define CH9 6 //****************************done***********************************
#define CH10 7 //****************************done***********************************



#define ledRL A2 //****************************done***********************************
#define ledGR A3 //****************************done*********************************** 
#define HLight A4 //****************************done***********************************
#define CLight A1 //****************************done***********************************

#define Buzzer 12 //****************************done***********************************

 // Boolean to represent switch value

bool ch7Value;
bool ch8Value;
bool ch9Value;
bool ch10Value;

// Integers to represent values from sticks and pots
int ch1Value;
int TXValue; //CH2


// Delays
int GRdelay = 250;
int RLdelay = 250;
int BuzzerDelay = 400;

//Ultrasonic Sensor
int trigPin = 8; //****************************done***********************************
int echoPin = 9; //****************************done***********************************
long duration;
int distance;




  //FS-i6x STICKS READING
// Read the number of a specified channel and convert to the range provided.
// If the channel is off, return the default value
int readChannel(int channelInput, int minLimit, int maxLimit, int defaultValue){
  int ch = pulseIn(channelInput, HIGH, 30000);
  if (ch < 100) return defaultValue;
  return map(ch, 1000, 2000, minLimit, maxLimit);
}
  //FS-i6x SWITCH READING`
// Read the switch channel and return a boolean value
bool readSwitch(byte channelInput, bool defaultValue){
  int intDefaultValue = (defaultValue)? 100: 0;
  int ch = readChannel(channelInput, 0, 100, intDefaultValue);
  return (ch > 50);
}
 
void setup(){
  Serial.begin(2000000);

  pinMode(CH1, INPUT);
  pinMode(CH7, INPUT);
  pinMode(CH8, INPUT);
  pinMode(CH10, INPUT);

  pinMode(ledRL, OUTPUT);
  pinMode(ledGR, OUTPUT);
  pinMode(HLight,OUTPUT);
  pinMode(CLight,OUTPUT);

  pinMode(TX, INPUT);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT); 
  
  ESC.attach(10); //DigitalPin 10
}
 
 
void loop() {
//ESC CONTROL

TXValue = pulseIn(TX, HIGH);
ch10Value = readSwitch(CH10, false);

digitalWrite(trigPin, LOW);
delayMicroseconds(3);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

 if (ch10Value == 0){
  distance= duration*0.034/2;
}
else if(ch10Value == 1){
  distance = 1000;

} 

  if (distance == 0 || distance > SIX_FEET){
    // TXValue unchanged
  }
  else if (distance < FOUR_FEET){
    TXValue = constrain(TXValue, 1300, 1500);
  }
  else if (distance < SIX_FEET){
    TXValue = constrain(TXValue, 1400, 1600);
  }
  

 


  Serial.println(TXValue);
  ESC.writeMicroseconds(TXValue);

  
//BACKUP BUZZER CODE
//ch3Value = readChannel(CH3, -100, 100, 0);
//Serial.println(ch3Value);




if(TXValue<1400){
     for (int thisNote = 0; thisNote < 43; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(Buzzer, melody1[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.10;
    delay(pauseBetweenNotes);
    noTone(Buzzer);
  }  
}

else(TXValue>1480);{
  noTone(Buzzer);
}

//CLIGHT CODE
 ch9Value = readSwitch(CH9, false);
// Serial.println(ch9Value);
 if(ch9Value > 0){
  digitalWrite(CLight,HIGH);
}
else{
  digitalWrite(CLight,LOW);
}


//HEADLIGHT CODE
 ch8Value = readSwitch(CH8, false);
 //Serial.println(ch8Value);
 if(ch8Value > 0){
  digitalWrite(HLight,HIGH);
}
else{
  digitalWrite(HLight,LOW);
}

//EMERGANCY PARKING CODE
  ch7Value = readSwitch(CH7, false);
  //Serial.print(ch7Value);
  if(ch7Value > 0){
    digitalWrite(ledGR, HIGH);
    digitalWrite(ledRL, HIGH);
    delay(GRdelay);
    digitalWrite(ledGR, LOW);
    digitalWrite(ledRL, LOW);
    delay(GRdelay) ;
  }

// INDECATOR CODE 

  // Get values for each channel
  ch1Value = readChannel(CH1, -100, 100, 0);
  
  // Print to Serial Monitor
  //Serial.println(ch1Value);
  
  delay(150);

  if (ch1Value > 10) {
    //while(ch1Value > 10){
 digitalWrite(ledRL, HIGH);
 delay(GRdelay);
 digitalWrite(ledRL, LOW);
 delay(GRdelay);
  //  }
}


else if (ch1Value < -10) {

 digitalWrite(ledGR, HIGH);
 delay(RLdelay);
 digitalWrite(ledGR, LOW);
 delay(RLdelay); 
 }
//else if(ch1Value == 0) {
// digitalWrite(ledRL, LOW);
//digitalWrite(ledGR, LOW);
// 
// }
}

The issue I am getting is:


if(TXValue<1400){
     for (int thisNote = 0; thisNote < 43; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(Buzzer, melody1[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.10;
    delay(pauseBetweenNotes);
    noTone(Buzzer);
  }  
}

else(TXValue>1480);{
  noTone(Buzzer);
}

Its just that if my motor is going lets say 10mph and I decide to go reverse, the if else statment wont stop it just keeps going and my remote value isnt reading until the buzzer is done playing its music.

Edit: I added the wrong code sorry i ment to put the original code (not messed with)

Look at the code for playing its music and tell us where you make any car movements. The music code is full of delays, what do you expect the Arduino to do while playing tones and delaying?

You can't just sample the 'echo' pin whenever you want. You have to trigger the 'trigger' pin first.

And checking for if (TXValue>1480) inside an "if (TXValue<1400)" statement doesn't make sense.

That's a complete if statement. The code that follows, between the { braces } will always be execute.

You probably want to lose the semicolon.

Review the syntax of the if statement.

a7

I forgot to put the original code :sweat_smile: i was just messing arround just to see if it would work. I understand the echo pin and i have changed its location. Otherwise the code still won't work. Its when i hit the reverse on my controller everything pauses and the if statment keeps going until the tone ends. Somewhere in the code i even put a delay

//BACKUP BUZZER CODE
//ch3Value = readChannel(CH3, -100, 100, 0);
//Serial.println(ch3Value);

if(TXValue<1400){
     for (int thisNote = 0; thisNote < 43; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(Buzzer, melody1[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.10;
    delay(pauseBetweenNotes);
    noTone(Buzzer);
  }  
}

else(TXValue>1480);{
  noTone(Buzzer);
}

this would be the original code.

I've taken a look and turns out:


if(TXValue<1400){
     for (int thisNote = 0; thisNote < 43; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(Buzzer, melody1[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.10;
    delay(pauseBetweenNotes);
    noTone(Buzzer);
  }  
}

there was a pause between notes delay (I didnt write this part of code) Is there any work arrounds to this?

Certainly. As shown in the code to do several things at the same time. You will have to rework the entire program.

Learn to write non-blocking code :wink:

Basically for-loops and while-loops don't belong in arduino code; for this delays are also not allowed. I don't have time now but will try guide you in a day or two if nobody else jumps in. Please post your pitches.h so I don't have to dig for it.

I dont know what that means but if u ment the speaker code then here it is:


#include "pitches.h"

  int melody1[] = {
  NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5,
  NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_C6,
  NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5, NOTE_AS5, NOTE_C6,
  NOTE_AS5, NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6,
  NOTE_GS5, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_DS5,
  NOTE_F5, NOTE_FS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_DS5, NOTE_FS5, NOTE_F5
};

int noteDurations[] = {
  4,8,4,8,8,8, 4,8,8,4,8,8, 4,8,4,8,8,8, 4,8,8,2, 4,8,4,8,8,8, 4,8,8,4,8,8, 4,8,8,4,8,8, 4,8,2
};

#define Buzzer 12 

void setup(){
  Serial.begin(115200); 

}
 
 
void loop() {

//BACKUP BUZZER CODE
ch3Value = readChannel(CH3, -100, 100, 0);
Serial.println(ch3Value);

if(TXValue<1450){
     for (int thisNote = 0; thisNote < 43; thisNote++) {
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(Buzzer, melody1[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1;
    delay(pauseBetweenNotes);
    noTone(Buzzer);
  }  
}

else(TXValue>1480);{
  noTone(Buzzer);
}
}

thanks for the help!

No

You include that file in your code; it's more than likely in your sketch directory.

I've found a pitches,h so I can compile.

First, start using more functions. For every action that you have, write a function.

I could find the following actions in your code

  1. measurement
  2. play melody
  3. clight
  4. head light
  5. emergency parking
  6. indicator

Let's start with a reasonably easy one (the emergency parking) to get you familiar with one of the ideas. This introduces a finite state machine / sequencer where you have a few states (steps) as well as timing using millis().

  1. leds on
  2. wait a bit
  3. lights off
  4. wait a bit

The original function could have looked like this; it's just your code placed in a function.

void emergencyParkingOld()
{
  if (ch7Value > 0)
  {
    digitalWrite(ledGR, HIGH);
    digitalWrite(ledRL, HIGH);
    delay(GRdelay);
    digitalWrite(ledGR, LOW);
    digitalWrite(ledRL, LOW);
    delay(GRdelay);
  }
}

We're using a local variable that will keep track of the state; making this variable static makes that it will not be forgotten between successive calls of the function. We will also need a static variable for our timing.

void emergencyParking()
{
  // next step (state) to execute
  static uint8_t state;
  // start time of delay
  static uint32_t delayStarttime;

  if (ch7Value <= 0)
  {
    // nothing to do
    return;
  }
  
  ...
  ...

The above also tests the ch7Value and bails out if ememrgency parking was not activated.

Next we can make use of our new state variable in a switch/case.

  ...
  ...
    switch (state)
  {
    case 0:
      digitalWrite(ledGR, HIGH);
      digitalWrite(ledRL, HIGH);
      break;
    case 1:
      delay(GRdelay);
      break;
    case 2:
      digitalWrite(ledGR, LOW);
      digitalWrite(ledRL, LOW);
      break;
    case 3:
      delay(GRdelay);
      break;
  }
}

Each time that as step is complete, we prepare for the next step and next switch to the next step, for the digitalWrites

      digitalWrite(ledGR, HIGH);
      digitalWrite(ledRL, HIGH);
      
      // set the start time for the delay
      delayStarttime = millis();
      // go to the next state
      state++;

The delay will be replaced by a comparison of the start time with the current time; the application will stay in that state till the wait step is finished.

      if (millis() - delayStarttime >= GRdelay)
      {
        // go back to the first state
        state = 0;
      }

In the last step, we will have to reset the state to zero so the next time the function is called it will start from the beginning. Full function

/*
  non-blocking ememrgency parking
*/
void emergencyParking()
{
  // next step (state) to execute
  static uint8_t state;
  // start time of delay
  static uint32_t delayStarttime;

  if (ch7Value <= 0)
  {
    // nothing to do
    return;
  }

  switch (state)
  {
    case 0:
      digitalWrite(ledGR, HIGH);
      digitalWrite(ledRL, HIGH);
      // set the start time for the delay
      delayStarttime = millis();
      // go to the next state
      state++;
      break;
    case 1:
      // if wait time (GRdelay) has lapsed
      if (millis() - delayStarttime >= GRdelay)
      {
        // go to the next state
        state++;
      }
      break;
    case 2:
      digitalWrite(ledGR, LOW);
      digitalWrite(ledRL, LOW);
      // set the start time for the delay
      delayStarttime = millis();
      // go to the next state
      state++;
      break;
    case 3:
      // if wait time (GRdelay) has lapsed
      if (millis() - delayStarttime >= GRdelay)
      {
        // go back to the first state
        state = 0;
      }
      break;
  }
}

And in loop() you can call it as shown

void loop()
{
  // EMERGENCY PARKING CODE
  ch7Value = readSwitch(CH7, false);
  emergencyParking();
}

To get rid of some compiler warnings, I changed

// Delays
int GRdelay = 250;
int RLdelay = 250;
int BuzzerDelay = 400;```
to
```cpp
// Delays
const uint32_t GRdelay = 250;
const uint32_t RLdelay = 250;
const uint32_t BuzzerDelay = 400;

The indicator function will be very similar. In all cases of the switch statement you will add an if/else/elseif to either control the left indicator or the right indicator or switch them off. See if you can figure it out yourself.

Next the function to play a melody. It's again a finite state machine with millis() based timing. There are 3 possible states

  1. start the note
  2. wait for duration that note must be played
  3. wait for the delay between the notes
void playMelody()
{
  // next step (state) to execute
  static uint8_t state;
  // start time of delay
  static uint32_t delayStarttime;
  // next note to play
  static uint8_t thisNote;

  uint16_t noteDuration = 1000 / noteDurations[thisNote];
  uint16_t pauseBetweenNotes = noteDuration * 1.10;
  
  ...
  ...

This uses the same basics as the emergency parking. To get rid of the for-loop, thisNote is now a static variable that will be incremented if it's time to play the next note of the melody. The noteDuration and the pauseBetweenNotes are again unsigned integers andnot integers.

In the next part of the code, we'll check the TXValue.

  ...
  ...
  if (TXValue > 1480)
  {
    // reset thisNote so next time we start playing the melody from the beginning
    thisNote = 0;
    // stop the sound
    noTone(Buzzer);
    // reset state
    state = 0;    
  }
  else if (TXValue < 1400)
  {
    // play a note of the melody
  }
  else
  {
    // YOU HAVE NOTE SPECIFIED WHAT MUST HAPPEN BETWEEN 1400 AND 1480 !!
  }
}

Now you can implement the playing of the melody using the finite state machine and the millis() based timing

  switch (state)
  {
    case 0:
      // play note
      tone(Buzzer, melody1[thisNote]);
      // set the start time for the duration that the note must play
      delayStarttime = millis();
      // go to the next state
      state++;
      break;
    case 1:
      // if the note has played for the specified duration
      if (millis() - delayStarttime >= noteDuration)
      {
        // stop playing the note
        noTone(Buzzer);
        // set the start time for the duration that we must pause between notes
        delayStarttime = millis();
        // go to the next state
        state++;
      }
      break;
    case 2:
      // if we have paused long enough
      if (millis() - delayStarttime >= pauseBetweenNotes)
      {
        // we're done with this note; play next note
        thisNote++;
        // if we are at the end of the melody
        if (thisNote >= 43)
        {
          // next time start from the beginning
          thisNote = 0;
        }
        // reset state for the next note
        state++;
      }
  }

And add it to loop()

void loop()
{
  //ESC CONTROL

  TXValue = pulseIn(TX, HIGH);
  ch10Value = readSwitch(CH10, false);

  digitalWrite(trigPin, LOW);
  delayMicroseconds(3);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  if (ch10Value == 0)
  {
    distance = duration * 0.034 / 2;
  }
  else if (ch10Value == 1)
  {
    distance = 1000;
  }

  if (distance == 0 || distance > SIX_FEET)
  {
    // TXValue unchanged
  }
  else if (distance < FOUR_FEET)
  {
    TXValue = constrain(TXValue, 1300, 1500);
  }
  else if (distance < SIX_FEET)
  {
    TXValue = constrain(TXValue, 1400, 1600);
  }

  Serial.println(TXValue);
  ESC.writeMicroseconds(TXValue);

  //BACKUP BUZZER CODE
  playMelody();

  // EMERGENCY PARKING CODE
  ch7Value = readSwitch(CH7, false);
  emergencyParking();
}

I strongly suggest that you create a function to calculate the TXValue (e.g. called getDistance() ) to keep loop() tidy. There are improvements to be made but this is it for now.

Your loop() should eventually look like

void loop()
{
  getDistance();
  
  // ESC control
  ESC.writeMicroseconds(TXValue);
  
  //BACKUP BUZZER CODE
  playMelody();
 
   //CLIGHT CODE
  ch9Value = readSwitch(CH9, false);
  clight();

  //HEADLIGHT CODE
  ch8Value = readSwitch(CH8, false);
  headlight();

  // EMERGENCY PARKING CODE
  ch7Value = readSwitch(CH7, false);
  emergencyParking();
  
  // INDICATOR CODE
  ch1Value = readChannel(CH1, -100, 100, 0);
  indicator();
}

I hope that it's clear how to approach a sketch that is non-blocking. To make it all work, you should nowhere use delay().

I've compiled the snippets as part of the bigger code but I have no way of testing.

Thanks so much I've been stuck for so long!!!!!

Although I did learn somthing new (adding funtions) but im still new to the concept :sweat_smile:

I dont have my TX hocked up anymore and insted I've added a switch. I did test the code only small changes and I've added a switch. Everything is working fine EXEPT everytime I hit the switch only one note plays I know I prob did a dumb mastake but I cant figure it out. Here is my code:

#include "pitches.h"

  int melody1[] = {
  NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5,
  NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_C6,
  NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_GS5, NOTE_AS5, NOTE_C6,
  NOTE_AS5, NOTE_F5, NOTE_C6, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_C6,
  NOTE_GS5, NOTE_AS5, NOTE_C6, NOTE_AS5, NOTE_GS5, NOTE_FS5, NOTE_DS5,
  NOTE_F5, NOTE_FS5, NOTE_GS5, NOTE_FS5, NOTE_F5, NOTE_DS5, NOTE_FS5, NOTE_F5
};

int noteDurations[] = {
  4,8,4,8,8,8, 4,8,8,4,8,8, 4,8,4,8,8,8, 4,8,8,2, 4,8,4,8,8,8, 4,8,8,4,8,8, 4,8,8,4,8,8, 4,8,2
};

const int buttonPin = 2;     // the number of the pushbutton pin
int buttonState = 0;         // variable for reading the pushbutton status


#define Buzzer 9
// Delays

const uint32_t BuzzerDelay = 400;

void playMelody(){

  // next step (state) to execute
  static uint8_t state;
  // start time of delay
  static uint32_t delayStarttime;
  // next note to play
  static uint8_t thisNote;

  uint16_t noteDuration = 1000 / noteDurations[thisNote];
  uint16_t pauseBetweenNotes = noteDuration * 1.10;
  if (buttonState == LOW){
    // reset thisNote so next time we start playing the melody from the beginning
 //   thisNote = 0;
    // stop the sound
    noTone(Buzzer);
    // reset state
    state = 0;    
  }
  else if (buttonState == HIGH){
 switch (state)
  {
    case 0:
      // play note
      tone(Buzzer, melody1[thisNote]);
      // set the start time for the duration that the note must play
      delayStarttime = millis();
      // go to the next state
      state++;
      break;
    case 1:
      // if the note has played for the specified duration
      if (millis() - delayStarttime >= noteDuration)
      {
        // stop playing the note
        noTone(Buzzer);
        // set the start time for the duration that we must pause between notes
        delayStarttime = millis();
        // go to the next state
        state++;
      }
      break;
    case 2:
      // if we have paused long enough
      if (millis() - delayStarttime >= pauseBetweenNotes)
      {
        // we're done with this note; play next note
        thisNote++;
        // if we are at the end of the melody
        if (thisNote >= 43)
        {
          // next time start from the beginning
          thisNote = 0;
        }
        // reset state for the next note
        state++;
      }
  }
  }
  }  
  
void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT);
}

void loop() {
    buttonState = digitalRead(buttonPin);

  // put your main code here, to run repeatedly:
  playMelody();
  Serial.println(buttonState);

}

I implemented all of this in my code (I had to rewrite everything) and it worked other than the buzzer also it did upload and I had no errors. I think its because of the duration of the switch pressing or somthing...

thanks soo much for the help!!!

What do you want to achieve? If the button is pressed, play the complete melody?

How is the button wired? Pull-up resistor between 5V and pin and button between pin and GND. Or pull-down resistor between GND and pin and button between pin and 5V?

All I want is the melody to play in the background when im pressing the button and do other funtions at the same time (non blocking code) also when i let go it should stop. I just wanted to change the remote to a switch instead. I've tried diffrent ways and it didnt work, I've also got my other TX remote back so I'll test it on that aswell

Thanks

Can you please answer the question how the button is wired?

You will have to learn to debug using serial prints :wink: There is a bug in the playMelody that I presented :frowning:

To debug this, put the below in each of the cases

          Serial.print(F("Switching to state "));
          Serial.print(state);

You will (should) see

Switching to state 1
Switching to state 2
Switching to state 3

The latter should have been

Switching to state 0

So case 2 needs to change to reset the state (the comment was correct, what it did was not correct).

      case 2:
        // if we have paused long enough
        if (millis() - delayStarttime >= pauseBetweenNotes)
        {
          // we're done with this note; play next note
          thisNote++;
          // if we are at the end of the melody
          if (thisNote >= 43)
          {
            // next time start from the beginning
            thisNote = 0;
          }
          // reset state for the next note
          state = 0;

          Serial.print(F("Switching to state "));
          Serial.print(state);
        }
        break;

There was also a break missing at the end of the case. That did not cause problems as it was the last case but I did fix it in above.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.