0

bine

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);

#define sensor A0 
#define led 13  
#define ledd 8  // d out from mq3 
#define buz 9   
#define motor 10 
void setup() 
{
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.print("ALCOHOL MONITORING"); 
  lcd.setCursor(0,1);
  lcd.print(" VEHICLE SYSTEM ");
  delay(2000);
  pinMode(sensor, INPUT); 
  pinMode(buz, OUTPUT); 
  pinMode(led, OUTPUT);  
  pinMode(ledd, OUTPUT); 
  pinMode(motor, OUTPUT); 
  lcd.clear();
}

void loop() 
{
  float adcValue=0;
  for(int i=0;i<10;i++)
  {
    float actualanalogReading =  analogRead(sensor); 
    adcValue = adcValue + actualanalogReading; 
    Serial.println("In loop counter >"); // logging
    Serial.print(i);
    Serial.print(" input reading : ");
    Serial.print(actualanalogReading);  
    Serial.print(" cumulative input reading : ");
    Serial.print(adcValue);  
    
    delay(10);
  }
    float v= (adcValue/10) * (3.0/1024.0); 
    float mgL= 0.67 * v;
    Serial.println("");
    Serial.print("Post calculation v value >" );
    Serial.print(v);
    Serial.print(" mg/L > ");
    Serial.print(mgL);  
    Serial.println("");
    Serial.print("BAC:"); 
    Serial.print(mgL);
    Serial.print(" mg/L"); 
    lcd.setCursor(0,0);
    lcd.print("BAC: "); 
    lcd.print(mgL,4);
    lcd.print(" mg/L        "); 
    lcd.setCursor(0,1);
    if(mgL > 0.8)   
    { 
      lcd.print("Drunk ENGINE OFF ");   
      Serial.println("Drunk ENGINE OFF ");  
      digitalWrite(buz, HIGH);
      digitalWrite(led, HIGH);
      digitalWrite(ledd, LOW);
      digitalWrite(motor, LOW);
    }
    else  
    {
      lcd.print("Normal ENGINE ON "); 
      Serial.println(" Normal ENGINE ON "); 
      digitalWrite(buz, LOW);
      digitalWrite(led, LOW);
      digitalWrite(ledd, HIGH);
      digitalWrite(motor, HIGH);
    }

    delay(100);
    
}

top code^^^ combines with this>>

const int LED = A5; 
const int DO = 6; 
void setup() { 
 Serial.begin(9600); 
 pinMode(LED, OUTPUT); 
 pinMode(DO, INPUT); 
} 
void loop() { 
 int alarm = 0; 
 float sensor_volt; 
 float RS_gas; 
 float ratio; 
//-Replace the name "R0" with the value of R0 in the demo of First Test -/ 
 float R0 = 0.10; 
 int sensorValue = analogRead(A1); 
 sensor_volt = ((float)sensorValue / 1024) * 5.0; 
RS_gas = (5.0 - sensor_volt) / sensor_volt; // Depend on RL on yor module 
 ratio = RS_gas / R0; // ratio = RS/R0 
//------------------------------------------------------------/ 
 Serial.print("sensor_volt = "); 
 Serial.println(sensor_volt); 
 Serial.print("RS_ratio = "); 
 Serial.println(RS_gas); 
 Serial.print("Rs/R0 = "); 
 Serial.println(ratio); 
 Serial.print("\n\n"); 
 alarm = digitalRead(DO); 
 if (alarm == 1) digitalWrite(LED, LOW); 
 else if (alarm == 0) digitalWrite(LED, HIGH); 
 delay(1000); 
}
6
  • 1
    Do you know how both "codes" work?
    – Majenko
    Sep 24 '20 at 12:57
  • @majenko both works just need to know how to combine them Sep 24 '20 at 14:11
  • 1
    I didn't ask if they both worked. I asked if you knew how they both worked. If you don't know how they work then there is no way you will be able to combine them.
    – Majenko
    Sep 24 '20 at 14:13
  • 2
    What exactly is your problem in combining them? With what terms aren't you familiar? I will gladly answer your questions, but honestly I don't want to write the code for you. There is no simple answer to your current questions (other than the one I linked), because it is a rather broad question unless we do all your work for you. As Majenko stated, the key in combining 2 sketches is to understand how they work. Then you can decide how the final program should work and write a new code with fitting snippets of the previous codes
    – chrisl
    Sep 24 '20 at 14:40
  • 1
    And you should look into the BlinkWithoutDelay example, which comes with the Arduino IDE. It shows how you can use the millis() function to write non-blocking code without delay() calls. It is an important principle to learn and may be really helpful when combining your sketches
    – chrisl
    Sep 24 '20 at 14:42
