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.
