Go Down

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

XOIIO



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


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




Unfortunately I do not have one of those :/

XOIIO

Ok, some progress I've gotten the LED to light up based on the variable, however since the line to set the countnumber is in the loop it resets, I think I remember seeing a line of code to change it somewhere but I'm not sure.

Code: [Select]
void loop()                   
{
  int countnumber = 0;
  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);
}
}


XOIIO

I've tried putting countnumber up above the setup to make it a global variable but then the led just stays on once it's uploaded, hrmmm

PaulS

Quote
I've tried putting countnumber up above the setup to make it a global variable

That involved a code change, didn't it. You are then supposed to post the code again, if it doesn't work.
The art of getting good answers lies in asking good questions.

XOIIO


Quote
I've tried putting countnumber up above the setup to make it a global variable

That involved a code change, didn't it. You are then supposed to post the code again, if it doesn't work.


Well here you go, pretty much the same.

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);
}
}



I've found that if I change
Code: [Select]
if (countnumber == 1) to another number the LED lights up solidly even when the circuit has not been touched.
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;

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()                   
{
  int countnumber = 0;
  long start = millis();
  long total1 =  cs_1_2.capacitiveSensor(30);
  if (total1 < 99) {
   countnumber++;
   Serial.print("touched");
  }
  if (countnumber == 2) {
  digitalWrite(ledpin, HIGH);
  } else {
    digitalWrite(ledpin, LOW);
}
}


PaulS

You have a Serial.begin() statement in setup(), but no useful Serial.print() statements in loop(). Why not?

It's time to remedy that situation. Print out the value that you get from cs_1_2.capacitiveSensor(30); (that you store in the completely inappropriately named variable, total1).
Print out the value in counter. Does it increase when it is supposed to?
The art of getting good answers lies in asking good questions.

XOIIO


You have a Serial.begin() statement in setup(), but no useful Serial.print() statements in loop(). Why not?

It's time to remedy that situation. Print out the value that you get from cs_1_2.capacitiveSensor(30); (that you store in the completely inappropriately named variable, total1).
Print out the value in counter. Does it increase when it is supposed to?


I changed the code to this, still nothing from the serial monitor, the fact that I am using the mega to program and not a usb programmer must be the problem with this. Also the "total1" part is just a piece from the capsense example script.
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;

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()                    
{
 int countnumber = 0;
 long start = millis();
 long total1 =  cs_1_2.capacitiveSensor(30);
 Serial.print(total1);
 if (total1 < 99) {
  countnumber++;
  Serial.print("touched");
 }
 if (countnumber == 1) {
 digitalWrite(ledpin, HIGH);
 } else {
   digitalWrite(ledpin, LOW);
}
}


PaulS

Quote
the fact that I am using the mega to program

To program what?

Why not actually try the code on the Mega, until you know that it works? Then, upload it to something else, if that is what you are doing.
The art of getting good answers lies in asking good questions.

XOIIO


Quote
the fact that I am using the mega to program

To program what?

Why not actually try the code on the Mega, until you know that it works? Then, upload it to something else, if that is what you are doing.


an attiny85, I'll give it a try just on the mega I guess.

XOIIO

I modified the code for the mega, the led does not change if tough the sensor with the variable as a global one.

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


CapacitiveSensor   cs_2_3 = CapacitiveSensor(2,3);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int ledpin = 13;
int countnumber = 0;
void setup()                   
{
  cs_2_3.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_2_3.capacitiveSensor(30);
  Serial.print(total1);
  if (total1 < 99) {
   countnumber++;
   Serial.print("touched");
  }
  if (countnumber == 1) {
  digitalWrite(ledpin, HIGH);
  } else {
    digitalWrite(ledpin, LOW);
}
}



If I keep the code the same except for changing the pins now it's inverted, the led turns off when touched. on the attiny it was the opposite, which is what I needed.

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


CapacitiveSensor   cs_2_3 = CapacitiveSensor(2,3);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
int ledpin = 13;
void setup()                   
{
  cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  pinMode (ledpin, OUTPUT);
  Serial.begin(9600);
}


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




AWOL

Did you intend to put "countnumber" local to "loop", and therefore reset it every time "loop" gets called?

XOIIO


Did you intend to put "countnumber" local to "loop", and therefore reset it every time "loop" gets called?


No, I don't want it to reset, however putting it in the loop is the only way the program did what it was supposed to, if it is outside the loop it does not work, and I'm not sure why.

GoForSmoke




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


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




Unfortunately I do not have one of those :/


MEGA _is_ an Arduino board, bigger and better than UNO. It has 3 hardware serial channels so you don't need to run SoftSerial like you would with an UNO. UNO is cheaper, more common, but not the only Arduino. Yours is more.
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





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


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




Unfortunately I do not have one of those :/


MEGA _is_ an Arduino board, bigger and better than UNO. It has 3 hardware serial channels so you don't need to run SoftSerial like you would with an UNO. UNO is cheaper, more common, but not the only Arduino. Yours is more.



I meant the serial to usb conveter, I guess I could try with the mega, this has gotten annoying though :/ it kind of worked, now it's backwards, sigh

GoForSmoke

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


Between the Tiny and the Mega you can connect pin to pin, without converter. I think that it may require a resistor and pretty sure that a resistor of 1k or more (anyone?) for short connect would not hurt.

You would be using the MEGA to communicate with your PC, it would need a sketch of its own. Like I wrote, "in a pinch". That means if you have no serial to USB, you can use the Mega for that until you get one. Mega has 3 serial channels of which only 1 goes to the PC. The other 2 use Mega pins. They are open to jumper direct connect just as the RX and TX of the Tiny chip.



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.

Go Up