2

While I'd like to direct you to ways to set up a nice tasking system with a frame counter, your system is likely simple enough that it is unnecessary. You can likely just replace the delay(1000); of the second program with approximately 5 calls of the first program's loop. May you instead learn something when cleaning up the following (which is literally a copy paste with minimal changes).


#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);

#define sensor A0 
#define led 13  
#define ledd 8  // d out from mq3 
#define buz 9   
#define motor 10 
void setup1() 
{
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.print("ALCOHOL MONITORING"); 
  lcd.setCursor(0,1);
  lcd.print(" VEHICLE SYSTEM ");
  delay(2000);
  pinMode(sensor, INPUT); 
  pinMode(buz, OUTPUT); 
  pinMode(led, OUTPUT);  
  pinMode(ledd, OUTPUT); 
  pinMode(motor, OUTPUT); 
  lcd.clear();
}

void loop1() 
{
  float adcValue=0;
  for(int i=0;i<10;i++)
  {
    float actualanalogReading =  analogRead(sensor); 
    adcValue = adcValue + actualanalogReading; 
    Serial.println("In loop counter >"); // logging
    Serial.print(i);
    Serial.print(" input reading : ");
    Serial.print(actualanalogReading);  
    Serial.print(" cumulative input reading : ");
    Serial.print(adcValue);  
    
    delay(10);
  }
    float v= (adcValue/10) * (3.0/1024.0); 
    float mgL= 0.67 * v;
    Serial.println("");
    Serial.print("Post calculation v value >" );
    Serial.print(v);
    Serial.print(" mg/L > ");
    Serial.print(mgL);  
    Serial.println("");
    Serial.print("BAC:"); 
    Serial.print(mgL);
    Serial.print(" mg/L"); 
    lcd.setCursor(0,0);
    lcd.print("BAC: "); 
    lcd.print(mgL,4);
    lcd.print(" mg/L        "); 
    lcd.setCursor(0,1);
    if(mgL > 0.8)   
    { 
      lcd.print("Drunk ENGINE OFF ");   
      Serial.println("Drunk ENGINE OFF ");  
      digitalWrite(buz, HIGH);
      digitalWrite(led, HIGH);
      digitalWrite(ledd, LOW);
      digitalWrite(motor, LOW);
    }
    else  
    {
      lcd.print("Normal ENGINE ON "); 
      Serial.println(" Normal ENGINE ON "); 
      digitalWrite(buz, LOW);
      digitalWrite(led, LOW);
      digitalWrite(ledd, HIGH);
      digitalWrite(motor, HIGH);
    }

    delay(100);
    
}

const int LED = A5; 
const int DO = 6; 
void setup() { 
 setup1(); //Serial.begin(9600);
 pinMode(LED, OUTPUT); 
 pinMode(DO, INPUT); 
} 
void loop() { 
 int alarm = 0; 
 float sensor_volt; 
 float RS_gas; 
 float ratio; 
//-Replace the name "R0" with the value of R0 in the demo of First Test -/ 
 float R0 = 0.10; 
 int sensorValue = analogRead(A1); 
 sensor_volt = ((float)sensorValue / 1024) * 5.0; 
RS_gas = (5.0 - sensor_volt) / sensor_volt; // Depend on RL on yor module 
 ratio = RS_gas / R0; // ratio = RS/R0 
//------------------------------------------------------------/ 
 Serial.print("sensor_volt = "); 
 Serial.println(sensor_volt); 
 Serial.print("RS_ratio = "); 
 Serial.println(RS_gas); 
 Serial.print("Rs/R0 = "); 
 Serial.println(ratio); 
 Serial.print("\n\n"); 
 alarm = digitalRead(DO); 
 if (alarm == 1) digitalWrite(LED, LOW); 
 else if (alarm == 0) digitalWrite(LED, HIGH); 

 for(int dd = 0; dd < 5; dd++) loop1();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.