0

I am playing note tone with CapacitiveSensor and an output of tone like this

void loop() {   
    time_before = millis(); 

    long total1 =  cs_2_3.capacitiveSensor(30);
    long total2 =  cs_2_4.capacitiveSensor(30);


    if((total1>200)||(total2>200)) {

        if(total1>200) {

            tone(12,261); 
            time_after = millis();
            Serial.print("Time: ");
            Serial.println(time_after-time_before);
        }
        if(total2>200) {   
             tone(12,294);   
             time_after = millis();
             Serial.print("Time: ");
             Serial.println(time_after-time_before);
        }
    }
}

How can I read how many seconds I play this note par example

3
  • 2
    You are not calling noTone() anywhere, and you are calling tone() with two args, so the duration is indefinite. Perhaps you should add more conditional statements to detect when the "press" is released, and stop the tone there, as well as calculate the note length.
    – uint128_t
    Dec 29, 2016 at 17:45
  • @uint128_t - Your comment is the correct answer to this OP's question. You should post it as an answer. As it stands only the code execution time is measured, and not the tone duration, and it is the latter which is the OP's real goal. Jan 17, 2017 at 21:19
  • 1
    @Greenonline Your answer looks pretty good, so I will upvote that :)
    – uint128_t
    Jan 18, 2017 at 5:53

2 Answers 2

1

Your code currently will only measure the code execution time and not the duration of the note(s) being played.

You need to follow uint128_t's suggestion and rewrite the code to detect the "button release", at which point you measure the time with millis(), and call noTone(). Only then will you be able to measure the note's duration.

I believe that the source of confusion is that you seem to think that tone() acts like delay() and waits for the note to finish - it does not. tone() merely starts the note and then the next instruction is executed, without waiting for the note to end.

So, using millis() straight after calling tone() will actually only measure the time it takes to execute the tone() statement (and a few if statements - depending on whether you use your original code, or Maximus's code).

0

Try this:

void loop(){              
    long total1 =  cs_2_3.capacitiveSensor(30);
    long total2 =  cs_2_4.capacitiveSensor(30);    

    if((total1>200)||(total2>200)){

        if(total1>200){
            time_before = millis();
            tone(12,261); 
            time_after = millis();

            Serial.print("Time: ");
            Serial.println(time_after-time_before);
        }

        if(total2>200){ 
            time_before = millis();  
            tone(12,294);   
            time_after = millis();

            Serial.print("Time: ");
            Serial.println(time_after-time_before);
        }
    }
}

Use code indentation if you can. Also, that first OR (||) is unnecessary. I think you need to re-write this, or is this what you wanted?

10
  • I cant use delay(1000).....
    – Tony Yared
    Dec 27, 2016 at 23:22
  • That is just a sample code. The author put that there to slow down the data rate between arduino and PC. You could ignore that, the code would work fine. Anyway, for your function, you don't need to use delay.
    – user29094
    Dec 27, 2016 at 23:25
  • Time: 158 Time: 159 Time: 157 Time: 159 Time: 158 Time: 157 Time: 159 Time: 158 Time: 155
    – Tony Yared
    Dec 27, 2016 at 23:27
  • if i don't use no delay this is the output have it's not correct
    – Tony Yared
    Dec 27, 2016 at 23:27
  • i want the somme of all the time
    – Tony Yared
    Dec 27, 2016 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.