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
}
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.
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;
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
}
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.