code complies, but doesnt work

Hi, sorry to message directly but ran some of your post and you look like you know your stuff.
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
    • 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.
    • Receive pin is the sensor pin - try different amounts of foil/metal on this pin
  1. */

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

Please put your code in its own window as seen in other posts. This can be done by placing

    [/color][code]  and [/code]
[color]

around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

You can edit your post and add the code tags as above.

Weedpharma

Can you post the actual Serial prints when there is NO totalx inputs?
Just "idle" loop.

And can you be specific WHICH output (Serial?) takes 3.4 seconds and how did you measure that.

Your code won't compile, as it's missing some curly braces.

Henry_Best:
Your code won't compile, as it's missing some curly braces.

I am from Missouri - "the show me state".
Let's see ONE example of missing braces, please.

    delay(600);

Bad - redesign required.

Mark

  if (status_power)
    digitalWrite(power, HIGH);
  else
    digitalWrite(power, LOW);
  if (status_power)

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

could be

digitalWrite(power, status_power);
digitalWrite(fans, status_power);

P.S. brackets seem to be OK (or were corrected in the meantime).

holmes4:

    delay(600);

Bad - redesign required.

Mark

Really?
Making arbitrary suggestion (for redesign) without knowing why it is there - no comments attached - is a VERY bad practice on this forum.
Next you will recommend to "use blink without delay".

Whandall:

  if (status_power)

digitalWrite(power, HIGH);
  else
    digitalWrite(power, LOW);
  if (status_power)

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



could be


digitalWrite(power, status_power);
digitalWrite(fans, status_power);




P.S. brackets seem to be OK (or were corrected in the meantime).

Even when the condition "status_power" is known to be boolean passing it to the function obfuscates the code just a little. It is OK in short code, but I would not use it in anything longer than few lines.

I am glad you see that the brackets are OK, I am still waiting to find out about missing braces. ( I was hoping you English folks from UK would catch it )

Vaclav:
Even when the condition "status_power" is known to be boolean passing it to the function obfuscates the code just a little.

I disagree.
status_power can be anything evaluating to a boolean, as it is already used in that context.

Using it that way hinders you to write

  if (status_power)
    digitalWrite(power, LOW);
  else
    digitalWrite(power, HIGH);

// or

 if (status_power)
    digitalWrite(power, HIGH);
  else
    digitalWrite(powar, LOW);

The original code was so clearly arranged, that it contained

  if (status_power)

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

I agree with Mark, delay() is just a source of Arduino problems. It is rarely used correct (because, yeay, there are not that many good applications for it...). It will block the possibility to look for inputs etc.

From the Arduino reference page for delay():

Caveat

While it is easy to create a blinking LED with the delay() function, and many sketches use short delays for such tasks as switch debouncing, the use of delay() in a sketch has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt. For alternative approaches to controlling timing see the millis() function and the sketch sited below. More knowledgeable programmers usually avoid the use of delay() for timing of events longer than 10's of milliseconds unless the Arduino sketch is very simple.

But who reads documentation? Sigh.

I'm not absolutely sure I could put the missing braces in the right place on this section of code:

    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)                    // <--- Duplicate statement, or something missing???
      
       if (status_power)
       digitalWrite(fans, HIGH);
       else
       digitalWrite(fans, LOW);

How long is the first if statement block? One statement or all of them? Also, it appears that either something is duplicated or another statement is missing.

econjack:
I'm not absolutely sure I could put the missing braces in the right place on this section of code:

    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)                    // <--- Duplicate statement, or something missing???
     
      if (status_power)
      digitalWrite(fans, HIGH);
      else
      digitalWrite(fans, LOW);




How long is the first *if* statement block? One statement or all of them? Also, it appears that either something is duplicated or another statement is missing.

OK, I'll reluctantly reply but this discussion is WAY off and NOT helping the OP at all. Just bunch of irrelevant (off subject to be clear ) nitpicking IMHO.

if (total3 > 200)
status_power = !status_power; if total3 > 200 change / complement the status_power
Serial.print("status_power = "); print ALWAYS , irregardless of total3

or in "reverse logic" - skip change of status_power if total3 < 200 and (always) print it.

Perfectly normal usage of "if" construct / command.
It would actually look pretty silly with braces , sorry , I mean brackets.
( And I am positive that someone would make derogatory off color remarks about it.)

Still like to see the "idle" loop serial output.

Vaclav:
OK, I'll reluctantly reply but this discussion is WAY off and NOT helping the OP at all. Just bunch of irrelevant (off subject to be clear ) nitpicking IMHO.

