Re: combining two codes with a common output

I am making a fire and water level sensor project. Each code is running properly, but I have troubles in combining them.

For my fire sensor, when the Flame Sensor detects flames, the red LED turns and the buzzer turns on, while if there is no flame detected, the green LED turns on. Here is the code for my fire sensor:

#define redled 13
#define greenled 12

const int Buzzer = 8; // Use buzzer for alert 
int FlamePin = 2;  // This is for input pin
int Flame = HIGH;  // HIGH when FLAME is exposed

void setup() {
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  pinMode(Buzzer, OUTPUT);
  pinMode(FlamePin, INPUT);
  Serial.begin(9600);
  
}

void loop() {
  Flame = digitalRead(FlamePin);
  if (Flame== HIGH)
  {
    Serial.println(Flame);
    digitalWrite(Buzzer, HIGH);
    digitalWrite(redled, HIGH);
    digitalWrite(greenled, LOW);
    
  }
  else
  {
    Serial.println(Flame);
    digitalWrite(Buzzer, LOW);
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, LOW);
  }
}

For my water level sensor, the RGB led turns into different colors and the buzzer turns on depending on the conditions. This is for my water level sensor:

//define RGB led

#define ledRed 8 
#define ledGreen 9
#define ledBlue 10

const int WaterLevel = A0; //Water Level Sensor to Arduino pin A0
const int Buzzer = 8; // Use buzzer for alert

int watervalue;
int watermap; //mapped value for water sensor
int minimumvalue = 100;
int maximumvalue = 750;
 

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);
  pinMode(Buzzer, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  watervalue = analogRead(WaterLevel);
  watermap = map(watervalue, minimumvalue, maximumvalue, 0, 10);
  
  if (watermap >2 && watermap <= 4){
      digitalWrite(ledBlue, HIGH); //Blue-green becomes RGB LED color
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledRed, LOW);
  }
 else if (watermap > 4 && watermap <= 6){
      digitalWrite(ledBlue, HIGH); 
      digitalWrite(ledGreen, LOW); //Violet becomes RGB LED color 
      digitalWrite(ledRed, HIGH);
      digitalWrite(Buzzer, HIGH);
     }
  else if (watermap > 6 ){
       digitalWrite(ledBlue, LOW); //Red becomes RGB LED color
       digitalWrite(ledRed, HIGH);
       digitalWrite(ledGreen, LOW);
       digitalWrite(Buzzer, HIGH);
     }
else {
      digitalWrite(ledBlue, LOW); //Green becomes RGB LED color
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledRed, LOW);  
      digitalWrite(Buzzer, LOW);
      }     
  Serial.println(watermap);
  delay(1000);      
}

I hope to combine the two sketches into one where each work properly. In addition, I want the buzzer as an output which would activate if any of the sensors would reach the condition. I would appreciate if you would give me guidance as to how it should be done. Thanks!

I'm new to the forums by the way, super awesome community!

Before you try to combine programs, get the pin conflicts resolved. Pin 8 is one I see right away.

Paul

Hi,
You might try this approach:

http://arduino-info.wikispaces.com/CombiningArduinoSketches

Assuming both sketches worked this this should do both.

Make sure you select the correct PINS for your dual project.
You should also remove the delay(1000) call and use something LIKE

if(millis()-t < 1000){return}
t = millis();

as the first two lines in loop. ( and declare t as global )

Sorry I missed the mention of needing to have the buzzer sound.
Look at the Tone libs, (from memory)
You can even use PWM output and generate simple notes to play a tune.

Or you just turn the pin high or low if you have a tolerance to pizzo buzzers.

///////////////////////
//define RGB led
#define FlamePin  2   // This is for input pin
#define Buzzer    8   // Use buzzer for alert  << Change for FREE PIN
#define ledRed    8   // Conflict with Buzzer Pin RESELECT
#define ledGreen  9
#define ledBlue   10
#define redled    13
#define greenled  12

int Flame = HIGH;  // HIGH when FLAME is exposed

const int WaterLevel = A0; //Water Level Sensor to Arduino pin A0

int watervalue;
int watermap; //mapped value for water sensor
int minimumvalue = 100;
int maximumvalue = 750;

void setup() {
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);

  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);

  pinMode(Buzzer, OUTPUT);
  pinMode(FlamePin, INPUT);

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  Flame = digitalRead(FlamePin);
  
  if (Flame== HIGH) {
    Serial.println(Flame);
    digitalWrite(Buzzer, HIGH);
    digitalWrite(redled, HIGH);
    digitalWrite(greenled, LOW);    
  } else {
    Serial.println(Flame);
    digitalWrite(Buzzer, LOW);
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, LOW);
  }

  // read the input on analog pin 0:
  watervalue = analogRead(WaterLevel);
  watermap = map(watervalue, minimumvalue, maximumvalue, 0, 10);
  
  if (watermap >2 && watermap <= 4){
      digitalWrite(ledBlue, HIGH); //Blue-green becomes RGB LED color
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledRed, LOW);
  } else if (watermap > 4 && watermap <= 6){
      digitalWrite(ledBlue, HIGH); 
      digitalWrite(ledGreen, LOW); //Violet becomes RGB LED color 
      digitalWrite(ledRed, HIGH);
      digitalWrite(Buzzer, HIGH);
  } else if (watermap > 6 ){
       digitalWrite(ledBlue, LOW); //Red becomes RGB LED color
       digitalWrite(ledRed, HIGH);
       digitalWrite(ledGreen, LOW);
       digitalWrite(Buzzer, HIGH);
  } else {
      digitalWrite(ledBlue, LOW); //Green becomes RGB LED color
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledRed, LOW);  
      digitalWrite(Buzzer, LOW);
  }     
  Serial.println(watermap);
  delay(1000);      
}

