Not very familiar with C++ - how do I fix this scope problem ?

Hi, I have some coding experience, but I am definitely not a c++ programmer.

I want to make a class library to more easily use Capacitive Sensors in my project which will use a lot of sensors. I looked at loads of Arduino guides and thought the easiest way was create a class in a library that also created the sensor and methods to do what I want.

The problem seems to be scope and I think I understand why this is not working - I need to pass in a reference to the sensor in the update method ? The problem is that I cannot find anything (that I understand) that tells me how I should do this.

Header File

/*
	CapSen sensor library a class for my
	capacitive sensor project. 
*/

#ifndef CapSen_h
#define CapSen_h

#include "Arduino.h"
#include "CapacitiveSensor.h"
class CapSen
{
  public:
    CapSen(int rPin, int gPin, int oPin, int iPin, long cSamp);
    void update(); //check sensor and take action
  private:
	int _rPin; //red led pin
	int _gPin; // green led pin
	long _cSamp; // number of samples for sensor
	int _cStatus; //status of sensor 0 - not lit , 1 - green lit, 2 - green and red lit
	unsigned long _tSet; //time last status change
	long _senValue; //value read from sensor
};

#endif

Cpp File

#include "Arduino.h"
#include "CapSen.h"
#include "CapacitiveSensor.h"


CapSen::CapSen(int rPin, int gPin, int oPin, int iPin, long cSamp)
{
	// set r & g pins as OUTPUT
	pinMode(rPin, OUTPUT);
	pinMode(gPin, OUTPUT);	
	
	CapacitiveSensor cs = CapacitiveSensor(oPin,iPin);
	_cStatus = 0;
	_senValue = 0;
	_tSet = 0;	
	_rPin = rPin;
	_gPin = gPin;
	_cSamp = cSamp;
}

// test sensor and act on reading
void CapSen::update()
{
	 // call capacitive sensor with sample # from constructor and assign to _senValue
	 _senValue = cs.CapacitiveSensor(_cSamp);
	 
	 // test senValue for change above min threshold
	 if (_senValue > 20) {
		 // we have a touch, check _cStatus and _tSet
		 if (_cStatus = 0){
			 // it's off so change -cStatus, set _tSet and turn on green led
			 _cStatus = 1;
			 _tSet = millis();
			 digitalWrite(_gPin, HIGH);			 
		 } else if (_cStatus = 1) {
			 // green is on, check time since it was set and if  > 5 seconds switch green led off otherwise turn on red LED
				if (millis() > (_tSet + 5000)) {
					_cStatus = 0;
					_tSet = 0;
					digitalWrite(_gPin, LOW);
				} else { // second touch within 5 seconds, turn on red led
					_cStatus = 2;
					_tSet = millis();
					digitalWrite(_rPin, HIGH);
				}
			} else if (_cStatus = 2) {
				// turn red & green leds off
				_cStatus = 0;
				_tSet = 0;
				digitalWrite(_gPin, LOW);
				digitalWrite(_rPin, LOW);
				}
	 }
}

There is also a keyword file and everything is in a folder under library with the same names

Simplified sketch with one sensor
Breadboard image attached.

/*
 * sketch to test CapSen class library
 * 
 */
#include <CapSen.h>
// instantiate a CapSen object passing in red pin, green pin, source pin, sink pin, samples
CapSen cs1 = CapSen(5,6,2,12,5);
 
void setup() {
  
}

void loop() {
 cs1.update();
 delay(20);
}

I hope this makes sense, how do I fix the error;
C:\Users\Home\Documents\Arduino\libraries\CapSen\CapSen.cpp: In member function 'void CapSen::update()':

C:\Users\Home\Documents\Arduino\libraries\CapSen\CapSen.cpp:24:15: error: 'cs' was not declared in this scope

_senValue = cs.CapacitiveSensor(_cSamp);

I know this code isn't optimal, but just need a fix, thanks.
Dave.

You define cs with this statement in CapSen():

CapacitiveSensor cs = CapacitiveSensor(oPin,iPin);

but what happens to cs when the end of the CapSen() method is reached?

econjack:
You define cs with this statement in CapSen():

CapacitiveSensor cs = CapacitiveSensor(oPin,iPin);

but what happens to cs when the end of the CapSen() method is reached?

I have really misunderstood something in that case, isn't cs created in the class when I create an instance and part of the instantiated object until I destroy the instance ?

Should I just inherit CapacitiveSensor to a new class and add my update method ?

Declare cs as a member of the class rather than in the constructor as you are now. You can still initialize it in the constructor.

wildbill:
Declare cs as a member of the class rather than in the constructor as you are now. You can still initialize it in the constructor.

Thanks, but I can't get this to work, so think I am now going to try something else.

Add the line

CapacitiveSensor cs;

In your private section then in your constructor include the line

cs = CapacitiveSensor(oPin,iPin);

Should work then...

Thanks, I think I have already tried that, I've tried so many different things I'm starting to think Python on a Pi and a touch screen may be easier :-0

C:\Users\Home\Documents\Arduino\libraries\CapSen\CapSen.cpp: In constructor 'CapSen::CapSen(int, int, int, int, long int)':

C:\Users\Home\Documents\Arduino\libraries\CapSen\CapSen.cpp:6:66: error: no matching function for call to 'CapacitiveSensor::CapacitiveSensor()'

CapSen::CapSen(int rPin, int gPin, int oPin, int iPin, long cSamp)

Just about ready to give up on the Arduino completely.

In Python

class CapSense():
  def __init__ (self, .......):
    self.cs = CapacitiveSensor()

  # rather than
  def __init__ (self, .......):
    cs = CapacitiveSensor()

So the same issue applies in basically the same way - its all the same stuff really.

Thanks for the suggestions - I think I have this solved now, embarrassing rookie error - I had not noticed the method to read the sensor value should be a lower case C "capacitiveSensor", not "CapacitiveSensor" - going away now to sit quietly in the corner...

Everyone makes those "flat forehead" mistakes...you know, where you slam the heel of your hand into your forehead while muttering "How could I be so stupid!!". Most of us have very flat foreheads...welcome to the group.