76Views4Replies

Author Options:

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

Hi,
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
    Serial.begin(9600);
    Mech.attach(9);
    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("\t\t\t");
     Serial.print(total2);                  // print sensor output 2
     Serial.print("\t\t\t");
     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 = ");
     Serial.print(status_light);
     Serial.print("\t");
    
    if (status_light)
       Mech.write(20);
    else
    Mech.write(160);
    delay(600);
   
     if (status_light)
       digitalWrite(light, HIGH);
     else
     digitalWrite(light, LOW);
    
    
     if (total2 > 200)                // dnt need this
       status_yellow = !status_yellow;
     Serial.print("status_yellow = ");
     Serial.print(status_yellow);     
     Serial.print("\t");
     if (status_yellow)
       digitalWrite(yellow, HIGH);
       else
       digitalWrite(yellow, LOW);

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

thank you for your help thank you.

Discussions

0
None
iceng

Best Answer 3 years ago

No body flow charts anymore.

0
None
rickharrisiceng

Answer 3 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.

0
None
verence

3 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