-1

I am having issues with some code. In the CrossingSensor::loop() function, there is a line with: sonar.ping_timer(readSensor);.

I can call that fine, but in that function I can't access the status variable. What am I missing?

Error: error: 'status' was not declared in this scope status = on;

/*
  Name
*/
#ifndef CrossingSensor_h
#define CrossingSensor_h

#include "Arduino.h"

class CrossingSensor
{
  enum Status {off, on};

  public:
    CrossingSensor();
    void init();
    void loop();
    void start();
    void stop();
    Status status;
  private:
    unsigned long _startMillis;
};

#endif
/*
  NLE
*/

#include "Arduino.h"
#include "CrossingSensor.h"
#include <NewPing.h>

NewPing sonar(2,3,6);//trigger, echo, max distance
unsigned int pingSpeed = 500;// 50ms would be 20 times a second
unsigned long pingTimer;// Holds the next ping time


CrossingSensor::CrossingSensor()
{
  Serial.println("CrossingSensor: Init");
  //init();
}

void CrossingSensor::init()
{
  Serial.println("CrossingSensor: Init/Attach/Reset");
  pingTimer = millis();
}

void readSensor()
{
  if (sonar.check_timer())
  {
    int cm = sonar.ping_result / US_ROUNDTRIP_CM;

    if(cm > 0 && cm < 10)
    {
      //status = on;
    }
    else
    {
      //status = off;
    }
  }
}

void CrossingSensor::loop()
{
  if (millis() >= pingTimer)
  {
    pingTimer += pingSpeed;
    sonar.ping_timer(readSensor);
  }
}

void CrossingSensor::start()
{
  if(status == off)
  {
    Serial.println("CrossingSensor: Turning Sensor On");
    status = on;
  }
}

void CrossingSensor::stop()
{
  if(status == on)
  {
    Serial.println("CrossingSensor: Turning Sensor Off");
    status = off;
  }
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22
user597454
  • 11
  • 2
  • Do you declare `status` in one of the headers? it is not a part of `NewPing.h` as far as I can find in any documentation – Hawkeye5450 Dec 15 '20 at 03:23
  • @Hawkeye5450 thanks for response! I added the header to the original post for clarification. Appreciate the help. – user597454 Dec 15 '20 at 03:33
  • "sensor" is a member of class "CrossingSensor". You can't access a "sensor" field without having "CrossingSendor" object. It looks like maybe you meant for "readSensor()" to be a member function of CrossingSensor? If so, everything would work fine. – paulsm4 Dec 15 '20 at 03:39
  • Thanks @paulsm4 - do you meaning changing void readSensor() to void CrossingSensor::readSensor() ? When I tried that before I saw error: invalid use of non-static member function 'void CrossingSensor::readSensor()' sonar.ping_timer(readSensor); Thanks for help so far. – user597454 Dec 15 '20 at 03:56

2 Answers2

2

You never declared the function readSensor() as a part of CrossingSensor. Since it isn't a member function, it has no access to status.

All you should have to do is declare the function as a member of CrossingSensor or pass it a CrossingSensor object.

You would also need to change the .cpp file to reflect that change. The declaration for each would look like the following:


    class CrossingSensor
    {
        enum Status {off, on};

        public:
        CrossingSensor();
        void init();
        void readSensor();
        void loop();
        void start();
        void stop();
        Status status;
        private:
        unsigned long _startMillis;
    };

    #endif

This is your method declaration:


    void CrossingSensor::readSensor()
    {
        // your code here
    }

This makes readSensor a part of CrossingSensor as well as the other way around.

Hawkeye5450
  • 672
  • 6
  • 18
  • THANK you all so far. When I add void readSensor(); to the header file under public: I still get: error: 'status' was not declared in this scope status = on; Thoughts? Thanks guys. – user597454 Dec 15 '20 at 03:51
  • @user597454 Can you update the files to reflect this change? – Hawkeye5450 Dec 15 '20 at 16:22
1

The readSensor() function isn't part of CrossingSensor so it can't see CrossingSensor's status.

Make it part of Crossingsensor by declaring it there in your header file, and then define it as void CrossingSensor::readSensor(), in your code, and not as void readSensor().

Also, I don't know what sonar.ping_timer() takes as an argument, but I doubt it is readSensor. You will have to find another way of passing status to sonar.ping_timer(), if that is what you intended to do.

ocrdu
  • 2,172
  • 6
  • 15
  • 22
  • Thanks @ocrduo, the docs say: sonar.ping_timer(function); - Send a ping and call function to test if ping is complete. So it takes a function as its param. – user597454 Dec 15 '20 at 17:26
  • Isn't the function it calls supposed to return something? readSensor doesn't return anything, it is void. Should be in the documentation of NewPing. Also, it won't automatically find that function without your saying what object it is in; see https://stackoverflow.com/questions/682721/calling-member-functions-within-main-c Don't forget to vote and accept an answer, so the question doesn't stay open. – ocrdu Dec 15 '20 at 17:29