Sketch uses 522063 bytes (49%) of program storage space. Maximum is 1044464 bytes.
Global variables use 44192 bytes (53%) of dynamic memory, leaving 37728 bytes for local variables. Maximum is 81920 bytes.

This was the result when compiled for ESP8266.
other devices will compile fine and produce different size output.

Not clear to me if the redLed in the fire part is a discrete led, or is it the redLed of the RGB in the water part.

Do you really want the same buzzer to be an alert for two different things?

Again tested to compile. I did not bother to program a device.
Not sure if your buzzer is active high or low so make the required edits.
But the code will buzz for 100ms every 1 second if all works out.
Just expand the example to have more features as you require.

you should remove the "buzzer off" code in your main IF{}ELSE{} code.
Just leave that to the if(buzzerON){} stuff in first part of the loop.
That way you just detect the need in either the water or flame and set the timeout and turn ON there.
So if the Water is "buzz" but the flame is not then you still get a Buzz for the 100ms.

Have two flags to detect either and use PWM to create different noises even on a standard buzzer.

///////////////////////
//define RGB led
#define FlamePin  2   // This is for input pin
#define Buzzer    8   // Use buzzer for alert  << Change for FREE PIN
#define ledRed    8   // Conflict with Buzzer Pin RESELECT
#define ledGreen  9
#define ledBlue   10
#define redled    13
#define greenled  12

int Flame = HIGH;  // HIGH when FLAME is exposed

const int WaterLevel = A0; //Water Level Sensor to Arduino pin A0

int watervalue;
int watermap; //mapped value for water sensor
int minimumvalue = 100;
int maximumvalue = 750;
bool buzzerison = false;
uint32_t   buzzertime = 0;
uint32_t   lastActivity = 0;

void setup() {
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);

  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(ledBlue, OUTPUT);

  pinMode(Buzzer, OUTPUT);
  pinMode(FlamePin, INPUT);

  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  if (buzzerison){
    if((millis()-buzzertime) > 100){
      buzzerison = false;
      digitalWrite(Buzzer, LOW);      
    }
  }
  if((millis()-lastActivity) < 1000){return;} // This will return if last activity was less than 1 second ago.
  lastActivity = millis();
  Serial.print(F("Testing Pins @ "));
  Serial.println(String(lastActivity, DEC));
  Flame = digitalRead(FlamePin);
  
  if (Flame== HIGH) {
    Serial.println(Flame);
    
    buzzertime = millis();
    buzzerison = true;
    digitalWrite(Buzzer, HIGH);

    digitalWrite(redled, HIGH);
    digitalWrite(greenled, LOW);    
  } else {
    Serial.println((String) F("Flame is ") + Flame);  // People will Scream at you for using this but for testing no harm.
    buzzertime = 0;
    buzzerison = false;
    digitalWrite(Buzzer, HIGH);
    digitalWrite(Buzzer, LOW);
    digitalWrite(greenled, HIGH);
    digitalWrite(redled, LOW);
  }
  // read the input on analog pin 0:
  watervalue = analogRead(WaterLevel);
  watermap = map(watervalue, minimumvalue, maximumvalue, 0, 10);
  
  if (watermap >2 && watermap <= 4){
      digitalWrite(ledBlue, HIGH); //Blue-green becomes RGB LED color
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledRed, LOW);
  } else if (watermap > 4 && watermap <= 6){
      digitalWrite(ledBlue, HIGH); 
      digitalWrite(ledGreen, LOW); //Violet becomes RGB LED color 
      digitalWrite(ledRed, HIGH);
      digitalWrite(Buzzer, HIGH);
  } else if (watermap > 6 ){
       digitalWrite(ledBlue, LOW); //Red becomes RGB LED color
       digitalWrite(ledRed, HIGH);
       digitalWrite(ledGreen, LOW);
       digitalWrite(Buzzer, HIGH);
  } else {
      digitalWrite(ledBlue, LOW); //Green becomes RGB LED color
      digitalWrite(ledGreen, HIGH);
      digitalWrite(ledRed, LOW);  
      digitalWrite(Buzzer, LOW);
  }     
  Serial.println(watermap);
}

Sketch uses 252319 bytes (24%) of program storage space. Maximum is 1044464 bytes.
Global variables use 33068 bytes (40%) of dynamic memory, leaving 48852 bytes for local variables. Maximum is 81920 bytes.

and NO, I have no idea why this Sketch was half the size of the previous.
Perhaps I just copied the details from the wrong screen.
Who knows such things.?
Yep, that 520k looks more like the size of my WifiCan stuff.

Thanks a lot! It worked well.

Forgive me for my ignorance as a beginner, but what concept does it revolve around with? Is it only with the boolean and millis functions?

if I understand correctly,

The code just tests the Pins like it did before.
But now when you want to turn buzzer on you also save the MILLIS() and set the flag buzzeron.

Then next time that the program hits Loop again, it checks if buzzerON
if it is, then has the time you want expired? (newtime-oldtime = duration).

No- then keep buzzing, Yes- turn it off and reset flag.

Then check if you were here less than 1 second ago, Yes- just return from loop.
No- (last check was > 1000ms) then test the pins again.

round and round we go.

Would the code also follow the same principle if, for example, I want to connect more sensors?

I do NOT see a problem in that respect,

with little thought you can just add more "last" time holders for different timeouts.
and more "isON" flags depending on what you have in mind.

You can also do the same for the LED's.
turn them ON when you need and have flags or time based expire to turn off at the top of the loop().
if that's also what you need.

Have a look at the Tone examples for a quick way to have different alert sounds.

Expand that to 150 lines and drop back in for another check-up.

Hv.

So that's how it works. Thanks a lot again for sharing your expertise in the topic! It surely gave me a good learning experience on how to build it.