My delay is not what I set it too be (it's random and not even close) [CODE]

When I upload the following code, the delay is not what I put it.

----------------------------------------- CODE -----------------------------------------------

[size=10pt]#include <CapacitiveSensor.h>

/* DIY Piano

   - DIY Piano with Arduino. This is a very simple project.
   - ONLY needed some very cheap materials,
     e.g. pencil, A4 paper, paper clip, speaker, male-to-male jumper, breadboard and Arduino Uno board.
   - Of course the Arduino Uno board is not cheap, but it is reusable for other projects too. So it is worth to own one.
   
   - This project using the concept of capasitive sensor, so no switch/button is required for it.
   - Here is the link of capacitive sensor. For better understanding, please visit the page.
     http://playground.arduino.cc//Main/CapacitiveSensor?from=Main.CapSense
     
   - To watch the Demo of This Project:
     http://www.youtube.com/watch?v=X4QNT5hOHLs&list=TLlm-tAV1gEzDF4VV39VdjCSLNlfe0tTpU


   Created by:
   Oh Hai Seng || Junny Oh    07 November 2013
*/



// Import the CapacitiveSensor Library.
#include <CapacitiveSensor.h>


// Name the pin as led. 
#define speaker 11


// Set the Send Pin & Receive Pin.
CapacitiveSensor   cs_2_3 = CapacitiveSensor(2,3);        // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_2_4 = CapacitiveSensor(2,4);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_2_5 = CapacitiveSensor(2,5);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_2_6 = CapacitiveSensor(2,6);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_2_7 = CapacitiveSensor(2,7);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_2_8 = CapacitiveSensor(2,8);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_2_9 = CapacitiveSensor(2,9);        // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foil


void setup()                    
{
  cs_2_3.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example
  
  // Arduino start communicate with computer.
  Serial.begin(9600);
}

void loop()                    
{
  // Set a timer.
  long start = millis();
  
  // Set the sensitivity of the sensors.
  long total1 =  cs_2_3.capacitiveSensor(3);
  long total2 =  cs_2_4.capacitiveSensor(3);
  long total3 =  cs_2_5.capacitiveSensor(3);
  long total4 =  cs_2_6.capacitiveSensor(3);
  long total5 =  cs_2_7.capacitiveSensor(3);
  long total6 =  cs_2_8.capacitiveSensor(3);
  long total7 =  cs_2_9.capacitiveSensor(3);
  


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

  Serial.print(total1);                  // print sensor output 1
  Serial.print("\t");                    // Leave some space before print the next output
  Serial.print(total2);                  // print sensor output 2
  Serial.print("\t");                    // Leave some space before print the next output
  Serial.print(total3);                  // print sensor output 3
  Serial.print("\t");                    // Leave some space before print the next output
  Serial.print(total4);                  // print sensor output 4
  Serial.print("\t");                    // Leave some space before print the next output
  Serial.print(total5);                  // print sensor output 5
  Serial.print("\t");                    // Leave some space before print the next output
  Serial.print(total6);                  // print sensor output 6
  Serial.print("\t");                    // Leave some space before print the next output
  Serial.println(total7);                // print sensor output 7
                                         // "println" - "ln" represent as "line", system will jump to next line after print the output.
  
  
  
  
  // When hand is touched the sensor, the speaker will produce a tone.
  // I set a threshold for it, so that the sensor won't be too sensitive.
  if (total1 > 300) tone(speaker,523);
  if (total2 > 300) tone(speaker,587);
  if (total3 > 300) tone(speaker,659);
  if (total4 > 300) tone(speaker,698);
  if (total5 > 310) tone(speaker,784);
  if (total6 > 320) tone(speaker,880);
  if (total7 > 310) tone(speaker,988);
  
  // When hand didn't touch on it, no tone is produced.
  if (total1<=300  &  total2<=10  &  total3<=10 & total4<=10  &  total5<=10  &  total6<=10 &  total7<=10)
    noTone(speaker);

  delay(100);                             // arbitrary delay to limit data to serial port 
}[/size]

----------------------------------------- CODE -------------------------------------
( no idea how you format here :\ )

As you can see at the last line of my code, I put a 100 ms delay. But the thing is, it only updates the serial monitor (and also the Arduino Board) every 5-10 sec (it’s not eve the same every time). Is there something wrong with the code, or does this have something to do with something else? I’ve tried other projects to test delay using print and println codes, and it worked as intended.
I’m a complete newbie of this sort of stuff, so please bear with me if I did something stupid.

Thanks in advance guys :slight_smile:

if (total1<=300  &  total2<=10  &  total3<=10 & total4<=10  &  total5<=10  &  total6<=10 &  total7<=10)

I’d be happier if those ‘&’ were twins.

Please use code tags.
It isn’t that hard.

Change ths Serial.begin(9600);

to 115200, will help a lot. Update in Serial monitor (lower right) to match.

What does this output?

 Serial.print(millis() - start);        // check on performance in milliseconds

The problem might be that your loop takes a lot of time to run.

This long start = millis(); should be unsigned long start = millis();

...R

Hello I have a similar problem... At serial monitor i see millis() with range 3860 to 4448 I use arduino UNO Please advice Sorry about my bad english and my little Knowledge

taratatzoum: Hello I have a similar problem... At serial monitor i see millis() with range 3860 to 4448 I use arduino UNO Please advice Sorry about my bad english and my little Knowledge

Adding on to a Thread that is pretty well dead and saying "I have a similar problem" only makes sense if you are using the exact same code as the OP.

Since you didn't post your code I can't be sure, but I rather suspect yours is different.

Either start your own Thread for your own problem (use a title that describes the problem) and post your code in it or (second best) post your code here.

...R