Explain the code

Can anyone please explain the following code to me, I’m super interested. I already run it and everything works perfectly, I just want to know what the programmer did there.

#include <Servo.h>
Servo myservo;
int flame = 10;
int smoke = 9;
int red = 6;
int green = 7 ;
int buzzer = 13 ;
void setup ()
{
  pinMode (flame, INPUT) ;
  pinMode (smoke, INPUT) ;
  pinMode (red, OUTPUT) ;
  pinMode (green, OUTPUT) ;
  pinMode (buzzer, OUTPUT) ;
  Serial.begin(9600);
  myservo.attach(8);
  myservo.write(0);
}

void loop ()
{
  int fval = digitalRead (flame) ;
  int sval = digitalRead(smoke);
  Serial.print("fval = ");
  Serial.println(fval);
  Serial.print("sval = ");
  Serial.println(sval);

  if ( sval == HIGH or fval == HIGH)
  {
    Serial.println(" WARNING! ");
    digitalWrite(red, HIGH);
    digitalWrite(buzzer, HIGH);
    digitalWrite(green, LOW);
    myservo.write(180);
  }
  else
  {
    Serial.println(" SAFE ");
    digitalWrite(red, LOW);
    digitalWrite(green, HIGH);
    digitalWrite(buzzer, LOW);
    myservo.write(0);
  }
  delay(100);
}

What part of the code do you not understand.

Please do not say everything.

Since it works, perhaps first explain in detail to the forum what the code actually does ?

Do you want to know how it works out of curiosity or to learn to program?
If it is to learn, I suggest looking for an Arduino programming course.
On the internet there are several free ones.

RV mineirin

I’m just curious that’s all. And I’d love to explain how it works but believe I don’t know, that’s why I’m asking you to explain it to me. I just saw it on youtube and I wanted to try new things, and this got my attention. I already build the circuit In proteus following the programer of the code instructions. and I wrote the code too but I wanted to know what did he wrote there and what basically every part doing.
I want to know how the Gas flame detector works.

It’s a code for Gas flame detector

I’m defiantly curious that’s all. I’m not trying to program, I Just want to know how it works and what did he does.

I’m saying everything cuz i absolutely have no idea about the code. I’m new to this, I’m just curious. I’ve read about the structures and the libraries but still nth help me to get what he wrote there. I’m just looking for a simple and good explanation of what he did there.

The sketch monitors inputs connected to the the Arduino. Based on the state of these inputs the code makes decisions using if( ) and then sends control signals to the sketches outputs.

This happens over and over as the loop() function will loop back and be executed over and over.

1 Like

Thanks a lot

Maybe adding more meaningful names and a few comments will make the code clearer:

#include <Servo.h>
Servo myservo;

// Assign names to pin numbers
const int RedLEDOutputPin = 6;
const int GreenLEDOutputPin = 7;
const int ServoControlPin = 8;
const int SmokeInputPin = 9;
const int FlameInputPin = 10;
const int BuzzerOutputPin = 13 ;

// Assign names to useful constants
const int SerialBaudRate = 9600;
const int ServoPositionAlarm = 180;  // Degrees
const int ServoPositionSafe = 0;  // Degrees

void setup ()
{
  pinMode (FlameInputPin, INPUT) ;
  pinMode (SmokeInputPin, INPUT) ;
  
  pinMode (RedLEDOutputPin, OUTPUT) ;
  pinMode (GreenLEDOutputPin, OUTPUT) ;
  pinMode (BuzzerOutputPin, OUTPUT) ;
  
  Serial.begin(SerialBaudRate);
  
  myservo.attach(ServoControlPin);
  myservo.write(ServoPositionSafe);
}

void loop ()
{
  int flameInput = digitalRead (FlameInputPin);
  int smokeInput = digitalRead(SmokeInputPin);
  Serial.print("flameInput = ");
  Serial.println(flameInput);
  Serial.print("smokeInput = ");
  Serial.println(smokeInput);

  if ( smokeInput == HIGH or flameInput == HIGH)
  {
    Serial.println(" WARNING! ");
    digitalWrite(RedLEDOutputPin, HIGH);
    digitalWrite(BuzzerOutputPin, HIGH);
    digitalWrite(GreenLEDOutputPin, LOW);
    myservo.write(ServoPositionAlarm);
  }
  else
  {
    Serial.println(" SAFE ");
    digitalWrite(RedLEDOutputPin, LOW);
    digitalWrite(GreenLEDOutputPin, HIGH);
    digitalWrite(BuzzerOutputPin, LOW);
    myservo.write(ServoPositionSafe);
  }
  delay(100);  // milliseconds
}
1 Like

