Author Options:

My code compiles, but doesn't work?? Answered

i'm doing a project based on the capacitive sensor method. the code will work fine as standard, but when its  with my added extras it doesnt work but it compiles. The other thing is that the its printing to the serial at 3.4 seconds, do you know why this might be.
here's the code.

#include <CapacitiveSensor.h>
#include <Servo.h>

Servo Mech;

int pos = 0;

  * CapitiveSense Library Demo Sketc  * Paul Badger 2008
5.  * Uses a high value resistor e.g. 10M between send pin and receive pin
6  * Resistor effects sensitivity, experiment with values, 50K - 50M. Larger resistor values yield larger sensor values.
7.  * Receive pin is the sensor pin - try different amounts of foil/metal on this pin
8.  */

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_4_6 = CapacitiveSensor(4,6);        // dnt need this
CapacitiveSensor   cs_4_8 = CapacitiveSensor(4,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
int light = 13;
int yellow = 12; //dnt need this
int power = 11;
int fans = 5;
boolean status_light = LOW, status_light_prev = LOW;
boolean status_yellow = LOW, status_yellow_prev = LOW; // dnt need this
boolean status_power = LOW, status_power_prev = LOW;   

void setup()                   
    cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
    pinMode(light, OUTPUT);
    pinMode(yellow, OUTPUT); // dnt need this
    pinMode(power, OUTPUT);
    pinMode(fans, OUTPUT);

void loop()                   
     long start = millis();
     long total1 =  cs_4_2.capacitiveSensor(30); // to power pin 11
     long total2 =  cs_4_6.capacitiveSensor(30); // dnt need this
     long total3 =  cs_4_8.capacitiveSensor(30); // to light

     Serial.print(millis() - start);        // check on performance in milliseconds
     Serial.print("\t");                    // tab character for debug windown spacing

     Serial.print(total1);                  // print sensor output 1
     Serial.print(total2);                  // print sensor output 2
     Serial.println(total3);                // print sensor output 3

     delay(100);                             // arbitrary delay to limit data to serial port

     if (total1 > 200)
       status_light = !status_light;
     Serial.print("\tstatus_light = ");

    if (status_light)

     if (status_light)
       digitalWrite(light, HIGH);
     digitalWrite(light, LOW);

     if (total2 > 200)                // dnt need this
       status_yellow = !status_yellow;
     Serial.print("status_yellow = ");
     if (status_yellow)
       digitalWrite(yellow, HIGH);
       digitalWrite(yellow, LOW);

    if (total3 > 200)
      status_power = !status_power;
     Serial.print("status_power = ");
     if (status_power)
       digitalWrite(power, HIGH);
       digitalWrite(power, LOW);
       if (status_power)

       if (status_power)
       digitalWrite(fans, HIGH);
       digitalWrite(fans, LOW);


thank you for your help thank you.


The forums are retiring in 2021 and are now closed for new topics and comments.

Best Answer 5 years ago

No body flow charts anymore.


Answer 5 years ago

Sadly. many coding problems would be resolved if even a basic flow chart were the starting point.A huge bug bear to me in a past job (software validation for the FDA) war either no existing documentation or poor and /or incorrect documentation.

Manufacturers were generally amazed when we asked for the documentation and the un-compiled code to check against it.

Fortunatelywe were able to charge them for reverse engineering the software to produce satisfactory documentation - very lucerative for a while.


5 years ago

It compiles. So what? That just means it is syntactically correct. "The red cucumber jumps over the annoyed football stadium" is a syntactically correct sentence, just totally senseless.

Welcome to the world of debugging. You will find out that writing code is the minor part. Debugging is where the time goes.

First remove all the parts you dnt (I suppose that should mean don't) need. If you don't need them get rid of them.

Second use meaningful variable names. Your total3 is neither a total of anything nor anything third after you removed the total2 stuff. How about sensorValue?

Third comment out all that stuff after reading the inputs and writing the values to serial. Then uncomment parts one by one and see where your code breaks.

Last, be more precise in your description. What should the program do, what does it/doesn't it do. What means "...that the its printing to the serial at 3.4 seconds" What is it printing? When? 3.4s after program start? In intervals every 3.4s?

And this part with

if (status_power)

if (status_power)

looks strange. If there wasn't something lost between those lines, the line with

digitalWrite(fans, LOW);

can never be reached


Answer 5 years ago

+1. Saved me a lot of typing....