Musical Plant Monitor won't stop playing the melody

Hello All,

I am trying to combine two projects. A Plant Monitoring project and a Melody project. They both work separately but when I try to put them together, the melody won’t stop playing.

What I would like to happen is that when the moisture reading is greater then 900, the melody plays, the buzzer sounds, and the LED lights. If the moisture reading is less than 900, the melody should not play, the buzzer should not beep, and the LED should not light.

Everything works fine except that the melody always plays no matter what the reading is.

The buzzer and the LED turn on and off as planned.

It’s probably a simple fix, but I’m at a complete loss. I’ve tried just about everything I can think of to no avail.

Any suggestions would be greatly appreciated.

Musical_Plant_Monitor.ino (2.16 KB)

Post your code. Use code tags.

the melody always plays no matter what the reading is.

No surprise really, because the code that plays the melody runs unconditionally each time through loop() and does not depend on the reading.

Sorry,

I thought the file would print.

Here is the code,

//Musical Plant Monitor

#include “pitches.h”

const int moistureAO = 0;
int AO = 0; // Pin connected to A0 on the controller
int tmp = 0; // Value of the analog pin
int buzzPin = 8; // Pin connected to the piezo buzzer
int LED = 13; // Pin connected to the LED

void setup() {

}

void loop() {
Serial.begin(9600); // Send Arduino reading to IDE
Serial.println(“Soil moisture sensor”);
pinMode(moistureAO, INPUT);
pinMode(buzzPin, OUTPUT); // Set pin as output
pinMode(LED, OUTPUT); // Set pin as output

// notes in the melody

int melody = {
NOTE_AS3, NOTE_DS4, NOTE_F4, NOTE_G4, 0, NOTE_AS3, NOTE_DS4, NOTE_F4, NOTE_G4, 0, NOTE_G4, NOTE_GS4, NOTE_G4, NOTE_F4, 0,
NOTE_DS4, NOTE_G4, NOTE_F4, NOTE_DS4, 0};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations = {
4, 4, 4, 1, 4, 4, 4, 4, 1, 4, 4, 4, 4, 1, 4, 4, 4, 4, 1};

// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 21; thisNote++)
{
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}

{
tmp = analogRead( moistureAO );
if ( tmp != AO ) {
AO = tmp;
Serial.print("Moisture = "); // Show the resistance value of the sensor
// in the IDE
Serial.println(AO);

}

delay (1000);
if (analogRead(0) > 900) { // If the reading is higher than 900,
digitalWrite(buzzPin, HIGH); // the buzzer will sound
digitalWrite(LED, HIGH); // and the LED will light
delay(1000); // Wait for 1 second
digitalWrite(buzzPin, LOW); // buzzer will stop
digitalWrite(LED, HIGH); // LED will stay lit
}

else {
digitalWrite(buzzPin, LOW); // If the reading is below 900,
// the buzzer and LED stay off
digitalWrite(LED, LOW);
}
}
}

Better, but as you did not use code tags the code has been mangled because some of it has been interpreted as HTML commands, hence the smiley in the code.

As I said previously, the melody will play regardless of the state of the analogue sensor. Put the melody playing portion of the code into its own function and only call it when (analogRead(0) > 900)

Once that works we can deal with the rest of the problems with the program such as the fact that some code that should really be in setup() to run once is called repeatedly in loop()

In this version the global values are outside of the funcations, the melody player is call as its own function, and the run once setup type stuff is in the setup function.

//Musical Plant Monitor

#include "pitches.h"

const int moistureAO = 0;
int AO = 0; // Pin connected to A0 on the controller
int tmp = 0; // Value of the analog pin
int buzzPin = 8; // Pin connected to the piezo buzzer
int LED = 13; // Pin connected to the LED
// notes in the melody

int melody[] = {
  NOTE_AS3, NOTE_DS4, NOTE_F4, NOTE_G4, 0, NOTE_AS3, NOTE_DS4, NOTE_F4, NOTE_G4, 0, NOTE_G4, NOTE_GS4, NOTE_G4, NOTE_F4, 0,
  NOTE_DS4, NOTE_G4, NOTE_F4, NOTE_DS4, 0
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 4, 4, 1, 4, 4, 4, 4, 1, 4, 4, 4, 4, 1, 4, 4, 4, 4, 1
};

void setup() {
  Serial.begin(9600); // Send Arduino reading to IDE
  Serial.println("Soil moisture sensor");
  pinMode(moistureAO, INPUT);
  pinMode(buzzPin, OUTPUT); // Set pin as output
  pinMode(LED, OUTPUT); // Set pin as output
}


void loop() {
  tmp = analogRead( moistureAO );
  if ( tmp != AO ) {
    AO = tmp;
    Serial.print("Moisture = "); // Show the resistance value of the sensor
    // in the IDE
    Serial.println(AO);
  }
  delay (1000);
  if (analogRead(moistureAO) > 900) { // If the reading is higher than 900,
    digitalWrite(buzzPin, HIGH); // the buzzer will sound
    digitalWrite(LED, HIGH); // and the LED will light
    playmelody();
    delay(1000); // Wait for 1 second
    digitalWrite(buzzPin, LOW); // buzzer will stop
    digitalWrite(LED, HIGH); // LED will stay lit
  }
  else {
    digitalWrite(buzzPin, LOW); // If the reading is below 900,
    // the buzzer and LED stay off
    digitalWrite(LED, LOW);
  }
}

void playmelody()
{
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 21; thisNote++)
  {
    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}

Thanks,

This seems to have solved my problem. I'm new to all this and haven't gotten that far yet. I guess I have to start studying more.