Go Down

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

XOIIO

Ahh, figured it out, and changed the code, now it works fine!

Code: [Select]

#include <CapacitiveSensor.h>


CapacitiveSensor   cs_3_4 = CapacitiveSensor(3,4);
int led1 = 0;
int led2 = 1;
int led3 = 2;
int counter = 0;


void setup()                   
{
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
}


void loop()                   
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1;
    delay(5000);
  }
  if (counter ==0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (counter == 3) {
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
}
}

GoForSmoke





All pins start LOW by default and then you have

Code: [Select]
  pinMode (led1, OUTPUT);


in setup()




I'm not sure what you mean, without that the LED does nothing because it isn't set as an output.


Your code in the last example:
Code: [Select]
void setup()                   
{
  cs_1_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  pinMode (led1, OUTPUT);
  countnumber = -1;
}


Right at the start you set led1 to OUTPUT while by default it is LOW.
Current will flow from Vcc to the LOW pin, the led will turn on.

What happens after that..........



well, it seems no matter where I set the led as output it just stays on, if I don't include it the led doesn't turn on but then doesn't turn on when it should be triggered, so what do I do?

I've been going through a bunch of different sketches that use counters, and no matter what method I try of increasing the counter nothing changes, what you are talking about is probably interfering with it but I don't know how to fix that.

anyways, latest one I've been trying to get working.
Code: [Select]

#include <CapacitiveSensor.h>


CapacitiveSensor   cs_1_2 = CapacitiveSensor(1,2);
int led1 = 0;
int counter = 0;


void setup()                   
{
  cs_1_2.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (led1, OUTPUT);
}


void loop()                   
{
  long total1 =  cs_1_2.capacitiveSensor(30);
  if (total1 < 500) {
  counter = counter +1;
}
  if (counter == 1) {
    digitalWrite(led1, HIGH);
  }
  else {
    digitalWrite(led1, LOW);
  }
}



You could try connecting the led + to pin 5 and led - to GROUND.
Then when pin 5 is HIGH the led should turn on and when it is LOW it should turn off.

When led + is Vcc (5V) and led - is pin 5 then you have to set pin 5 HIGH to turn it off.
Since all pins start LOW, the setup() code above that does not set pin 5 HIGH will start with the led ON.
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.

GoForSmoke


Ahh, figured it out, and changed the code, now it works fine!

Code: [Select]

#include <CapacitiveSensor.h>


CapacitiveSensor   cs_3_4 = CapacitiveSensor(3,4);
int led1 = 0;
int led2 = 1;
int led3 = 2;
int counter = 0;


void setup()                   
{
  cs_3_4.set_CS_AutocaL_Millis(0xFFFFFFFF);
  pinMode (led1, OUTPUT);
  pinMode (led2, OUTPUT);
  pinMode (led3, OUTPUT);
}


void loop()                   
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1;
    delay(5000);
  }
  if (counter ==0) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, HIGH);
  }
  if (counter == 2) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
  }
  if (counter == 3) {
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
}
}



Want it to repeat that pattern?

Code: [Select]
void loop()                   
{
  long total1 =  cs_3_4.capacitiveSensor(30);
  if (total1 > 500) {
    counter = counter +1;
    if (counter > 3)  counter = 0;
    delay(5000);
  }
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