Hi everybody,
i'm trying to create some kind of game which consists on making music by simply following a pattern given by the program.
So i have 9 touch sensitive sensors and 9 leds. Each sensor makes the sound of 1 note of the musical scale.
I want to create a sequence that goes like:
LIGHT LED 3
wait until SENSOR 3 detects touch---->BEEP 3th note of the scale
if its been 5 seconds---->blink LED 3
if it's been 10 seconds----> reset song pattern
LIGHT LED 5
wait until sensor 5 detects touch----->BEEP 5th note of the scale
if its been 5 seconds---->blink LED 3
if it's been 10 seconds----> reset song pattern
etc
So the pattern of the light and sounds is given by me (and there's just 1) i.e.
3th,5th,4th,7th,1st.
for now the code i have is:
#include <CapacitiveSensor.h>
#include "Pitches.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
*/
int ledPin13 = 13;
int ledPin12 = 12;
int ledPin11= 11;
CapacitiveSensor cs_4_2 = CapacitiveSensor(7,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(7,3); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil
//CapacitiveSensor cs_4_8 = CapacitiveSensor(7,4); // R 10M entre pin 4 i 6. pin 6 en mode sensat
int melody[] = {
NOTE_E4, NOTE_F4, NOTE_G4, NOTE_E4, NOTE_C4, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_C4
};
// int sensors[]= {
// sensor1, sensor2, sensor1, sensor1, sensor2, sensor1, sensor2, sensor1, sensor1, sensor2, sensor1, sensor1, sensor2
//};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 4, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4
};
void setup()
{
pinMode(ledPin13, OUTPUT); // configura el pin 13 como salida
pinMode(ledPin12, OUTPUT); // configura el pin 12 como salida
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); // turn off autocalibrate on channel 1 - just as an example
Serial.begin(9600); // abrimos puerto serie configurando la velocidad en 9600 bps
}
void loop()
{
digitalWrite(ledPin13, HIGH);
// long start = millis();
long sensor1 = cs_4_2.capacitiveSensor(30);
long sensor2 = cs_4_6.capacitiveSensor(30);
// long sensor3 = cs_4_8.capacitiveSensor(30);
// Serial.print(millis() - start); // check on performance in milliseconds
//Serial.print("\t"); // tab character for debug windown spacing
// Serial.print("SENSOR3:");
// Serial.print(sensor3); // print sensor output 2
// Serial.println(" ");
delay(100);
int sensors[]= {
sensor1, sensor2, sensor1, sensor1, sensor2, sensor1, sensor2, sensor1, sensor1, sensor2, sensor1, sensor1, sensor2
};
for (int thisNote=0; thisNote<13; thisNote++){
//for (int thisSensor=0;thisSensor<13;thisSensor++){
while (sensors[0]<100){
long sensor1 = cs_4_2.capacitiveSensor(30);
long sensor2 = cs_4_6.capacitiveSensor(30);
Serial.print("SENSOR1:");
Serial.print(sensor1); // presenta sensor output 1
Serial.print(" ");
Serial.print("SENSOR2:");
Serial.print(sensor2); // print sensor output 2
Serial.println(" ");
delay(100);
}
if (sensors[0]>=100){
// to calculate the note duration, take one second divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
digitalWrite(ledPin13, HIGH);
tone(8, melody[thisNote], 100);