Different behaviour with battery

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
ruperty
 
Posts: 49
Joined: Sun Mar 01, 2015 7:41 am

Different behaviour with battery

Post by ruperty »

I am finding things work differently when I have my flora project run through the battery rather than through the computer usb. Is there a reason why this should happen?

To be more specific I have three (code below is for two sets) strips of conductive fabric for which I want to detect a stroking rate, which then activates a vibrating disc. With the usb I only have to touch one strip for the presses to be detected, but with just the battery I have to touch two strips at the same time.

Why should the battery/usb make a difference?

Here's some code:

Code: Select all

 
boolean currentPressed[ ] = {false, false, false, false, false, false};
#define THRESH 0
... loop code
for (int i=0;i<6;i++) {
    long total =  pins[i].readPin(400); 
    if ((total > THRESH) && (!currentPressed[i])) {
      currentPressed[i] = true;
      lastpress = millis();
      numPressed ++;
      strokingRate += 2;
    } 
    else if ((total <= THRESH) && (currentPressed[i])) {
      currentPressed[i] = false;
      numPressed --;
    }
    
    if (currentPressed[i]) {
      if (numPressed > 0) 
        total = (int) (total / numPressed);
      if (total > 128) total = 128;  
     }  
  }

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Different behaviour with battery

Post by adafruit_support_mike »

How does your .readPin() function generate a value?

User avatar
ruperty
 
Posts: 49
Joined: Sun Mar 01, 2015 7:41 am

Re: Different behaviour with battery

Post by ruperty »

adafruit_support_mike wrote:How does your .readPin() function generate a value?
It is a function from the CapSense library.

The code is taken from your sample.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Different behaviour with battery

Post by adafruit_support_mike »

That library does a primitive version of capacitive sensing. It works, but the values it produces are sensitive to things like the supply voltage and the high/low input thresholds for the microcontroller's input pin.

That being the case, you have to tune the code for a specific set of operating conditions, then stick with those conditions if you want the output to remain stable.

User avatar
ruperty
 
Posts: 49
Joined: Sun Mar 01, 2015 7:41 am

Re: Different behaviour with battery

Post by ruperty »

adafruit_support_mike wrote:That being the case, you have to tune the code for a specific set of operating conditions, then stick with those conditions if you want the output to remain stable.
I have been trying to sort this out, but not sure what to tune.

I have a number of pieces of fabric buttons, with the code below to vibrate a disc when a button is touched. Powered by usb if I touch one button the disc vibrates, as expected.

On the other hand, if powered by the battery and I touch one button the disc does not vibrate. However, if I touch two buttons at the same time then it does vibrate. Could it be something to do with grounding?

Code: Select all

#include <CapPin.h>
 
CapPin cPin_10 = CapPin(10);     
CapPin cPin_9  = CapPin(9);        
CapPin cPin_12 = CapPin(12);     
CapPin cPin_0  = CapPin(0);        
CapPin cPin_2  = CapPin(2);       
CapPin cPin_3  = CapPin(3);       
 
CapPin pins[] = {cPin_12, cPin_9, cPin_10, cPin_3, cPin_2, cPin_0};
boolean currentPressed[ ] = {false, false, false, false, false, false};
#define THRESH 0
#define VIBRATEON true

void setup()
{
  pinMode(6,OUTPUT); 
  Serial.begin(115200);
  Serial.println("start");
}
 
void loop(){ 
  
  for (int i=0;i<6;i++) {
    long total =  pins[i].readPin(1000); 

    if ((total > THRESH) && (!currentPressed[i])) {
      Serial.print ("--> "); Serial.print (i); Serial.print (" "); Serial.println (total);
      currentPressed[i] = true;
      writeToVibrate(total);
    }else if ((total <= THRESH) && (currentPressed[i])) {
      currentPressed[i] = false;
      Serial.print (i); Serial.print (" "); Serial.println (0);
      writeToVibrate(0);
    }
  }
  
}

void writeToVibrate(int value){
    if(VIBRATEON)
      digitalWrite (6,value);    
}

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Different behaviour with battery

Post by adafruit_support_mike »

What values are you getting from the sampling loop when you touch zero, one, or two pads?

User avatar
ruperty
 
Posts: 49
Joined: Sun Mar 01, 2015 7:41 am

Re: Different behaviour with battery

Post by ruperty »

adafruit_support_mike wrote:What values are you getting from the sampling loop when you touch zero, one, or two pads?
Up to the value passed into readPin. Below are some samples of individual presses with the button number indicated. If I keep my finger down then the value is around the maximum, e.g. 985.

--> 0 349
--> 0 788
--> 0 1
--> 0 958
--> 0 989
--> 0 935
--> 0 971
--> 1 10
--> 1 969
--> 1 981
--> 1 646
--> 1 991
--> 1 986
--> 1 853
--> 1 151
--> 2 860
--> 2 970
--> 2 750
--> 2 859
--> 2 722
--> 2 988
--> 2 386
--> 2 4

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Different behaviour with battery

Post by adafruit_support_mike »

Okay, based on the code above you'll want to set the threshold value around 250-300.

User avatar
ruperty
 
