CO2 sensor MHZ17 error ppm not declared in this scope

I have a MHZ19 and I got it working only with the serial plotter and monitor.
I want to make leds blink. but dont know how.
At first I have the working version. then with an addition arduino says PPM is not declared.
I cant continu.
Who knows the anser.

Working:

#include <SoftwareSerial.h>;

SoftwareSerial mySerial(A0, A1); // connect also co2sensor-ardunoUno17 with Tx-A0,Rx-A1,Vin-5v,GND-GND ,Old bootloader in my case

byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
unsigned char response[9];

void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

void loop()
{
mySerial.write(cmd, 9);
memset(response, 0, 9);
mySerial.readBytes(response, 9);
int i;
byte crc = 0;
for (i = 1; i < 8; i++) crc+=response*;*

  • crc = 255 - crc;*
  • crc++;*
  • if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {*
  • Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));*
  • } else {*
  • unsigned int responseHigh = (unsigned int) response[2];*
  • unsigned int responseLow = (unsigned int) response[3];*
    _ unsigned int ppm = (256*responseHigh) + responseLow;_
  • Serial.println(ppm);*
  • }*
  • delay(10000);*
    }
    Not working. ppm was not declared??
    #include <SoftwareSerial.h>;
    SoftwareSerial mySerial(A0, A1); // connect also co2sensor-ardunoUno17 with Tx-A0,Rx-A1,Vin-5v,GND-GND ,Old bootloader in my case
    byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
    unsigned char response[9];
    void setup() {
  • Serial.begin(9600);*
  • mySerial.begin(9600);*
    }
    void loop()
    {
  • mySerial.write(cmd, 9);*
  • memset(response, 0, 9);*
  • mySerial.readBytes(response, 9);*
  • int i;*
  • byte crc = 0;*
    _ for (i = 1; i < 8; i++) crc+=response*;_
    _
    crc = 255 - crc;_
    _
    crc++;_
    _
    if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {_
    _
    Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));_
    _
    } else {_
    _
    unsigned int responseHigh = (unsigned int) response[2];_
    _
    unsigned int responseLow = (unsigned int) response[3];_
    _ unsigned int ppm = (256responseHigh) + responseLow;_

    * Serial.println(ppm);*
    * }*
    * delay(10000);*
    * // nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn*
    * if(ppm == 0) {*
    * Serial.println(“PANIC!!”);*
    * }*
    * if(ppm < 500) {*
    * Serial.println(“Oh oh,… we have less!!”);*
    * }*
    * if(ppm > 500) {*
    * Serial.println(“Woohoo - we are rich!!”);*
    * }*
    //mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
    * Serial.println("-----------");*
    * delay(5000);*
    }

ppm is defined within the braces of the else statements, that limits it scope.

it can’t be referenced outside the braces.

   } else {
        ...
        unsigned int ppm = (256*responseHigh) + responseLow;
        ...
    }

    ...

    if(ppm == 0) {

Hi, Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum". OR http://forum.arduino.cc/index.php/topic,148850.0.html. Then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How do you want to make the LEDs blink?

Tom... :)

Report to moderator is not the correct way to add a post

instead of the words panic ,enoug CO2 Or Ahigh level off CO2 (ppm) I want to activate the digital out ports. But when I write my code at once it wil have errors. I want to make small program steps.

maybe this ihas a better layout.

But how do I put my ppm variable in the declarations.

not working code (error ppm is not declared in this scope.

#include <SoftwareSerial.h>;

SoftwareSerial mySerial(A0, A1); // connect also co2sensor-ardunoUno17 with  Tx-A0,Rx-A1,Vin-5v,GND-GND  ,Old bootloader in my case

byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; 
unsigned char response[9];

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() 
{
  mySerial.write(cmd, 9);
  memset(response, 0, 9);
  mySerial.readBytes(response, 9);
  int i;
  byte crc = 0;
  for (i = 1; i < 8; i++) crc+=response[i];
  crc = 255 - crc;
  crc++;

   if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
    Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));
  } else {
    unsigned int responseHigh = (unsigned int) response[2];
    unsigned int responseLow = (unsigned int) response[3];
    unsigned int ppm = (256*responseHigh) + responseLow;
    Serial.println(ppm);
  }
  delay(10000);
  // nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
   if(ppm == 0) {
    Serial.println("PANIEK!!");
  }

  if(ppm < 500) {
    Serial.println("Oh oh,... we hebben maar weinig geld!!");
  }

  if(ppm > 500) {
    Serial.println("Woohoo - we zijn rijk!!");
  }


//mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
    Serial.println("-----------");
    delay(5000);
}

working code…

#include <SoftwareSerial.h>;

SoftwareSerial mySerial(A0, A1); // connect also co2sensor-ardunoUno17 with  Tx-A0,Rx-A1,Vin-5v,GND-GND  ,Old bootloader in my case

byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; 
unsigned char response[9];

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() 
{
  mySerial.write(cmd, 9);
  memset(response, 0, 9);
  mySerial.readBytes(response, 9);
  int i;
  byte crc = 0;
  for (i = 1; i < 8; i++) crc+=response[i];
  crc = 255 - crc;
  crc++;

   if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
    Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));
  } else {
    unsigned int responseHigh = (unsigned int) response[2];
    unsigned int responseLow = (unsigned int) response[3];
    unsigned int ppm = (256*responseHigh) + responseLow;
    Serial.println(ppm);
  }
  delay(10000);
}

in your working code, you’re not trying to reference ppm outside the else braces.

define ppm – unsigned int ppm; – at the beginning of loop() after it’s brace ({) and set the value of ppm with defining it as int within the else

void loop()
{
    unsigned int ppm;
    ...
    } else {
        ...
        ppm = (256*responseHigh) + responseLow;
        ...
    }

    ...
    if(ppm == 0) {
        Serial.println("PANIEK!!");
    }
    ...

I am going to try that. thanks already for the quick help.

Ok. :) Only " unsigned int ppm; " in my scriot and the error was gone. The topic can be closed. or how does it work.

We don't close the topic as it may be helpful to others but you might like to edit your original post and add [SOLVED] to the subject line