Pages: [1]   Go Down
Author Topic: attiny loop issue  (Read 651 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.jpg
http://hellowoo.com/uploads/capsense2.jpg

this is the code:
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.
« Last Edit: March 10, 2013, 03:09:37 am by hilukasz » Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 2
Posts: 138
W9LZ
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

millis declaration? In the .h file?  CapacitiveSensor?????cs_4_2 = CapacitiveSensor(capacitiveSensorOutput,capacitiveSensorInput); 
Do you need an underscore or no space where the blank space (?????) is?
« Last Edit: March 09, 2013, 06:31:17 pm by Learning » Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

millis declaration?

not following.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 2
Posts: 138
W9LZ
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Aren't you doing a call here?  int timeDiff = millis() - makeLightningTimer;  Where is millis() coming from?
Logged

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Aren't you doing a call here?  int timeDiff = millis() - makeLightningTimer;  Where is millis() coming from?

http://arduino.cc/en/Reference/millis its built function.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Offline Offline
Full Member
***
Karma: 3
Posts: 188
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

millis declaration? In the .h file?  CapacitiveSensor?????cs_4_2 = CapacitiveSensor(capacitiveSensorOutput,capacitiveSensorInput); 
Do you need an underscore or no space where the blank space (?????) is?

CapacitiveSensor is a class in the header file that's linked.
Logged

for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

alabama
Offline Offline
Full Member
***
Karma: 1
Posts: 183
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hilukasz,
This line
Code:
CapacitiveSensor cs_4_2 = CapacitiveSensor(capacitiveSensorOutput,capacitiveSensorInput);
sets you pins properly and initialises  the library.
They are set for pins 4 and 2, but you are setting up for pins 3 and 4.
Check the example included with the library for the right way to set it up.
I don't initialize or set up the pins in setup at all, the library does it, just like in the example;
Here is the code I use for my ATTiny85 sensors over I2c, with auto calibration disabled
for continuous readings.
Code:

#include <TinyWireS.h>
#include <CapacitiveSensor.h>


int i2c_regs[2]; //  register array
// Tracks the current register pointer position
byte reg_position;
CapacitiveSensor    cs_3_4 = CapacitiveSensor(3,4);   //Pin 3 is sensor, resistor between 3&4,see above, other sensor is grounded
void setup() {
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  TinyWireS.begin(4);                         // change for each sensor (subtract one for "row" in reader function for this sensor)
  TinyWireS.onRequest(requestEvent);      // register event
}

void loop() {  
  int level;
  long start = millis();
  level =  cs_3_4.capacitiveSensorRaw(200);
  //level = int(level);
  i2c_regs[0] = highByte(level);             //these two lines split the integer into bytes and store in array  i2c_regs[1] = lowByte(level);
  i2c_regs[1] = lowByte(level);
  }

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent()
{
    TinyWireS.send(i2c_regs[reg_position]);// respond with message of 2 bytes
  // as expected by master
    // Increment the reg position on each read, and loop back to zero
    reg_position = (reg_position+1) % 2;        
}
TomJ
« Last Edit: March 09, 2013, 10:21:37 pm by Tumbleweed » Logged

Einstein once said you don't really understand anything until you can explain it to your Grandmother

Pages: [1]   Go Up
Jump to: