Adding "Tone" to a sketch.

Hi,
I'm completely new to all this programming stuff and I'm trying to sort out an issue with a sketch I sort of cobbled together from other sketch's. Its using the capsense library. It basically turns on 5 LED's according to the values being read on the sense pin. What I want to do is have a different tone sound on pin 7 (buzzer) for each step. So the higher the reading the higher the tone. I'm really new to this and I'm having some trouble sorting it so any help would be greatly appreciated. Cheers.

Heres the code I've cobbled together:

#include <CapacitiveSensor.h>




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

int const led1 = 9;  //sets LED's 1 2 3 4 5 as outputs on Digital Pins 9 10 11 12 and 13
int const led2 = 10;
int const led3 = 11;
int const led4 = 12;
int const led5 = 13;
int const buzzer = 7;

void setup() 
{
  Serial.begin(9600);
}
void loop() 
{
pinMode(led1,OUTPUT);  // Sets PinMode of LED's 1 thru 5 and buzzer as outputs
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(led5,OUTPUT);  
pinMode(buzzer,OUTPUT);
  
  short start = millis();
  short total1 = cs_4_2.capacitiveSensor(65);
  
  if (total1 >= 290) {
    digitalWrite(led1,HIGH);
  }else digitalWrite(led1, LOW);  
  
  if (total1 >=350) {
    digitalWrite(led2,HIGH);
  }else digitalWrite(led2, LOW);   
  
   if (total1 >= 440) {
    digitalWrite(led3,HIGH);
  }else digitalWrite(led3, LOW);  

   if (total1 >=530) {
    digitalWrite(led4,HIGH);
  }else digitalWrite(led4,LOW);
  
  if (total1 >= 620) {
    digitalWrite(led5,HIGH);
  }else digitalWrite(led5, LOW);

  Serial.print("\t");
  Serial.println(total1); // print sensor output 2
  delay(50); // arbitrary delay to limit data to serial port
     
}

Read the forum rules.
Post your code correctly, then we can see.

And where do I find the Forum Rules on posting code "correctly"??? :confused:

Top of every section.

A link for your convenience...
http://forum.arduino.cc/index.php/topic,148850.0.html

Thanks for the link Coding Badly. I edited the code and it saved it again so I'm hoping I got it right this time. Fingers crossed. This is my first time posting in a forum so I'm gonna make a few mistakes.
Cheers. :confused:

 if (total1 >= 290) {
    digitalWrite(led1,HIGH);
  }else digitalWrite(led1, LOW);

You have all these if statements already checking the value and lighting the pins. The trivial answer is to just add your call to tone to each of these to play the tone you want.

https://www.arduino.cc/en/Reference/Tone

Here is a sample of the original code that I changed. However when power is applied to the circuit the buzzer is on all the time, the tone does change according to the levels detected but the buzzer doesn't turn off. What actually needs to happen is that one tone be played until the next level is exceeded and then change to the next frequency and so on.

#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * 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
 */


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

int const led1 = 9;                      //sets LED's 1 2 3 4 5 as outputs on Digital Pins 9 10 11 12 and 13
int const led2 = 10;
int const led3 = 11;
int const led4 = 12;
int const led5 = 13;
int const buzzer = 8;

int sound = 200;

void setup()					
{
  Serial.begin(9600);
}
void loop()					
{
  pinMode(led1,OUTPUT);  // Sets PinMode of LED's 1 thru 5 and buzzer as outputs
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);  
  pinMode(buzzer,OUTPUT);

  short start = millis();
  short total1 = cs_4_2.capacitiveSensor(65);

  if (total1 >= 250) {
    digitalWrite(led1,HIGH);
    sound = 250;
  }
  else digitalWrite(led1, LOW);   

  if (total1 >=310) {
    digitalWrite(led2,HIGH);
    sound = 270;

Your code didn't all post. Some is missing at the end.

darthinsidiom:
Here is a sample of the original code that I changed. However when power is applied to the circuit the buzzer is on all the time, the tone does change according to the levels detected but the buzzer doesn't turn off. What actually needs to happen is that one tone be played until the next level is exceeded and then change to the next frequency and so on.

I don't quite understand when it is supposed to turn off. You want it to play whatever tone according to the level and that's what it is doing.

Don't post "samples". (Also called "snippets".)

The problem is in the code you didn't post.

Ok cool. Thanks for the tip MorganS. So here is the code with the changes I have attempted.

#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * 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
 */


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

int const led1 = 9;                      //sets LED's 1 2 3 4 5 as outputs on Digital Pins 9 10 11 12 and 13
int const led2 = 10;
int const led3 = 11;
int const led4 = 12;
int const led5 = 13;
int const buzzer = 8;

int sound = 200;

void setup()					
{
  Serial.begin(9600);
}
void loop()					
{
  pinMode(led1,OUTPUT);  // Sets PinMode of LED's 1 thru 5 and buzzer as outputs
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);  
  pinMode(buzzer,OUTPUT);

  short start = millis();
  short total1 = cs_4_2.capacitiveSensor(65);

  if (total1 >= 250) {
    digitalWrite(led1,HIGH);
    sound = 250;
  }
  else digitalWrite(led1, LOW);   

  if (total1 >=310) {
    digitalWrite(led2,HIGH);
    sound = 270;
  }
  else digitalWrite(led2, LOW); 

    if (total1 >= 400) {
    digitalWrite(led3,HIGH);
    sound = 290;
  }
  else digitalWrite(led3, LOW);  

    if (total1 >=490) {
    digitalWrite(led4,HIGH);
    sound = 310;
  }
  else digitalWrite(led4,LOW);

    if (total1 >= 580) {
    digitalWrite(led5,HIGH);
    sound = 330;
  }
  else digitalWrite(led5, LOW);
  

    Serial.print("\t");
  Serial.println(total1);			// print sensor output 2
  tone(buzzer, sound);
  delay(50); 					// arbitrary delay to limit data to serial port

}

I'm still not clear on what condition the tone should stop and go silent.

Try this. There's two changes. First, since your numeric values start at 250, I've inserted another if() statement that looks to see if the value is less than 250. This sets the sound to zero, which isn't a valid sound.

Then at the bottom, check if the sound is valid - start the tone otherwise stop the tone which was previously playing.

#include <CapacitiveSensor.h>

/*
 * CapitiveSense Library Demo Sketch
 * Paul Badger 2008
 * Uses a high value resistor e.g. 10M between send pin and receive pin
 * 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
 */


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

int const led1 = 9;                      //sets LED's 1 2 3 4 5 as outputs on Digital Pins 9 10 11 12 and 13
int const led2 = 10;
int const led3 = 11;
int const led4 = 12;
int const led5 = 13;
int const buzzer = 8;

#define MINIMUM_SOUND 200
int sound = MINIMUM_SOUND;

void setup()					
{
  Serial.begin(9600);
}
void loop()					
{
  pinMode(led1,OUTPUT);  // Sets PinMode of LED's 1 thru 5 and buzzer as outputs
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(led5,OUTPUT);  
  pinMode(buzzer,OUTPUT);

  short start = millis();
  short total1 = cs_4_2.capacitiveSensor(65);

  if (total1 < 250) {
    sound = 0;
  }

  if (total1 >= 250) {
    digitalWrite(led1,HIGH);
    sound = 250;
  }
  else digitalWrite(led1, LOW);   

  if (total1 >=310) {
    digitalWrite(led2,HIGH);
    sound = 270;
  }
  else digitalWrite(led2, LOW); 

    if (total1 >= 400) {
    digitalWrite(led3,HIGH);
    sound = 290;
  }
  else digitalWrite(led3, LOW);  

    if (total1 >=490) {
    digitalWrite(led4,HIGH);
    sound = 310;
  }
  else digitalWrite(led4,LOW);

    if (total1 >= 580) {
    digitalWrite(led5,HIGH);
    sound = 330;
  }
  else digitalWrite(led5, LOW);
  

    Serial.print("\t");
  Serial.println(total1);			// print sensor output 2
  if(sound >= MINIMUM_SOUND) {
    tone(buzzer, sound);
  } else {
    //no sound to play - turn it off
    noTone(buzzer);
  }
  delay(50); 					// arbitrary delay to limit data to serial port

}

[/quote]

Hey MorganS that certainly did the trick. Thanks so much for taking the time and the effort to help out with my code. I'm gonna print out your changes and examine them further to see how it worked in my existing code.Once again Kudos.