I've been working on a project that need two sensors(mq2 and mpu6050), I got each sketches from the internet. The thing is I want the two sensors to work on their own but in one sketch. When the mq2 senses some gas, I want the LED and BUZZER turned on and the only way to turn it off is by resetting the arduino. For the MPU6050, it's the same with the mq2, when the MP6050 is triggered, I want the other LED and the same buzzer turn on, again the only way to turn it off is by resetting the arduino. I want to combine these two skectches in one. But when I combined it, the circuit won't work. Could someone help me ? Plssssssss I really need it
I attached the 3 files
Here is the code that I modified and combined:
void setup() {
setupmq2();
setupaccel();
}
void loop() {
loopmq2();
loopaccel();
}
////////////////////////////////////////////////////////////
int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A0;
// Your threshold value
int sensorThres = 150;
void setupmq2() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
}
void loopmq2() {
int analogSensor = analogRead(smokeA0);
Serial.print("Pin A0: ");
Serial.println(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres)
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
noTone(buzzer);
}
}
///////////////////////////////////////////////////
#include <Wire.h>
#include <MPU6050.h>
#define minval -5
#define maxval 3
MPU6050 mpu;
void setupaccel()
{
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
// Initialize MPU6050
Serial.println("Initialize MPU6050");
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
}
mpu.setThreshold(3);
// Check settings
checkSettings();
}
void checkSettings()
{
Serial.println();
Serial.print(" * Sleep Mode: ");
Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");
Serial.print(" * Clock Source: ");
switch(mpu.getClockSource())
{
case MPU6050_CLOCK_KEEP_RESET:
Serial.println("Stops the clock and keeps the timing generator in reset");
break;
case MPU6050_CLOCK_EXTERNAL_19MHZ:
Serial.println("PLL with external 19.2MHz reference");
break;
case MPU6050_CLOCK_EXTERNAL_32KHZ:
Serial.println("PLL with external 32.768kHz reference");
break;
case MPU6050_CLOCK_PLL_ZGYRO:
Serial.println("PLL with Z axis gyroscope reference");
break;
case MPU6050_CLOCK_PLL_YGYRO:
Serial.println("PLL with Y axis gyroscope reference");
break;
case MPU6050_CLOCK_PLL_XGYRO:
Serial.println("PLL with X axis gyroscope reference");
break;
case MPU6050_CLOCK_INTERNAL_8MHZ:
Serial.println("Internal 8MHz oscillator");
break;
}
Serial.print(" * Gyroscope: ");
switch(mpu.getScale())
{
case MPU6050_SCALE_2000DPS:
Serial.println("2000 dps");
break;
case MPU6050_SCALE_1000DPS:
Serial.println("1000 dps");
break;
case MPU6050_SCALE_500DPS:
Serial.println("500 dps");
break;
case MPU6050_SCALE_250DPS:
Serial.println("250 dps");
break;
}
Serial.print(" * Gyroscope offsets: ");
Serial.print(mpu.getGyroOffsetX());
Serial.print(" / ");
Serial.print(mpu.getGyroOffsetY());
Serial.print(" / ");
Serial.println(mpu.getGyroOffsetZ());
Serial.println();
}
void loopaccel()
{
Vector rawGyro = mpu.readRawGyro();
Vector normGyro = mpu.readNormalizeGyro();
Serial.print(" Xraw = ");
Serial.print(rawGyro.XAxis);
Serial.print(" Yraw = ");
Serial.print(rawGyro.YAxis);
Serial.print(" Zraw = ");
Serial.println(rawGyro.ZAxis);
if (normGyro.XAxis > maxval ||
normGyro.XAxis < minval &&
normGyro.YAxis > maxval ||
normGyro.YAxis < minval &&
normGyro.ZAxis > maxval ||
normGyro.ZAxis < minval)
{
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
}
else
{
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
Serial.print(" Xnorm = ");
Serial.print(normGyro.XAxis);
Serial.print(" Ynorm = ");
Serial.print(normGyro.YAxis);
Serial.print(" Znorm = ");
Serial.println(normGyro.ZAxis);
}
Here is the code for the mq2 gas sensor:
/*******
All the resources for this project:
https://www.hackster.io/Aritro
*******/
int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
// Your threshold value
int sensorThres = 150;
void setup() {
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(smokeA0, INPUT);
Serial.begin(9600);
}
void loop() {
int analogSensor = analogRead(smokeA0);
Serial.print("Pin A0: ");
Serial.println(analogSensor);
// Checks if it has reached the threshold value
if (analogSensor > sensorThres)
{
digitalWrite(redLed, HIGH);
digitalWrite(greenLed, LOW);
tone(buzzer, 1000, 200);
}
else
{
digitalWrite(redLed, LOW);
digitalWrite(greenLed, HIGH);
noTone(buzzer);
}
delay(100);
}
And here is the code for the MPU6050:
#include <Wire.h>
#include <MPU6050.h>
#define minval -5
#define maxval 3
MPU6050 mpu;
void setup()
{
Serial.begin(115200);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
delay (2000);
// Initialize MPU6050
Serial.println("Initialize MPU6050");
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
mpu.setThreshold(3);
// Check settings
checkSettings();
}
void checkSettings()
{
Serial.println();
Serial.print(" * Sleep Mode: ");
Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");
Serial.print(" * Clock Source: ");
switch(mpu.getClockSource())
{
case MPU6050_CLOCK_KEEP_RESET:
Serial.println("Stops the clock and keeps the timing generator in reset");
break;
case MPU6050_CLOCK_EXTERNAL_19MHZ:
Serial.println("PLL with external 19.2MHz reference");
break;
case MPU6050_CLOCK_EXTERNAL_32KHZ:
Serial.println("PLL with external 32.768kHz reference");
break;
case MPU6050_CLOCK_PLL_ZGYRO:
Serial.println("PLL with Z axis gyroscope reference");
break;
case MPU6050_CLOCK_PLL_YGYRO:
Serial.println("PLL with Y axis gyroscope reference");
break;
case MPU6050_CLOCK_PLL_XGYRO:
Serial.println("PLL with X axis gyroscope reference");
break;
case MPU6050_CLOCK_INTERNAL_8MHZ:
Serial.println("Internal 8MHz oscillator");
break;
}
Serial.print(" * Gyroscope: ");
switch(mpu.getScale())
{
case MPU6050_SCALE_2000DPS:
Serial.println("2000 dps");
break;
case MPU6050_SCALE_1000DPS:
Serial.println("1000 dps");
break;
case MPU6050_SCALE_500DPS:
Serial.println("500 dps");
break;
case MPU6050_SCALE_250DPS:
Serial.println("250 dps");
break;
}
Serial.print(" * Gyroscope offsets: ");
Serial.print(mpu.getGyroOffsetX());
Serial.print(" / ");
Serial.print(mpu.getGyroOffsetY());
Serial.print(" / ");
Serial.println(mpu.getGyroOffsetZ());
Serial.println();
}
void loop()
{
Vector rawGyro = mpu.readRawGyro();
Vector normGyro = mpu.readNormalizeGyro();
Serial.print(" Xraw = ");
Serial.print(rawGyro.XAxis);
Serial.print(" Yraw = ");
Serial.print(rawGyro.YAxis);
Serial.print(" Zraw = ");
Serial.println(rawGyro.ZAxis);
if (normGyro.XAxis > maxval ||
normGyro.XAxis < minval &&
normGyro.YAxis > maxval ||
normGyro.YAxis < minval &&
normGyro.ZAxis > maxval ||
normGyro.ZAxis < minval)
{
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(300);
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
delay(300);
delay (1000);
}
else
{
digitalWrite(7,LOW);
digitalWrite(8,LOW);
}
Serial.print(" Xnorm = ");
Serial.print(normGyro.XAxis);
Serial.print(" Ynorm = ");
Serial.print(normGyro.YAxis);
Serial.print(" Znorm = ");
Serial.println(normGyro.ZAxis);
delay(10);
}
MQ2_gas_sensor_codeham.ino (821 Bytes)
acceloremeter_edited_from_net.ino (3.09 KB)
Merged.ino (3.8 KB)