Close

Code to get basic functions

A project log for Hacking a Fairphone

...or how to build an extension module, that can enhance your abilities with your phone.

Kirschner ChristophKirschner Christoph 12/03/2017 at 17:160 Comments
/*
 Name:		Touch_Extension_FP2.ino
 Created:	03.12.2017 15:03:18
 Author:	Kirschner Christoph
*/

#include <HID.h>
#include <HID-Settings.h>
#include <HID-Project.h>
#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
*/
const int led1 = 6;
const int led2 = 8;
const int led3 = 9;

bool buttonPressed1 = false; // volume up
bool buttonPressed2 = false; // wake screen/ sleep screen
bool buttonPressed3 = false; // volume down

int firstPress1 = 0; // first press of button 1 aka volume up
int firstPress2 = 0; // first press of button 2 aka wake screen/ sleep screen
int firstPress3 = 0; // first press of button 3 aka volume down

bool screenCase = true;

CapacitiveSensor   cs_13_A0 = CapacitiveSensor(13, A0);        // 1.1M resistor between pins 13 & A0, pin A0 is sensor pin, add a wire and or foil if desired
CapacitiveSensor   cs_5_A1 = CapacitiveSensor(5, A1);        // 1.1M resistor between pins 5 & A1, pin A1 is sensor pin, add a wire and or foil
CapacitiveSensor   cs_10_A2 = CapacitiveSensor(10, A2);        // 1.1M resistor between pins 10 & A2, pin A2 is sensor pin, add a wire and or foil

void setup()
{
	Consumer.begin();
	System.begin();
	Keyboard.begin();
	//Serial.begin(9600);


	pinMode(13, OUTPUT);
	pinMode(10, OUTPUT);
	pinMode(5, OUTPUT);
	pinMode(A0, INPUT);
	pinMode(A1, INPUT);
	pinMode(A2, INPUT);

}

void loop()
{
	long start = millis();
	long total1 = cs_13_A0.capacitiveSensor(30); // touch output in numbers
	long total2 = cs_5_A1.capacitiveSensor(30);
	long total3 = cs_10_A2.capacitiveSensor(30);

	if (total1 > 100) { digitalWrite(led1, HIGH); } // lighting up the right leds
	else { digitalWrite(led3, LOW); }

	if (total2 > 100) { digitalWrite(led2, HIGH); }
	else { digitalWrite(led2, LOW); }

	if (total3 > 100) { digitalWrite(led3, HIGH); }
	else { digitalWrite(led1, LOW); }

	/*	Serial.print("Total1");
	Serial.println(total1);
	Serial.print("Total2");
	Serial.println(total2);
	Serial.print("Total3");
	Serial.println(total3); */

	int time = millis();
	if (total3 > 100 && buttonPressed1 == false) {
		firstPress1 = millis();
		buttonPressed1 = true;
	}
	if (total3 < 50 && buttonPressed1 == true) {
		if (time - firstPress1 > 50) {
			Consumer.write(MEDIA_VOL_UP); // HID code for volume up
			//Serial.println("vol up");
		}
		buttonPressed1 = false;
	}

	time = millis();
	if (total2 > 100 && buttonPressed2 == false) {
		firstPress2 = millis();
		buttonPressed2 = true;
	}
	if (total2 < 50 && buttonPressed2 == true) {
		if (time - firstPress2 > 50) {
			if (screenCase == true) {
				System.write(SYSTEM_SLEEP);
				screenCase = false;
			} else if (screenCase == false) {
				Keyboard.write(CONSUMER_POWER); //HID code for waking phone/set phone to sleep
				screenCase = true;	
			}
			//Serial.println("power on/off");
		}
		buttonPressed2 = false;
	}

	time = millis();
	if (total1 > 100 && buttonPressed3 == false) {
		firstPress3 = millis();
		buttonPressed3 = true;
	}
	if (total1 < 50 && buttonPressed3 == true) {
		if (time - firstPress3 > 50) {
			Consumer.write(MEDIA_VOL_DOWN); // HID code for volume down
			//Serial.println("vol down");
		}
		buttonPressed3 = false;
	}

}

Discussions