here is all the updated code if it helps, main ino file:
#include <CapacitiveSensor.h>
#include "Letter.h"
const byte capSenseSend = 2;
const byte numberOfSensors = 2;
Letter letters[numberOfSensors]; // a-z
const byte sensorPin[] = { 22, 23 };
const byte ledPin[] = { 8 };
int allValues[numberOfSensors];
void setup(){
Serial.begin(19200);
delay(100);
CapacitiveSensor cs_sensor[numberOfSensors];
for(int i = 0; i < numberOfSensors; i++){
cs_sensor[i] = CapacitiveSensor(capSenseSend,sensorPin[i]);
}
// setup LEDs
for(int i = 0; i < numberOfSensors; i++){
pinMode(ledPin[i],OUTPUT);
digitalWrite(ledPin[i], LOW);
}
//cs_a.set_CS_AutocaL_Millis(0xFFFFFFFF);
setSampleSize();
}
void loop(){
setSensorValue();
checkIfButtonPressed();
delay(100);
}
void setSampleSize(){
for(int i = 0; i < numberOfSensors; i++){
letters[i].setSampleAmount(5);
}
}
void setSensorValue(){
for(int i = 0; i < numberOfSensors; i++){
letters[i].setCurrentReading(cs_sensor[i].capacitiveSensor(50));
}
}
void checkIfButtonPressed(){
for(int i = 0; i < numberOfSensors; i++){
letters[i].checkIfIsOn(cs_sensor[i]);
}
}
void fadeLedIn(int pinNumber){
for(int j = 0; j <= 255; j+=10){
analogWrite(pinNumber,j);
delay(10);
}
}
void fadeLedOut(int pinNumber){
for(int j = 255; j >= 0; j-=10){
analogWrite(pinNumber,j);
delay(10);
}
digitalWrite(pinNumber, LOW);
}
void sendSerialData(int buttonTouched){
delay(10); Serial.print("Data,");
delay(10); Serial.print(buttonTouched);
delay(10); Serial.print(",");
Serial.print("sensors : ");
Serial.print(allValues[0]); Serial.print(" , "); Serial.print(allValues[1]); delay(10);
Serial.print("\n");
delay(10);
}
Letter.h
#ifndef LETTER_H
#define LETTER_H
class Letter {
public:
Letter();
double getAverage(int sample);
void setSampleAmount(int changeSampleTo);
void setLastReading(int theLastReading);
void setCurrentReading(int theCurrentReading);
void checkIfisOn(int curentSensorValue[]);
int getCurrentReading();
int getLastReading();
int aValue;
int lastReading;
int currentReading;
private:
int _sampleAmount;
double _tempAverage,
_capacitiveAverage;
double _runningAverage[100];
};
#endif
Letter.ino
#include "Letter.h" // "" for files within same folder as sketch...?
Letter::Letter() {
_sampleAmount = 5;
_tempAverage = 0;
_capacitiveAverage = 0;
lastReading = 0;
currentReading = 0;
//long _runningAverage[_sampleAmount];
};
void Letter::setSampleAmount(int changeSampleTo){
_sampleAmount = changeSampleTo;
};
void Letter::setLastReading(int theLastReading){
lastReading = theLastReading;
}
void Letter::setCurrentReading(int theCurrentReading){
currentReading = theCurrentReading;
}
int Letter::getLastReading(){
return lastReading;
}
int Letter::getCurrentReading(){
return currentReading;
}
double Letter::getAverage(int sample) {
for(int i = 0; i < _sampleAmount; i++){
_runningAverage[i] = sample;
}
// reset _tempAverage
_tempAverage = 0;
// add all the samples up
for(int i = 0; i < _sampleAmount; i++){
_tempAverage += _runningAverage[i];
}
// divide by number of samples to get average
_capacitiveAverage = _tempAverage/_sampleAmount;
return _capacitiveAverage;
}
void Letter::checkIfisOn(){
boolean firstTime = true;
int firstRelease = 0;
const int threshold = 55;
int differenceReading = curentSensorValue-lastReading;
if(differenceReading > threshold && firstTime == true){
sendSerialData(i);
fadeLedIn(ledPin[i]);
if((currentReading-lastReading) < threshold){
digitalWrite(ledPin[i], LOW);
fadeLedOut(ledPin[i]);}
lastReading[i] = currentReading[i];
firstTime = false;
}
}