having some issues running capacitive sensor on attiny. I got the library to work fine and everything seems kosher, except for some reason the loop only happens once if the capacitive sensing leds (2,3) are connected. As soon as I disconnect them from the resistors they work fine and the program runs as expected (I can even turn it on and off by touching the capacitive sensor led). I did simple tests and LED functions as well as capacitive functions all work fine alone, its when they are together that this weird things happens.
here are two screen shots of how I have it connected:
http://hellowoo.com/uploads/capsense.jpghttp://hellowoo.com/uploads/capsense2.jpgthis is the code:
#include <CapacitiveSensor.h>
// do a fade up every "x" amount of LED cycles...
// make sure it's not multiple of 3 else it will be same LED everytime
const byte fadeUpEvery = 2,
fadeInSpeed = 5,
sampleAmount = 5,
capacitiveSensorOutput = 2,
capacitiveSensorInput = 3;
CapacitiveSensor cs_4_2 = CapacitiveSensor(capacitiveSensorOutput,capacitiveSensorInput);
long runningAverage[sampleAmount];
boolean toggleLED,
isOn;
int highLow,
delayTime,
numberTimes,
counter,
ledNumCount;
unsigned long makeLightningTimer;
int led[] = { 0, 1, 4 };
const int delayMin = 4000,
delayMax = 9000;
void setup(){
pinMode(capacitiveSensorOutput, OUTPUT);
digitalWrite(capacitiveSensorOutput, HIGH);
pinMode(capacitiveSensorInput, INPUT);
digitalWrite(capacitiveSensorInput, LOW);
isOn = true;
toggleLED = false;
highLow = LOW;
counter = 0;
makeLightningTimer = millis();
ledNumCount = 0;
for(int i = 0; i < 3; i++){
pinMode(led[i],OUTPUT);
}
}
void loop(){
if(isOn == true){
flicker(led[ledNumCount], 2, 500);
makeLightning();
}
checkIfisOn();
}
////////////////
// Check if hand is present
//--------------
int lastReading;
void checkIfisOn(){
const int threshold = 18;
int currentReading = getAverage();
if((currentReading-lastReading) > threshold){
isOn = !isOn;
AllLEDsOnOff(1);
if(isOn == true){
fadeAllLedsIn();
cycleLEDs(5);
AllLEDsOnOff(2);
} else { AllLEDsOnOff(3); fadeAllLedsOut(); }
}
lastReading = currentReading;
}
////////////////
// get average reading out of X samples
//--------------
int getAverage() {
int tempAverage = 0;
int capacitiveAverage = 0;
for(int i = 0; i < sampleAmount; i++){
runningAverage[i] = cs_4_2.capacitiveSensor(10);
}
// add all the samples up
for(int i = 0; i < sampleAmount; i++){
tempAverage = runningAverage[i] + tempAverage;
}
// divide by number of samples to get average
capacitiveAverage = tempAverage/sampleAmount;
return capacitiveAverage;
}
////////////////
// LED functions
//--------------
void fadeAllLedsIn() {
for(int i = 0; i < 3; i++){
for(int j = 0; j < 255; j++){
analogWrite(led[i],j);
delay(fadeInSpeed);
}
}
}
void fadeAllLedsOut() {
for(int j = 255; j > 0; j--){
for(int i = 0; i < 3; i++){
analogWrite(led[i],j);
delay(fadeInSpeed);
}
}
digitalWrite(led[0], LOW); digitalWrite(led[1], LOW); digitalWrite(led[2], LOW);
}
void AllLEDsOnOff(int numTimes) {
int isHighLow = HIGH;
numTimes = (numTimes)*2;
for(int i = 0; i < numTimes; i++){
for(int j = 0; j < 3; j++){
digitalWrite(led[j], isHighLow);
}
delay(600);
isHighLow = (isHighLow == LOW)? HIGH : LOW;
}
}
void cycleLEDs(int numTimesToCycle){
const int setupLedSpeed = 80;
int isHighLow = LOW;
for(int i = 0; i < numTimesToCycle*2; i++){
for(int j = 0; j < 3; j++){
digitalWrite(led[j], isHighLow);
delay(setupLedSpeed);
isHighLow = (isHighLow == HIGH)? LOW : HIGH;
}
}
}
void fade(int pinNumber){
//fade in
for(int j = 0; j < 255; j++){
analogWrite(pinNumber,j);
delay(fadeInSpeed);
}
flicker(pinNumber, 3, 50);
//fade out
for(int j = 255; j > 0; j--){
analogWrite(pinNumber,j);
delay((fadeInSpeed/4));
}
digitalWrite(pinNumber, LOW); // just in case pin doesn't go all the way low
}
void makeLightning(){
int delayInterval = random(delayMin,delayMax);
int timeDiff = millis() - makeLightningTimer;
if( timeDiff >= delayInterval){
int numberTimes = random(1, 4);
int delayTime = random(30, 100);
// normal flicker
if(counter != fadeUpEvery){
flicker(led[ledNumCount], numberTimes, delayTime);
}
// every "x" do a fade in flicker
if(counter == fadeUpEvery){
counter = 0;
fade(led[ledNumCount]);
}
ledNumCount++;
if(ledNumCount >= 3) {
ledNumCount = 0;
}
}
makeLightningTimer = millis();
counter++;
}
void flicker(int ledPin, int numberOfFlickers, int betweenDelayTime){
for(int i = 0; i < numberOfFlickers; i++){
digitalWrite(ledPin, LOW);
delay(betweenDelayTime);
digitalWrite(ledPin,HIGH);
delay(betweenDelayTime);
digitalWrite(ledPin, LOW);
}
}
I'm assuming it's some sort of variable I am missing in the electronics vs the code though. any ideas?
EDIT: also forgot to mention I can still turn it on and off, its just the makeLightning function only seems to work once. If I turn it off then on via capacitive "button" it will also only run once.