Unable to use Variable outside an IF statement

Hey guys so i got a project that requires the use of a CO2 sensor, the one i got is the 5% infrared CO2 Sensor by DF robot. The code below is the one i copied off thier site which works however I cannot call upon the “CO2 variable” outside the IF statement any help will be appreciated.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10); // RX, TX
unsigned char hexdata[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; //Read the gas density command /Don't change the order

void setup() {
  Serial.begin(9600);
  while (!Serial) {

  }
  mySerial.begin(9600);
}

void loop() {
   mySerial.write(hexdata,9);
   delay(500);
  
int CO2=0;

 for(int i=0,j=0;i<9;i++)
 {
  if (mySerial.available()>0)
  {
     long hi,lo,CO2;
     int ch=mySerial.read();

    if(i==2){     hi=ch;   }   //High concentration
    if(i==3){     lo=ch;   }   //Low concentration
    if(i==8) {
             float CO2=hi*256+lo;  
             Serial.print((CO2/10000)+0.8); //WORKS HERE
            
               }

  }
 

 }

Serial.print((CO2/10000)+0.8);// DOES NOT WORK HERE
 }

I get this as my Error message

exit status 1
'CO2' was not declared in this scope

co2_test_file.ino (800 Bytes)

You declare C02 as

int CO2=0;

Then you declare it again as a float

float CO2=hi*256+lo;

You need to decide what kind of variable C02 is meant to be then declare it once, probably before loop();

it's defined a 3rd time as a long

            long hi,lo,CO2;

It compiles for me; however, the CO2 that is in scope is an int and will always be 0. It is very confusing to have it declared as 3 different types in 3 different scopes.

You have 3 different variables named CO2

  int CO2 = 0;
float CO2 = hi * 256 + lo;
      long hi, lo, CO2;

Each has a different scope, which is something that you should read up on, and it is not good practice to have multiple variables with the same name as you have.

Serial.print((CO2 / 10000) + 0.8); // DOES NOT WORK HERE

C02 cannot be used here because, as you say, it was declared inside the code block for the if statement so has no meaning outside of it

The easiest thing to do is to declare C02 as a global variable at the start of the program then use it throughout the program but do not declare it again by preceding its name with a data type

FYI. The code the OP posted in the code block does not match the code OP attached, which is why it compiled for me.

Was going to say that’s a new one but it is not. :frowning:

While he’s learning, it may be worth reading up on indentation / Ctrl-T and basic C style.
They can make code debugging a lot easier.

Thank you all for your replies

How would you guys go about doing it
as i took this code off the distributor’s site and attempted to modify, to be honest i do not understand the code. This is the original code.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10); // RX, TX
unsigned char hexdata[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; //Read the gas density command /Don't change the order

void setup() {
  Serial.begin(9600);
  while (!Serial) {

  }
  mySerial.begin(9600);
}

void loop() {
   mySerial.write(hexdata,9);
   delay(500);
   

 for(int i=0,j=0;i<9;i++)
 {
  if (mySerial.available()>0)
  {
     long hi,lo,CO2;
     int ch=mySerial.read();

    if(i==2){     hi=ch;   }   //High concentration
    if(i==3){     lo=ch;   }   //Low concentration
    if(i==8) {
             float CO2=hi*256+lo;  //CO2 concentration
             Serial.print((CO2/10000)+0.8);
            
               }

  }
 

 }

 }

My goal here is to use the readings from the CO2 sensor to trigger a motor, but in order to do this i would need to be able to read the CO2 readings outside the loop. Any suggestions on how to do this? Keeping in mind i’m super new and just getting into it.

Thank youuu

jsj1:
How would you guys go about doing it

You are doing some undocumented monkey business with numbers input from serial... without knowing why that is, or exactly what else the program has to do, it would be based on guesswork (so not worth our time).

If I was given a complete description of the desired program behaviour, inputs and outputs, I could probably cook something up fairly fast.

I've looked at the existing logic, and it is quite strange really. Comments in the program would help enormously with that. There isn't a single comment anywhere.

aarg:
You are doing some undocumented monkey business with numbers input from serial... without knowing why that is, or exactly what else the program has to do, it would be based on guesswork (so not worth our time).

If I was given a complete description of the desired program behaviour, inputs and outputs, I could probably cook something up fairly fast.

I've looked at the existing logic, and it is quite strange really. Comments in the program would help enormously with that. There isn't a single comment anywhere.

Yeah i agree with you the coding is all over the place but this was the code that came with the product so that is why i'm having a hard time to amend it to fit my needs.

I basically want to take a reading from the CO2 sensor then use that value within an IF statement to control a motor.

You just need to follow up the comments that were made about the scope of a variable in reply #4. Then the answer will become obvious to you.

Edit - actually you were straight up told how to solve it. :slight_smile:

jsj1:
Thank you all for your replies

How would you guys go about doing it

Keeping in mind I'm super new and just getting into it.

If your knowledge is such that you can't spot that 3 declarations of the same variable is going to be a problem then you need to learn the basics of programming. If you are serious about this then work through the examples in the IDE, on this web site and elsewhere and get an understanding of C/C++ so you can read the code and understand it.

If you really don't want the bother of learning to program then ask a moderator to move this to gigs and collaborations and ask someone to do it for you, and expect to pay.

for(int i=0,j=0;i<9;i++)
 {
  if (mySerial.available()>0)
  {

This worries me.
That for loop will finish a lot quicker than you hope

TheMemberFormerlyKnownAsAWOL:

for(int i=0,j=0;i<9;i++)

{
  if (mySerial.available()>0)
  {


This worries me.
That for loop will finish a lot quicker than you hope

It’s also just weird. I mean, why?

Thank you Guys i'm a bit of dumb dumb but after reading up on global variables and if statements i finally got it to work.

So sorry for the rubbish code, still got loads to learn.

Please post your working code. There may be other ways to improve it and it will serve as an example for others who come across this thread

#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10); // RX, TX
unsigned char hexdata[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; //Read the gas density command /Don't change the order

float CO2;  //define CO2 as a global variable with float as i need the accuracy 
  
void setup() {
  Serial.begin(9600);
  while (!Serial) {
  }
  mySerial.begin(9600);
}


void loop() {
   mySerial.write(hexdata,9);
   delay(500);
   
for(int i=0,j=0;i<9;i++)
 {
  if (mySerial.available()>0)
  {
  long hi;
  long lo;
  int ch=mySerial.read();
    if(i==2){     hi=ch;   }   //High concentration
    if(i==3){     lo=ch;   }   //Low concentration
    if(i==8) {
             CO2=(hi*256+lo);
                        
               }
  }
 
 }
Serial.println(((CO2)/10000)); //converts CO2 from PPM to % 
 }

Are you sure about that?
That for loop still looks risky.

That for loop/code was given by the manufacturing company who made the CO2 sensor. So i guess it should be right.