Graphene/Mediaplayer controller

Da raspibo.
Versione del 4 dic 2016 alle 19:44 di Dancast78 (discussione | contributi) (Creata pagina con '= Media player controller = Altra applicazione sempre mediante l'uso di Teensy che può essere visto oltre che come terminale seriale anche come dispositivo HID e quindi simul...')
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
Jump to navigation Jump to search

Media player controller

Altra applicazione sempre mediante l'uso di Teensy che può essere visto oltre che come terminale seriale anche come dispositivo HID e quindi simulare mouse, tastiera e joystick.

In questo caso il test sfrutta la libreia capsense di Arduino che ha il vantaggio di poter essere calibrata settando alcuni valori.

#include <CapacitiveSensor.h>
 
/*
   Simple example of interaction between Teens 3.1 and graphene film used as capacitive touch sensor
   set uotput as serial+keyboard+mouse+joystick
   Based on CapacitiveSensor library: http://playground.arduino.cc/Main/CapacitiveSensor
   Set the correct number of samples to measure time
*/
 
 
CapacitiveSensor   Play_Pause = CapacitiveSensor(20, 15);
CapacitiveSensor   Inc_Volume = CapacitiveSensor(20, 16);
CapacitiveSensor   Dec_Volume = CapacitiveSensor(20, 17);
 
int val;
long calc;
long tasto_piu;
long tasto_meno;
const int ledPin_r = 21;
const int ledPin_g = 5;
const int ledPin_b = 6;
const long soglia_tasto_piu = 1800;
const long soglia_tasto_meno = 1800;
const int inc = 100;
 
 
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  long start = millis();
  long time1 =  Play_Pause.capacitiveSensor(250);     //Tune with correct values
  long time2 =  Inc_Volume.capacitiveSensor(100);     //Tune with correct values
  long time3 =  Dec_Volume.capacitiveSensor(250);     //Tune with correct values
 
  Serial.print((millis() - start) / 10);
  Serial.print("\t");
  Serial.print(time1);
  Serial.print("\t");
  Serial.print(time2);
  Serial.print("\t");
  Serial.print(time3);
  Serial.print("\t");
  if (time1 > 3000) {                                 //Change to detect best values
    Serial.print("p1");
    Keyboard.set_media(KEY_MEDIA_VOLUME_DEC);
    Keyboard.send_now();
    Keyboard.set_media(0);
    Keyboard.send_now();
  }
  Serial.print("\t");
  if (time2 > 11000) {                                 //Change to detect best values
    Serial.print("p2");
    Keyboard.set_media(KEY_MEDIA_PLAY_PAUSE);
    Keyboard.send_now();
    Keyboard.set_media(0);
    Keyboard.send_now();
  }
  Serial.print("\t");
  if (time3 > 5000) {                               //Change to detect best values
    Serial.print("p3");
    Keyboard.set_media(KEY_MEDIA_VOLUME_INC);
    Keyboard.send_now();
    Keyboard.set_media(0);
    Keyboard.send_now();
  }
  Serial.print("\t");
 
  tasto_piu = touchRead(22);
  tasto_meno = touchRead(23);

  if (tasto_piu > soglia_tasto_piu && val <= 3072) {
    val = val + inc;
    //Serial.print("tasto +");
  }
  Serial.print("\t");  
  if (tasto_meno > soglia_tasto_meno && val >= 0) {
    val = val - inc;
    //Serial.print("tasto -");
  }
  Serial.print("\t");
  if (val > 2048) {
    analogWrite(ledPin_g, val - 2048);
  } else {
    analogWrite(ledPin_g, 0);
  }
  if (val > 1024) {
    analogWrite(ledPin_b, val - 1024);
  } else {
    analogWrite(ledPin_b, 0);
  }
  analogWrite(ledPin_r, val);
  Serial.print(val);
  Serial.print("\t");
  Serial.print(tasto_piu);
  Serial.print("\t");
  Serial.print(tasto_meno);
  Serial.println("");
  delay(100);
}

Con questo esempio possiamo controllare un media player come Vlc pilotando i tasti play e pause con un sensore ed alzando ed abbassando il volume con altri due sensori.

Per questa applicazione può essere usata anche la pellicola più trasparente variando i valori in capacitiveSensor() e settando oppurtunamente i tempi.

Codice per entrambi gli esperimenti

Questo semplicemente il codice che comprende entrambi gli esperimenti precedenti con qualche aggiustamento:

#include <CapacitiveSensor.h>

/*
   Simple example of interaction between Teens 3.1 and graphene film used as capacitive touch sensor
   set uotput as serial+keyboard+mouse+joystick
   Based on CapacitiveSensor library: http://playground.arduino.cc/Main/CapacitiveSensor
   Set the correct number of samples to measure time
*/


CapacitiveSensor   Play_Pause = CapacitiveSensor(20, 15);
CapacitiveSensor   Inc_Volume = CapacitiveSensor(20, 16);
CapacitiveSensor   Dec_Volume = CapacitiveSensor(20, 17);

int val;
long calc;
long tasto_piu;
long tasto_meno;
const int ledPin_r = 21;
const int ledPin_g = 5;
const int ledPin_b = 6;
const long soglia_tasto_piu = 2400;
const long soglia_tasto_meno = 2400;
const int inc = 100;


void setup()
{
  Serial.begin(9600);
}

void loop()
{
  long start = millis();
  long time1 =  Play_Pause.capacitiveSensor(250);     //Tune with correct values
  long time2 =  Inc_Volume.capacitiveSensor(100);     //Tune with correct values
  long time3 =  Dec_Volume.capacitiveSensor(250);     //Tune with correct values

  Serial.print((millis() - start) / 10);
  Serial.print("\t");
  Serial.print(time1);
  Serial.print("\t");
  Serial.print(time2);
  Serial.print("\t");
  Serial.print(time3);
  Serial.print("\t");
  if (time1 > 3000) {                                 //Change to detect best values
    Serial.print("p1");
    Keyboard.set_media(KEY_MEDIA_VOLUME_DEC);
    Keyboard.send_now();
    Keyboard.set_media(0);
    Keyboard.send_now();
  }
  Serial.print("\t");
  if (time2 > 11000) {                                 //Change to detect best values
    Serial.print("p2");
    Keyboard.set_media(KEY_MEDIA_PLAY_PAUSE);
    Keyboard.send_now();
    Keyboard.set_media(0);
    Keyboard.send_now();
  }
  Serial.print("\t");
  if (time3 > 3000) {                               //Change to detect best values
    Serial.print("p3");
    Keyboard.set_media(KEY_MEDIA_VOLUME_INC);
    Keyboard.send_now();
    Keyboard.set_media(0);
    Keyboard.send_now();
  }
  Serial.print("\t");

  tasto_piu = touchRead(22);
  //Serial.print(val);
  //Serial.print("\t");
  //Serial.print(tasto_piu);
  //Serial.print("\t");
  tasto_meno = touchRead(23);
  //Serial.println(tasto_meno);
  if (tasto_piu > soglia_tasto_piu && val <= 3072) {
    val = val + inc;
  }
  if (tasto_meno > soglia_tasto_meno && val >= 0) {
    val = val - inc;
  }
  if (val > 2048) {
    analogWrite(ledPin_g, val - 2048);
  } else {
    analogWrite(ledPin_g, 0);
  }
  if (val > 1024) {
    analogWrite(ledPin_b, val - 1024);
  } else {
    analogWrite(ledPin_b, 0);
  }
  analogWrite(ledPin_r, val);
  Serial.println("");
  delay(100);
}