Go Down

Topic: How can I count an event? (Read 16832 times) previous topic - next topic

XOIIO

Apr 02, 2013, 08:34 am Last Edit: Apr 02, 2013, 08:37 am by XOIIO Reason: 1
Alright so for my first project I am going to be using an attiny for a touch sensor, and leds will light up depending how many times it detects a touch (or button press or whatever).

I thought that this might work but sadly it did not.

Code: [Select]
#include <CapacitiveSensor.h>

CapacitiveSensor   cs_1_2 = CapacitiveSensor(1,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int ledpin = 0;
int countnumber = 0;


void setup()                    
{
 cs_1_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
 pinMode (ledpin, OUTPUT);
}

void loop()                    
{
 long start = millis();
 long total1 =  cs_1_2.capacitiveSensor(30);
 if (total1 < 99) {
 
 }
 if (countnumber = 1) {
   digitalWrite(ledpin, HIGH);
 }
}



The code checked out fine but it didn't do squat.

Basically I am looking for something like var in batch pogramming, and using multiple loops or something like that. Var would start as 0, if it got touched once it would change to 1, and the first led would light up, and it would move to the next loop, if it was touched a second time var would change again, the next led would light up and so forth.

I'm having trouble with this so any help is greatly appreciated. I've tried all number of things using stuff I've seen in other codes but it refuses to work.

AWOL

Code: [Select]
if (countnumber = 1) {
C/C++ programming 101: The assignment  vs. equality operators

GoForSmoke

The only capacitive sensor I've used was made of tin foil and paper by myself. I connected it to an UNO and played around with it watching the data on the serial monitor. To cut down on the print flow I only printed data that varied from the last value by more than 10 or 20 points yet what I saw showed me that I could tell when I actually touched the sensor as opposed to when my finger came close. The data changed all the way down to the touch and continued to change if I put more pressure on. The way I could tell touch from non-touch was a sudden jump to lower values.

From what I saw, making a debounce routine would take more work than for a button or switch and that routine take longer to detect a touch.

How does your capacitive sensor compare? Did you make it or buy it?
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

XOIIO


The only capacitive sensor I've used was made of tin foil and paper by myself. I connected it to an UNO and played around with it watching the data on the serial monitor. To cut down on the print flow I only printed data that varied from the last value by more than 10 or 20 points yet what I saw showed me that I could tell when I actually touched the sensor as opposed to when my finger came close. The data changed all the way down to the touch and continued to change if I put more pressure on. The way I could tell touch from non-touch was a sudden jump to lower values.

From what I saw, making a debounce routine would take more work than for a button or switch and that routine take longer to detect a touch.

How does your capacitive sensor compare? Did you make it or buy it?



I made one using the instructions from the capsense library page, it works great, and I can use it to trigger an LED just fine.

This is my first project though, I haven't looked into arduino code before today, that's why I'm having trouble.

XOIIO


Code: [Select]
if (countnumber = 1) {
C/C++ programming 101: The assignment  vs. equality operators


is arduino coding C? I just replied to the other guy, first time touching arduino code today, only other programming experience is batch and a bit of vbs.


nickgammon


is arduino coding C?


Yes, I believe I might have mentioned that in the "sticky" at the start of this forum section.

http://arduino.cc/forum/index.php/topic,97455.0.html

Point 1.

It's amazing how much time you can save by reading all the "read me" stuff.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

XOIIO



is arduino coding C?


Yes, I believe I might have mentioned that in the "sticky" at the start of this forum section.

http://arduino.cc/forum/index.php/topic,97455.0.html

Point 1.

It's amazing how much time you can save by reading all the "read me" stuff.


well a combination of being up over 24 hours, on pain killers and wisdom teeth kicking my ass I'm not looking around as much as a normally would be.

Anyways, I am still looking for a solution to this.

AWOL

I'd add some debug prints - no point in trying black-box debugging in a new environment.

XOIIO


I'd add some debug prints - no point in trying black-box debugging in a new environment.


By that are you saying that the code I have should be working?

UKHeliBob

Have you fixed the = instead of == bug ?
Inserting Serial.prints at strategic points in your code will let you find out if it is doing what you think it should and help track down problems.  For instance, what is the value of countnumber just before the if statement ?  Does it vary ?  Is it ever 1 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

XOIIO


Have you fixed the = instead of == bug ?
Inserting Serial.prints at strategic points in your code will let you find out if it is doing what you think it should and help track down problems.  For instance, what is the value of countnumber just before the if statement ?  Does it vary ?  Is it ever 1 ?



I'm working on getting the serial thing to work, I keep getting "Serial" was not defined in this scope.

I'm not sure what you are reffering to with the == bug

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

AWOL

Quote
By that are you saying that the code I have should be working?

I have no idea - I don't have your hardware or your libraries and I can't see how you have wired it up, but I'd certainly be putting in some indicators to show me that what I'm doing is returning results that look sensible. Like debug prints.

XOIIO


Quote
By that are you saying that the code I have should be working?

I have no idea - I don't have your hardware or your libraries and I can't see how you have wired it up, but I'd certainly be putting in some indicators to show me that what I'm doing is returning results that look sensible. Like debug prints.


well I still haven't gotten past that darn "Serial" was not defined in this scope message, I'm not sure why it won't work properly, I did fix the "==" thing.

I'm thinking that maybe serial print doesn't work with an attiny85?

Go Up