...assuming that the OP is reading anything, since there is no action on reply #1, or any reply at all, for that matter.

OK, I'll reluctantly reply but this discussion is WAY off and NOT helping the OP at all. Just bunch of irrelevant (off subject to be clear ) nitpicking IMHO.

Oh, come on, Vaclav, you've never had a humble opinion in your life. And how is something like pointing this out:

     if (status_power)
       digitalWrite(power, HIGH);
     else
       digitalWrite(power, LOW);
       if (status_power)                    // <--- Duplicate statement, or something missing???
      
       if (status_power)
       digitalWrite(fans, HIGH);
       else
       digitalWrite(fans, LOW);

not helping the OP see at least some of his problems? I think pointing out any error the OP wrote is useful. If he had known better, he wouldn't have written it.

aarg:
...assuming that the OP is reading anything, since there is no action on reply #1, or any reply at all, for that matter.

Interesting etiquette.

You are "assuming" the OP lost interest , therefore it is OK to freely criticize his code.

In more civilized forums this is called "hijacking".

econjack:
I'm not absolutely sure I could put the missing braces in the right place on this section of code:

    if (total3 > 200)

status_power = !status_power;
    Serial.print("status_power = ");
    Serial.println(status_power);
    if (status_power)                            //Here
      digitalWrite(power, HIGH);
    else
      digitalWrite(power, LOW);
      if (status_power)                 
                                                    //Here
      if (status_power)                  //and Here
      digitalWrite(fans, HIGH);
      else
      digitalWrite(fans, LOW);




How long is the first *if* statement block? One statement or all of them? Also, it appears that either something is duplicated or another statement is missing.

Duplicated, no! There are three instances of the if(status_power) statement where one would suffice.

@Vaclav: He told us it compiles, but what the OP posted won't compile.
Either he posted the wrong code, or he's being economical with the truth, neither of which helps him.

Vaclav:
Really?
Making arbitrary suggestion (for redesign) without knowing why it is there - no comments attached - is a VERY bad practice on this forum.
Next you will recommend to "use blink without delay".

<snigger!>

I have a problem with the make with the makefile in Marlin-PI3_Pro_C I get it most of the way but have no Ideal as to what to put here !

Detailed instructions for using the makefile:

1. Modify the line containg "ARDUINO_INSTALL_DIR" to point to the directory that

contains the Arduino installation (for example, under Mac OS X, this

might be /Applications/Arduino.app/Contents/Resources/Java).

2. Modify the line containing "UPLOAD_PORT" to refer to the filename

representing the USB or serial connection to your Arduino board

(e.g. UPLOAD_PORT = /dev/tty.USB0). If the exact name of this file

changes, you can use * as a wildcard (e.g. UPLOAD_PORT = /dev/tty.usb*).

3. Set the line containing "MCU" to match your board's processor.

Older one's are atmega8 based, newer ones like Arduino Mini, Bluetooth

or Diecimila have the atmega168. If you're using a LilyPad Arduino,

change F_CPU to 8000000. If you are using Gen7 electronics, you

probably need to use 20000000. Either way, you must regenerate

the speed lookup table with create_speed_lookuptable.py.

4. Type "make" and press enter to compile/verify your program.

5. Type "make upload", reset your Arduino board, and press enter to

upload your program to the Arduino board.

Note that all settings are set with ?=, this means you can override them

from the commandline with "make HARDWARE_MOTHERBOARD=71" for example

This defined the board you are compiling for (see Configuration.h for the options)

HARDWARE_MOTHERBOARD = 33

Arduino source install directory, and version number

ARDUINO_INSTALL_DIR = /arduino/ardunio-1.6.5-r5/java
ARDUINO_VERSION = 105

You can optionally set a path to the avr-gcc tools. Requires a trailing slash. (ex: /usr/local/avr-gcc/bin)

AVR_TOOLS_PATH = /usr/local/avr-gcc/bin

#Programmer configuration
UPLOAD_RATE = 250000
AVRDUDE_PROGRAMMER = arduino
UPLOAD_PORT = /dev/ttyUSB0

#Directory used to build files in, contains all the build files, from object files to the final hex file.
BUILD_DIR = applet

From here on down I get errors of nothing there to do . . What goes down there ?

This defines whether Liquid_TWI2 support will be built

LIQUID_TWI2 ?= 0

this defines if Wire is needed

WIRE ?= 0

################################################################