Ingo Lohs
Published © GPL3+

Touch Me If You Can

Get in touch with a touch sensor!

BeginnerShowcase (no instructions)1 hour1,352
Touch Me If You Can

Things used in this project

Hardware components

Photon
Particle Photon
×1
Touch Sensor on a breakout-board
free of costs - only shipment is to pay at the moment
×1
Buzzer
Buzzer
×1
Jumper wires (generic)
Jumper wires (generic)
5 pieces Male <> Male
×1
Breadboard (generic)
Breadboard (generic)
×1

Story

Read more

Schematics

Touch-Sensor

v1.0

Code

Touch-Sensor

C/C++
v1.0
// Touch-Sensor Particle Photon - works with v0.6.2 without Libaries
// Ingo Lohs, v1.0 v. 16.10.2017
// after the touch of the sensor, a predefined melody is played with a piezo buzzer

// Definitions
const int TOUCH_PIN_IN = D4;   // Pin connected to SIG at Touch-Sensor
const int LED_PIN = D7;        // LED pin
const int BUZZER_PIN_OUT = A3; // Buzzer

unsigned long lastmillis = 0; // time for interation the loop

byte names[]  = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[]   = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int statePin = LOW;

/* Play Melody
 * -----------
 *
 * Program to play melodies stored in an array, it requires to know
 * about timing issues and about how to play tones.
 *
 * The calculation of the tones is made following the mathematical
 * operation:
 *
 *       timeHigh = 1/(2 * toneFrequency) = period / 2
 *
 * where the different tones are described as in the table:
 *
 * note         frequency       period  PW (timeHigh)   
 * c            261 Hz          3830    1915    
 * d            294 Hz          3400    1700    
 * e            329 Hz          3038    1519    
 * f            349 Hz          2864    1432    
 * g            392 Hz          2550    1275    
 * a            440 Hz          2272    1136    
 * b            493 Hz          2028    1014    
 * C            523 Hz          1912    956
 *
 * (cleft) 2005 D. Cuartielles for K3
 * Source: https://www.arduino.cc/en/Tutorial/PlayMelody
 */
 

void setup() 
{
  Serial.begin(9600);
  pinMode(TOUCH_PIN_IN, INPUT);
  pinMode(LED_PIN, OUTPUT);
}

void loop() 
{
    if ((millis() - lastmillis) > 500) {
        lastmillis = millis();
        readData();
    }
}


void readData() {

    int proximity = digitalRead(TOUCH_PIN_IN); // Read the state of the sensor
    
        if (proximity == HIGH) 
        {
        Serial.println(">> Sensor pressed"); // Particle CLI: use "particle serial monitor"
              
              analogWrite(BUZZER_PIN_OUT, 0);     
              for (count = 0; count < MAX_COUNT; count++) {
                statePin = !statePin;
                digitalWrite(LED_PIN, statePin);
                for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
                  for (count2=0;count2<8;count2++) {
                    if (names[count2] == melody[count*2 + 1]) {       
                      analogWrite(BUZZER_PIN_OUT,500);
                      delayMicroseconds(tones[count2]);
                      analogWrite(BUZZER_PIN_OUT, 0);
                      delayMicroseconds(tones[count2]);
                    } 
                    if (melody[count*2 + 1] == 'p') {
                      // make a pause of a certain size
                      analogWrite(BUZZER_PIN_OUT, 0);
                      delayMicroseconds(500);
                    }
                  }
                }
              }
        }
        else
        {
        digitalWrite(LED_PIN, LOW); // Turn the LED off
        digitalWrite(BUZZER_PIN_OUT, LOW); // Turn the Buzzer off
        }    

}

Touch-Sensor

v1.0

Credits

Ingo Lohs

Ingo Lohs

182 projects • 194 followers
I am well over 50 years and come from the middle of Germany.

Comments