Go Down

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

nickgammon

Where did that library come from?

Code: [Select]

#include <CapacitiveSensor.h>


How to use this forum


Sore tooth or not, you are making us work hard to help you.
Please post technical questions on the forum, not by personal message. Thanks!

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

nickgammon

http://arduino.cc/forum/index.php/topic,148850.msg1118324.html#post_tips

Quote

If you are using a library that does not come with the IDE (in other words, you downloaded it from somewhere) please post a link to this library.

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

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

Coding Badly

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


Use this core...
http://code.google.com/p/arduino-tiny/
...and a TTL-serial-to-USB converter.

Even better, use this version of the ArduinoISP sketch...
https://github.com/Coding-Badly/TinyISP
...with this library...
https://github.com/Coding-Badly/TinyDebugKnockBang

XOIIO


http://arduino.cc/forum/index.php/topic,148850.msg1118324.html#post_tips

Quote

If you are using a library that does not come with the IDE (in other words, you downloaded it from somewhere) please post a link to this library.




Well here it is, although I don't see how the library relates to my question, which is just asking for a simple way to count an event or use a variable to count.
http://playground.arduino.cc//Main/CapacitiveSensor?from=Main.CapSense

XOIIO


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


Use this core...
http://code.google.com/p/arduino-tiny/
...and a TTL-serial-to-USB converter.

Even better, use this version of the ArduinoISP sketch...
https://github.com/Coding-Badly/TinyISP
...with this library...
https://github.com/Coding-Badly/TinyDebugKnockBang



I am using that core, but I'll look into that other arduino isp sketch, I used this tutorial http://hlt.media.mit.edu/?p=1695

Coding Badly

I am using that core...


In which case, the board you have selected is not from that core.

XOIIO


I am using that core...


In which case, the board you have selected is not from that core.



hmm took a closer look I guess I just had an older one, I'll give it another go

XOIIO

#22
Apr 02, 2013, 10:39 am Last Edit: Apr 02, 2013, 10:41 am by XOIIO Reason: 1
Well no serial errors this time, however I'm not getting anything on the serial monitor :/ I'll keep trying though.

I'm not sure but maybe since I am programming through a mega that's interfering somehow.

nickgammon


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.


Can you post your amended code please?
Please post technical questions on the forum, not by personal message. Thanks!

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

XOIIO



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.


Can you post your amended code please?


Sure.

Code: [Select]
#include <SoftwareSerial.h>
#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);
  Serial.begin(9600);
}

void loop()                   
{
  long start = millis();
  long total1 =  cs_1_2.capacitiveSensor(30);
  if (total1 < 99) {
   digitalWrite(ledpin, HIGH);
   Serial.print("touched");
  } else {
  digitalWrite(ledpin, LOW);
}
}



I've found that if I don't have digitalWrite(ledpin, LOW) in the else statement it does not work for some reason, it seems to me it would set the led pin as high and leave it.

I've also been trying a different approach to changing the count number.

Code: [Select]
#include <SoftwareSerial.h>
#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);
  Serial.begin(9600);
}

void loop()                   
{
  long start = millis();
  long total1 =  cs_1_2.capacitiveSensor(30);
  if (total1 < 99) {
   countnumber++;
   Serial.print("touched");
  }
  if (countnumber = 1) {
  digitalWrite(ledpin, HIGH);
  } else {
    digitalWrite(ledpin, LOW);
}
}



It seems to be after trying this (among other combinations) countnumber just won't change, even using "==". I've determined that because if I set the number it looks to as 0, it turns the LED on, if I change the countnumber to be 1 initially and leave the second half, it doesn't turn on even though they are set to be the same.

AWOL

Quote
I've found that if I don't have digitalWrite(ledpin, LOW) in the else statement it does not work for some reason

I think you're confusing "it doesn't work" with "it doesn't look like it is working".
If the digitalWrite isn't there, there is nothing to turn the LED off once it is lit.

XOIIO


Quote
I've found that if I don't have digitalWrite(ledpin, LOW) in the else statement it does not work for some reason

I think you're confusing "it doesn't work" with "it doesn't look like it is working".
If the digitalWrite isn't there, there is nothing to turn the LED off once it is lit.


Well yes, so would it not just stay on once it has been touched? With both there it turns on when touched, and turns off when you stop touching it but with only digitalWrite(ledpin, HIGH) the led does not turn on whatsoever.


anyways, one last thing before I pass out, I've tried this, and the LED just stays on, I'm unsure why. Still no luck getting serial readings.

Code: [Select]
#include <SoftwareSerial.h>
#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);
  Serial.begin(9600);
}

void loop()                   
{
  long start = millis();
  long total1 =  cs_1_2.capacitiveSensor(30);
  if (total1 < 99) {
   countnumber++;
   Serial.print("touched");
  }
  if (countnumber == 1) {
  digitalWrite(ledpin, HIGH);
  } else {
    digitalWrite(ledpin, LOW);
}
}


UKHeliBob

Some questions/observations
How is the LED wired ?
Which pin(s) does the serial connection use on the Arduino that you are using (pin 0 by any chance ?)
The ledpin will only go HIGH when countnumber is exactly zero.  How often will that happen ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

GoForSmoke


...and a TTL-serial-to-USB converter.


In a pinch, have the Tiny connect RX and TX to an Arduino board running SoftSerial.

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


Some questions/observations
How is the LED wired ?
Which pin(s) does the serial connection use on the Arduino that you are using (pin 0 by any chance ?)
The ledpin will only go HIGH when countnumber is exactly zero.  How often will that happen ?


I have the attiny hooked up according to this.

Code: [Select]
// pin name:    not-mega:         mega(1280 and 2560)
// slave reset: 10:               53
// MOSI:        11:               51
// MISO:        12:               50
// SCK:         13:               52


I have the LED connected on pin 0, and the resistor and wire for detecting touch on pins 1 and 2, based the pinout here: http://hlt.media.mit.edu/?p=1695

The led always light up if I make it look for the countnumber 0, if I try to change the countnumber though using the "if" section connected to capsense it never turns on.

Go Up