Step 3 - Detecting Swipes

PICTURE HERE

A little bit of thinking...

Take a moment to play with your current circuit. While looking at the Serial Plotter tool, observe how the waveform changes when you swipe left or right over the sensors, or don't put your hand near it.

Notice that if you are careful, and get your hand or finger in just the right place, you can control quite accurately where the line on the plot window goes? We can use this as a simple slider and control the position of something on the screen!

We can also work out the direction of a swipe across the sensor, by looking at how the value we measure is changing. If it is increasing, we can call this a left swipe, and if it is getting smaller, it's a right swipe.

  1. If the sensor value is not changing, let's assume there is no swipe. This is the easy bit!
  2. Next, we need to work out if the value is changing, and then which direction it is going in. We can do this by storing two sensor values. The most recent value, and the previous value.
    • If we subtract one from the other, and the value is zero, we know it isn't changing.
    • If the result is big, then we are swiping one way, and if the result is negative, we are swiping the other way
Cool eh? This is way of finding how fast a signal is changing is called taking the derivative. The picture below shows what happens when we take the derivative of a signal. Notice that the graph on the right has a bigger value when the graph on the left is steepest?

Left and Right

So now we know what to look for, lets try and detect a left and right swipe properly. Try to look at the code below and work out what it is doing first. This will help you understand how to change it later on.

Remember, to find out the direction of a swipe, we look at the value of the derivative of the sensor value, which is how fast it is changing. By looking at whether the rate of change of the sensor value is above or below a certain point, we can say a left or right swipe has happened.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#define SENSOR_PIN A0
#define LED_PIN    13

// This will store the most recent value of our swipe sensor.
int currentSensorValue;

// This stores the value we will compare with.
int previousSensorValue;

// This stores how fast the sensor value is changing.
int sensorSteepness;

// We use this to make sure we don't accidentally detect lots of swipes.
int swipe_damper = 1;

// Record which direction we are going in here.
int swipe_direction;

void setup() {
    // Start the serial connection with the PC.
    Serial.begin(115200);
}

void left_or_right(){
    // This function will work out whether we are swiping in a particular
    // direction or not.
    
    // These will determine how fast we have to move to make a swipe.
    const int threshold_1 = 10;
    const int threshold_2 = 20;

    if(sensorSteepness > threshold_2){
        // We swiped left!
        swipe_direction = 100;
        swipe_damper    = 100;
    }else if(sensorSteepness < -threshold_2){
        // We swiped right!
        swipe_direction = -100;
        swipe_damper    = 100;
    }else if(sensorSteepness < threshold_1 && sensorSteepness > - threshold_1){
        // No swipe
        swipe_direction = 0;
    }else{
        // Not sure if there is a swipe, so assume no.
        swipe_direction = 0;
    }
}

void loop() {
    
    // read the sensor value and store it.
    currentSensorValue = analogRead(SENSOR_PIN);

    // Update the new steepness value.
    sensorSteepness = (currentSensorValue - previousSensorValue) / swipe_damper;
    swipe_damper    = swipe_damper > 1 ? swipe_damper - 1 : 1;

    // Lets work out the direction of a swipe.
    left_or_right();

    // Move the most recent sensor measurement to the previous one.
    previousValues[valueIndexAdd] = currentSensorValue;

    // print the sensor steepness value for the plotter.
    Serial.print(currentSensorValue);
    Serial.print(" ");
    Serial.print(swipe_direction);
    Serial.print(" ");
    Serial.println(sensorSteepness);
    delay(1);
}

When you run this code, you should see a new line in the plotter, which goes either up or down based on whether you swiped left or right!

PICTURE HERE

What Next?

So, now you know how to build a gesture sensor using an Arduino! But, we we haven't been very imaginative with it have we? This I leave to you, what possible uses can you think up for it?

  • A motion sensitive burglar alarm?
  • Using it to control a game? Perhaps Ping-pong?
  • Can you make your own game, where you control the player position with the sensor?
Let us know what you build!


< Previous | Home