Author Options:

Need help with arduino code.? Answered

Alright I asked this question on the arduino forums but nobody is giving me a straight answer, and it's getting annoying. I am using the capsense library on an attiny85, I want to count the number of times an even happens, in this case the capacitive censor exceeding a certain amount. Then based on that number I want it to turn on one led, or two, and so forth, to keep count. Can someone tell me how the f*****g get this to work? I try to use serial to debug and just get "Serial" not defined in this scope. Heres the code: #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);

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


I want to write a code for this prob. I have ir sensor to detect a line and ultrasonic sensor to detect the distance btw the obstacles and the robo .Please if anyone can get me the code it's urgent and the problem is it's getting wrong everytime I tried . In this prob . We have to detect the obstacle first and then take a left turn then using robo arm we have to pick the object and then take a right turn then we have to place it in the box. Dimensions are been written in the pic. Plz help me


Several things:
One, the library is now called "CapSense.h"
Two, all the arguments are therefore called "CapSense"

I've gotten it to work just fine turning an LED on when it's touched, the problem lies that the LED does not turn on when I have the script set up as above, countnumber's value does not seem to change. Is there an easier way or more straightforward way to do this because I can't get the darn thing to work for the life of me.

So where did you get your copy of the sensor library - the Arduino.cc site version is called "CapSense", and works on Arduino 1+

I just googled arduino capsense and got this page


They did have to change the name because cypress semiconductor has that trademarked.

Hi, in your code you mention cs_1_2.....would these be the legs 6 and 7 of the atTiny85 as per the pin diagram of the chip or would these mean Analog_input_1 and Analog_input_2 which would be the legs 7 and 4 of the chip...Can you please clarify..I'm having issues even in getting a basic LED to be enabled by touch....

The relevant declaration is her
CapacitiveSensor cs_1_2 = CapacitiveSensor(1,2);

The pin numbers are identified in the call. The second pin (pin 2 here) is the input pin from the capacitor

First of all countnumber ==1 isn't storing 1 to the variable. If you want to store a number to a variable like that you only use a single =. When you check to see if the variable equals a number you use ==. But if you intend to keep track of mutiple presses you will want to use countnumber ++ to add 1 each time it detects a touch. Then where you have the LED light be sure to set a delay for how long you want the LED to stay on then set your variable back to zero.

Ok I've gotten it to work using countnumber, however since I can only get it to work inside the loop it resets, I want the variable to keep incrementing.

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

Then don't initialize the counter in the loop. Use a while statement around the code. Is it just supposed to loop 0..99, 0..99 ?

Its just supposed to increase each time it detects a touch. I've tried initializing it outside the loop but then it doesn't work at all.

heres the post in the arduino forums.

The guys in there are more patient than I am. Nobody's giving you a straight answer, because it looks like you aren't giving them decent information. Your code is terrible. You need to practice, you need to learn, by yourself and by listening to experts.

Write down a description of what you want to do, so far you don't seem to know yourself:
Here's a very rough idea of what I mean.

Intialise cap sense.
while true
Read pin,
if pin cap < (some value) its touched, count it.
increment a counter.
if the counter is > 1, turn the led on,
while (pin touched) {wait and do nothing}

This is called pseudo code, its kind of a written description of what you are trying to do, kind of in your programming language.

Sort out making the LED go off on the NEXT touch.

Well I haven't touched arduino code before today but I do now what I want to do, the problem is that the counter is not incrementing and having a global variable does not work, when from what I can tell there is no reason that it shouldn't.

Have you actually read what I wrote ?

If you know what to do, write some pseudocode that explains it to yourself, post it here.

Have you implemented what I said to do, with a while loop around your code, after the counter is set to zero ?


What RANGE does total1 vary over between touch and not touch ?

have you read what I wrote?

I stated in an answer above, if I initialize the counter outside the loop, then the LED stays lit solid, and does not change if you touch the sensor or not, it stays on. It doesn't matter if I do it in void setup() or above that, it stays on.

I don't know what range total1 varies over, but it turned the LED on just fine if I had digitalwrite there instead of changing the countnumber, so using it to trigger something else would work the same. Besides, changing what the total needs to be greater than does nothing because with the variable declared outside the loop the led stays on.

WHICH loop ? You haven't shown me any code WITH a loop in it, apart from "loop() "

FIND OUT how much total1 varies. You've got serial debugging USE IT

STILL no pseudocode.