Posts: 49
Joined: Sun Mar 01, 2015 7:41 am

Re: Different behaviour with battery

Post by ruperty »

adafruit_support_mike wrote:Okay, based on the code above you'll want to set the threshold value around 250-300.
Ok, I tried that but it didn't make any difference; with the battery, pressing one fabric button doesn't turn on the vibrating disc.

I tried 100 as well. What is this value anyway, could you explain what readPin is doing?

User avatar
Lakitna
 
Posts: 100
Joined: Tue Sep 09, 2014 10:02 am

Re: Different behaviour with battery

Post by Lakitna »

readPin reads the voltage present on the pin and converts it to a digital value. In this case the voltage is coming from the capacitive sensor. But due to the nature of this voltage measurement it's not displayed in voltage, but in levels. The ADC (Analog to Digital Converter) in Arduino's is 10-bits meaning that it can read 1024 levels where 5v = 1023 and 0v = 0. So when you're setting the threshold to 100 you are actually setting it to (5v / 1024) * 100 = 0,49v or about half a volt.

The best thing you can do is just to try and find the right threshold value while your project is connected to battery power. Trail and error can be very annoying but also very useful.

User avatar
ruperty
 
Posts: 49
Joined: Sun Mar 01, 2015 7:41 am

Re: Different behaviour with battery

Post by ruperty »

Lakitna wrote:readPin reads the voltage present on the pin and converts it to a digital value. In this case the voltage is coming from the capacitive sensor. But due to the nature of this voltage measurement it's not displayed in voltage, but in levels. The ADC (Analog to Digital Converter) in Arduino's is 10-bits meaning that it can read 1024 levels where 5v = 1023 and 0v = 0. So when you're setting the threshold to 100 you are actually setting it to (5v / 1024) * 100 = 0,49v or about half a volt.

The best thing you can do is just to try and find the right threshold value while your project is connected to battery power. Trail and error can be very annoying but also very useful.
Thanks. I am getting there but still not quite understanding what is going on. Is it measuring the voltage from the battery, or a finger? Does touching the fabric button increase or decrease the voltage? My battery is 4.5v isn't that the same as the usb? Should I be looking for a threshold that is greater or less than the input voltage (so far I've tried 100, 250, 400 and 1000 but none of them work)?

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Different behaviour with battery

Post by adafruit_support_mike »

The sensor is measuring the amount of time it takes to discharge the capacitance of the touchpad. Your finger increases the capacitance, which increases the discharge time.

User avatar
Lakitna
 
Posts: 100
Joined: Tue Sep 09, 2014 10:02 am

Re: Different behaviour with battery

Post by Lakitna »

For finding the threshold, maybe it's a good idea to hook a potentiometer to your project and use its output as threshold value. You can then turn the potmeter and find the approx threshold value.

The code below shows you how to do this when the potentiometer is connected to A0. The threshold value with this setup can be anywhere between 0 and 1023. If you need more simply add *2 to the first line of the loop.

Code: Select all

#include <CapPin.h>
 
int threshold;
int potMeterPin = A0;

CapPin cPin_10 = CapPin(10);     
CapPin cPin_9  = CapPin(9);        
CapPin cPin_12 = CapPin(12);     
CapPin cPin_0  = CapPin(0);        
CapPin cPin_2  = CapPin(2);       
CapPin cPin_3  = CapPin(3);       
 
CapPin pins[] = {cPin_12, cPin_9, cPin_10, cPin_3, cPin_2, cPin_0};
boolean currentPressed[ ] = {false, false, false, false, false, false};
#define THRESH 0
#define VIBRATEON true

void setup()
{
  pinMode(6,OUTPUT); 
  Serial.begin(115200);
  Serial.println("start");
}
 
void loop(){ 
  threshold = analogRead(potMeterPin);

  for (int i=0;i<6;i++) {
    long total =  pins[i].readPin(1000); 

    if ((total > threshold) && (!currentPressed[i])) {
      Serial.print ("--> "); Serial.print (i); Serial.print (" "); Serial.println (total);
      currentPressed[i] = true;
      writeToVibrate(total);
    }else if ((total <= threshold) && (currentPressed[i])) {
      currentPressed[i] = false;
      Serial.print (i); Serial.print (" "); Serial.println (0);
      writeToVibrate(0);
    }
  }
  
}

void writeToVibrate(int value){
    if(VIBRATEON)
      digitalWrite (6,value);    
}

User avatar
ruperty
 
Posts: 49
Joined: Sun Mar 01, 2015 7:41 am

Re: Different behaviour with battery

Post by ruperty »

adafruit_support_mike wrote:The sensor is measuring the amount of time it takes to discharge the capacitance of the touchpad. Your finger increases the capacitance, which increases the discharge time.
What does it mean to "discharge the capacitance"?

User avatar
ruperty
 
Posts: 49
Joined: Sun Mar 01, 2015 7:41 am

Re: Different behaviour with battery

Post by ruperty »

Lakitna wrote:For finding the threshold, maybe it's a good idea to hook a potentiometer to your project and use its output as threshold value. You can then turn the potmeter and find the approx threshold value.
Thanks, I don't have a potentiometer.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”