Unfortunately, it’s not my code. I’m just curious to know what every part does. And this code is for a Gas flame detector.

@shesherees, your topic was moved to a more suitable location on the forum.

If you have zero experience in programming, you should watch some of the basic programming videos on YouTube.

Go to YouTube and search:

Arduino basic programming

For your sincerity in the answer, I will try to explain the operation describing in the lines of the program.

#include <Servo.h>    // Include Servo.h Lib to control servo motor
//                    // A LIB is a "piece" of a program that has information already
Servant myservo;      // Create an instance called myservo for my servo motor. Instance is a program object
int flame = 10;       // Create a variable named flame of the int format and put the value 10 inside it
//                    // It is also a way of associating a name with an arduino pin
int smoke = 9;        // Create a smoke variable of the int format and place the value 9 inside it
//                    // It is also a way to associate a name with an arduino pin
int red = 6;          // Create a red name variable of the int format and put the value 6 inside it
//                    // It is also a way to associate a name with an arduino pin
int green = 7;        // Create a green name variable of the int format and place the value 7 inside it
//                    // It is also a way of associating a name with an arduino pin
int buzzer = 13;      // Create a variable name buzzer of the int format and put the value 13 inside it
//                    // It is also a way to associate a name with an arduino pin

void setup ()         // Mandatory routine that runs only once when turning on or pressing reset
{
  pinMode (flame, INPUT);   // Defines that the flame name pin will be an input pin
  pinMode (smoke, INPUT);   // Defines that the pin named smoke will be an input pin
  pinMode (red, OUTPUT);    // Defines that the smoke name pin will be an input pin
  pinMode (green, OUTPUT);  // Defines that the smoke name pin will be an input pin
  pinMode (buzzer, OUTPUT); // Defines that the smoke name pin will be an input pin
  Serial.begin (9600);      // Initializes the serial at 9600 BPS
  myservo.attach (8);       // Informs that the servo motor is connected to pin 8
  myservo.write (0);        // Position the servo in the central position (0)
}

void loop ()                          // Mandatory routine that always runs
{
  int fval = digitalRead (flame);     // Create a variable of the fval name of the int format and store in it the level read from the flame pin
  int sval = digitalRead (smoke);     // Create a sval variable of the int format and keep the level read from the smoke pin in it
  Serial.print ("fval =");            // Print on the fval serial monitor
  Serial.println (fval);              // Print the value of the variable fval on the serial monitor
  Serial.print ("sval =");            // Print on the fval serial monitor
  Serial.println (sval);              // Print the value of the sval variable on the serial monitor

  if (sval == HIGH or fval == HIGH)   // If the value of sval HIGH or if the value of fval is HIGH
  {
    Serial.println ("WARNING!");      // Prints WARNING on serial monitor
    digitalWrite (red, HIGH);         // Change the level of the red pin to HIGH
    digitalWrite (buzzer, HIGH);      // Change the level of the buzzer pin to HIGH (sounds the buzzer)
    digitalWrite (green, LOW);        // Change the level of the green pin to LOW
    myservo.write (180);              // Rotate the servo 180 degrees
  }
  else                                // Otherwise
  {
    Serial.println ("SAFE");          // Prints SAFE on serial monitor
    digitalWrite (red, LOW);          // Change the level of the red pin to LOW
    digitalWrite (green, HIGH);       // Change the level of the green pin to HIGH
    digitalWrite (buzzer, LOW);       // Change the level of the buzzer pin to LOW
    myservo.write (0);                // Rotate the servo to 0 degrees
  }
  delay (100);                        // Wait 100 milliseconds
}
1 Like

This is a fire and smoke detector.
This program reads a value on two pins from the arduino.
These pins are: flame and smoke.
Flame and smoke sensors must be connected to these pins.
If flame or smoke is detected, it turns on a red LED, turns off the green LED, activates the alert buzzer and moves the servo motor to a 180 degree position.
If nothing is detected, it turns on the green LED, turns off the red LED and silences the buzzer, and moves a servo motor to the 0 degree position.
Waits 100 milliseconds and resumes scanning.

RV mineirin

1 Like

I appreciate you so much because I understand it very well and I find it as easy and not complicated as I thought it will be and this is because of your great explanation.

Tks