Skip to content

Instantly share code, notes, and snippets.

@mfaishalakbar
Created August 26, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfaishalakbar/ee302b796d282bd5711e63c4f2d3bb15 to your computer and use it in GitHub Desktop.
Save mfaishalakbar/ee302b796d282bd5711e63c4f2d3bb15 to your computer and use it in GitHub Desktop.
const char N = 4; // number of piezos
int seuil = 375; // threhold
int valeur1[N] = {0}; //first value of the N piezos
int valeur2[N] = {0}; //second value of the N piezos
unsigned long lt[N] = {0}; //time of last activation of piezos N
uint8_t kb[8] = { 0 }; //Array that Used for Key Sending for UnoKeyboard
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
for (char i = 0; i<N; i++) { //for piezo 1 to 4 (i = 0, 1, 2, 3)
valeur1[i] = analogRead(i); //read first value
}
delay(1); // wait
for (char i = 0; i<N; i++) { //for piezo 1 to 4
valeur2[i] = analogRead(i); //read second value
}
while ((valeur1[0] < valeur2[0] || valeur1[1] < valeur2[1] || valeur1[2] < valeur2[2] ||valeur1[3] < valeur2[3]) && (valeur2[0] > seuil || valeur2[1] > seuil || valeur2[2] > seuil || valeur2[3] > seuil) ) {
//well this while can be shorter i'm sure but ... i don't know if "valeur1<valeur2" is working so ... i have to try
// while one of the piezo have an increasing signal and that the peak registered (valeur2) is better than the trehold (seuil)
for (char i = 0; i<N; i++) { //continue to read value
valeur1[i] = valeur2[i];
valeur2[i] = analogRead(i);
}
// delay(0.5);
/*
* next is looking for a decreasing value AND with 40ms of delay
* when OK, it send a signal to the computer (signal can be d, f , j or k (it depends of the piezos and how they are linked to the arduino))
* then lt is refreshed
*/
if (valeur1>valeur2) {
if (valeur1[0] >= seuil && millis()>=lt[0]+40) {
kb[2] = 100; //What's this? Read Here : https://www.arduino.cc/en/Reference/ASCIIchart
Serial.write(kb, 8); //Print the Data as Keypress output at Arduino Uno Keyboard
releaseKey(); //Release Key
//Serial.print('d');
lt[0] = millis();
}
if (valeur1[1] >= seuil && millis()>=lt[1]+40) {
kb[3] = 107;
Serial.write(kb, 8);
releaseKey();
//Serial.print('k');
lt[1] = millis();
}
if (valeur1[2] >= seuil && millis()>=lt[2]+40) {
kb[4] = 106;
Serial.write(kb, 8);
releaseKey();
//Serial.print('j');
lt[2] = millis();
}
if (valeur1[3] >= seuil && millis()>=lt[3]+40) {
kb[5] = 102;
Serial.write(kb, 8);
releaseKey();
//Serial.print('f');
lt[3] = millis();
}
}
}
}
void releaseKey(){
kb[2] = 0;
kb[3] = 0;
kb[4] = 0;
kb[5] = 0;
Serial.write(kb, 8); //Release key after keypress
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment