Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

[SOLVED] Using CapTouch lib from a class

$
0
0

Hi,

 

I am working on a wireless touch controller for 433Mhz dimmer modules.

The idea is simple. When a touch button is touched a 433Mhz command is send to a wireless dimmer module. 

A single or double tap switches the module on or off, holding it will cycle it from dim-bright-dim until the button is released.

 

For this I am using the CapTouch library posted in library section of this forum.

 

I have create a class for this to enable me to quickly add more sensors. 

However when I initialise more than one class the code doesn't work anymore. And I can't figure out why.

The code still compiles but a touch is registered constantly. (all lights are on)

 

Can anyone look in my code and give me some pointers?

#include <RemoteTransmitter.h>
#include <CapTouch.h>

//KaKuTransmitter kaKuTransmitter(11);

#ifndef touchDimmer_h
#define touchDimmer_h
class touchDimmer {
public:
  touchDimmer(char housecode, byte unit_number, byte pin);
  void begin();
  void check();
private:
  long _lTimeHigh;
  long _lHighTimer;
  long _lLowTimer;
  long _lTimeLow;
  long _lTimer;
  int _iPulse;
  int _iNumReadings;
  int _iReadings[10];      // the readings from the analog input
  int _iIndex;                  // the index of the current reading
  int _iTotal;                  // the running total
  float _average;              // the average 
  boolean _bCurState;
  boolean _bPrevState;
  boolean _bDimmerMode;
  boolean _bButtonState;
  int _iHighPulse;		// Time in millis that is nessesary to count as high
  int _iLowPulse;		// Time in millis that is nessesary to count as low
  byte _housecode;
  byte _unit_number;
  byte _pin;
  //    protected:
  CapTouch* button;
};
#endif

touchDimmer::touchDimmer(char housecode, byte unit_number, byte pin) 
{
  _housecode = housecode;
  _unit_number = unit_number;
  _pin = pin;
  button = new CapTouch(_pin,TOUCH_BUTTON);
}

void touchDimmer::begin()
{
  _lTimeHigh = 0;
  _lHighTimer = 0;
  _lLowTimer = 0;
  _lTimeLow = 0;
  _lTimer = 0;
  _iPulse = 0;
  _iNumReadings = 10;
  _iReadings[_iNumReadings];
  _iIndex = 0;      
  _iTotal = 0;         
  _average = 0;         
  _bCurState = false;
  _bPrevState = false;
  _bDimmerMode = false;
  _bButtonState = false;
  _iHighPulse = 20;
  _iLowPulse = 20;
  for (int thisReading = 0; thisReading < _iNumReadings; thisReading++) {
    _iReadings[thisReading] = 0;
  }
};

void touchDimmer::check()
{
  _lTimer = millis();
  _iTotal= _iTotal - _iReadings[_iIndex];  
  _iReadings[_iIndex] = button->isTouched();
  _iTotal= _iTotal + _iReadings[_iIndex]; 
  _iIndex = _iIndex + 1;
  if (_iIndex >= _iNumReadings)              
    _iIndex = 0;                           
  _average = _iTotal / _iNumReadings;  
  if(_iTotal == _iNumReadings) {
    _bCurState = HIGH;
    if(_bCurState != _bPrevState) {
      _lTimeHigh = _lTimer;
      digitalWrite(RED_LED,HIGH);
      if((_lHighTimer % _iHighPulse) == 0) {
        if(_bCurState != _bPrevState) {
          _iPulse++;
          _bPrevState = _bCurState;
          digitalWrite(RED_LED,HIGH);
        }     
      }     
    }
    _lHighTimer = _lTimer - _lTimeHigh;
    if((_lHighTimer > 100)) {
      if(_bDimmerMode == LOW) { 
        Serial.println("Dimmer ON");
        _bDimmerMode = HIGH;
        _iPulse = 0;
        digitalWrite(GREEN_LED, HIGH);
//        kaKuTransmitter.sendSignal(_housecode, _unit_number, true);
      }
    } 
  }

  if(_iTotal == 0) {
    _bCurState = LOW;
    _lTimeLow = _lTimer;
    _lLowTimer = _lTimer - _lTimeLow;
    if (_bDimmerMode == HIGH) {
      _bDimmerMode = LOW;
      digitalWrite(GREEN_LED, LOW);
//      kaKuTransmitter.sendSignal(_housecode, _unit_number, true);
    }
    if (((_lTimer - _lTimeHigh) > 300) && (_iPulse > 0)){
      switch(_iPulse){
      case 1:
//        kaKuTransmitter.sendSignal(_housecode, _unit_number, true);
        break;
      case 2:
//        kaKuTransmitter.sendSignal(_housecode, _unit_number, false);
        break;
      }    
      _iPulse = 0;
    }
    if((_lLowTimer % _iLowPulse) == 0) {
      if(_bCurState != _bPrevState) {
        _bPrevState = _bCurState;
        digitalWrite(RED_LED,LOW); // Light the led
      }        
    }
  }
}
touchDimmer test = touchDimmer('N',15,10);
touchDimmer test2 = touchDimmer('N',15,5);
void setup() {
  pinMode(RED_LED, OUTPUT); 
  pinMode(GREEN_LED, OUTPUT);
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(RED_LED, LOW);
  test.begin();
  test2.begin();
}
void loop() {
  test.check();
  test2.check();
} 

Viewing all articles
Browse latest Browse all 2077

Trending Articles