collapse

* Posts Recentes

O que é isto ? por SerraCabo
[12 de Abril de 2024, 14:20]


Amplificador - Rockboard HA 1 In-Ear por almamater
[11 de Abril de 2024, 20:46]


Emulador NES em ESP32 por dropes
[10 de Abril de 2024, 15:30]


Meu novo robô por josecarlos
[29 de Março de 2024, 18:30]


Bateria - Portátil por almamater
[25 de Março de 2024, 22:14]


Escolher Osciloscópio por jm_araujo
[06 de Fevereiro de 2024, 23:07]


TP4056 - Dúvida por dropes
[31 de Janeiro de 2024, 14:13]


Leitura de dados por Porta Serie por jm_araujo
[22 de Janeiro de 2024, 14:00]


Distancia Cabo por jm_araujo
[08 de Janeiro de 2024, 16:30]


Laser Engraver - Alguém tem? por almamater
[16 de Dezembro de 2023, 14:23]

Autor Tópico: Touch Screen Nintendo  (Lida 12711 vezes)

0 Membros e 1 Visitante estão a ver este tópico.

Offline delphi

  • Mini Robot
  • *
  • Mensagens: 217
Re: Touch Screen Nintendo
« Responder #15 em: 07 de Abril de 2010, 15:03 »
Olá,

Há uns tempos numas brincadeiras com o arduino e um touchscreen nintendo (penso que o princípio aplica-se a todos touchscreens) resultou o seguinte código.

A ideia era de fazer do touchscreen um "interruptor" para controlar as luzes (nesta experiência com LEDs RGBs) e assim podia mudar de cor como pretendesse (era para um bar).

Comprei na sparkfun o BOB-09170 Nintendo DS Touch Screen Connector Breakout, e na dealxtreme comprei estes touchscreens http://www.dealextreme.com/details.dx/sku.3259

A imagem que coloquei no touchscreen não tenho aqui, mas facilmente se altera o código para outra imagem qualquer.

O código não é o último que tenho e por isso não está acabado (por ex. em vez de utilizar delay, pode utilizar interrupts,..., mas penso que serve para mostrar como se pode controlar um touchscreen com o arduino. Por ex. o código está começado para ser possível definir e utilizar vários skins mas não está finalizado, falta tambem uma função para calibrar,...

Espero que ajude.




Código: [Seleccione]
#define XLOW 10
#define YLOW 10

//initial skin selected
int skinNum = 0;

//total skins
int maxSkins = 1;

boolean toprint = true;

// Output
int redPin   = 11;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 9;  // Blue LED,  connected to digital pin 11


// Program variables
int redVal   = 0; // Variables to store the values to send to the pins
int greenVal = 0;   // Initial values are Red full, Green and Blue off
int blueVal  = 0;

int activeColor = -1;
int maxX = 960;
int maxY = 870;

// variables for input pin and control LED
int Xplus  = 0;//x1
int Xminus = 3; //y2
int Yplus = 1; //
int Yminus = 2; //

int nowTouching = 0;
boolean sendNoTouch = true;

int xval = 0;
int yval = 0;

int stepX = 5;
int oldxval = 0;

void setup(){
  xval = measureX();
  yval = measureY();
  checkTouch();     
 
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
 
  Serial.begin(9600);
  Serial.println("Setup Ok");
 
}

int measureX()
{
 PORTC = (0<<Yplus)|(0<<Yminus)|(1<<Xplus)|(0<<Xminus);
 DDRC  = (0<<Yplus)|(0<<Yminus)|(1<<Xplus)|(1<<Xminus);

 delay(10);
 int aux = analogRead(Yminus);
 if (aux > maxX) { return 0; }
  else { return maxX - aux; }
}

int measureY()
{
 PORTC = (1<<Yplus)|(0<<Yminus)|(0<<Xplus)|(0<<Xminus);
 DDRC  = (1<<Yplus)|(1<<Yminus)|(0<<Xplus)|(0<<Xminus);

 delay(10);
 int aux = analogRead(Xplus);
 if (aux > maxY) { return 0; }
  else { return maxY - aux; }
}

int checkTouch()
{
 PORTC = B0010;
 DDRC = B0001;

 return digitalRead(Xminus);
}

void loop()
{
  //-leitura das coordenadas
  xval = measureX();
  yval = measureY();
  checkTouch();         

  nowTouching = !(xval < XLOW | yval < YLOW);
 
  //-se não está a tocar, verifica se antes tambem não estava
  if (!nowTouching)
  {
    if (sendNoTouch)
    {
      checkTouch();     
      Serial.print("0,0,0");
      Serial.println();
    };
    sendNoTouch = false;
    delay(10);
  } else
  {
    Serial.print("X=");
    Serial.print(xval, DEC);
    Serial.print("; Y=");
    Serial.print(yval, DEC);
    int button = getPlace(xval, yval);
    if (button != -1)
    {
      executeAction (button);
      Paint();
     
      Serial.print("    botao: ");
      Serial.print(button); 
    }
    Serial.println();
    sendNoTouch = true;
   
    oldxval = xval;
  }
}

//-onPanelClick
void executeAction(int button)
{
  if (skinNum == 0)
  {
    executeSkin0(button);
  }; 
}

void nextSkin()
{
  if (skinNum == (maxSkins -1 )) skinNum = -1;
  skinNum = skinNum + 1;
}

void executeSkin0(int button)
{
 
      if (button == 11 )//-all off
      {
        activeColor = -1;
        redVal = 0;
        greenVal = 0;
        blueVal = 0;
      }else if (button == 12 )//all on
      {
        activeColor = -1;
        redVal = 255;
        greenVal = 255;
        blueVal = 255;
      }else if (button == 5 )//red full
      {
        activeColor = 1;
        redVal = 255;
      }else if (button == 6 )//red off
      {
        activeColor = 1;
        redVal = 0;
      }else if (button == 7 )//green full
      {
        activeColor = 2;
        greenVal = 255;
      }else if (button == 8 )//green off
      {
        activeColor = 2;
        greenVal = 0;
      }else if (button == 9 )//blue full
      {
        activeColor = 3;
        blueVal = 255;
      }else if (button == 10 )//blue off
      {
        activeColor = 3;
        blueVal = 0;
      }else if (button == 3 )//up active color
      {
        fadeDown(true);
      }else if (button == 4 )//down active color
      {
        fadeDown(false);
      }
}

void fadeDown(boolean up)
{
  if (!up)
  {
   if (activeColor == 1) { redVal = goodColor(redVal - stepX);}
   else if (activeColor == 2) { greenVal = goodColor(greenVal - stepX);}
   else if (activeColor == 3) { blueVal = goodColor(blueVal - stepX);}
  } else
  {
   if (activeColor == 1) { redVal = goodColor(redVal + stepX);}
   else if (activeColor == 2) { greenVal = goodColor(greenVal + stepX);}
   else if (activeColor == 3) { blueVal = goodColor(blueVal + stepX);}
  } 
}

void nextColor()
{
  activeColor = activeColor + 1;
  if (activeColor == 4 ) {activeColor = 1;};
}

void Paint()
{
  analogWrite(redPin, redVal); 
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin, blueVal); 
 
  Serial.print("redVal: ");
  Serial.print(redVal);
  Serial.print("greenVal: ");
  Serial.print(greenVal);
  Serial.print("blueVal:");
  Serial.print(blueVal);
}

int goodColor(int value){
    if (value<0) {return  0;}
    else if (value>255) {value = 255;}
   
    return value;
}

int getPlace(int x, int y)
{
  if (skinNum == 0) {
    if (b1() ) {return 1;}
    if (b2() ) {return 2;}
    if (b3() ) {return 3;}
    /*
    if (b4() ) {return 4;}
    if (b5() ) {return 5;}
    if (b6() ) {return 6;}
    if (b7() ) {return 7;}
    if (b8() ) {return 8;}
    if (b9() ) {return 9;}
    if (b10() ) {return 10;}
    if (b11() ) {return 11;}
    if (b12() ) {return 12;}
    */
  }
  return -1;
}

/////////////////////////////////////////////////////////////////////////////
//skin 1 begin
boolean b1()
{
  return YouClickedMe(1, 140, 718, 30 ) ;
}

boolean b2()
{
  return YouClickedMe(1, 84, 600, 30 ) ;
}
boolean b3()
{
  return YouClickedMe(1, 25, 560, 30 ) ;
}

boolean b4()
{
  return YouClickedMe(2, 180, 750, 100 ) ;
}

boolean b5()
{
  return YouClickedMe(3, 726, 195, 100 ) ;
}

boolean b6()
{
  return YouClickedMe(4, 726, 465, 100 ) ;
}

boolean b7()
{
  return YouClickedMe(5, 505, 60, 100 ) ;
}
boolean b8()
{
  return YouClickedMe(6, 505, 635, 100 ) ;
}

boolean b9()
{
  return YouClickedMe(7, 325, 70, 100 ) ;
}
boolean b10()
{
  return YouClickedMe(8, 325, 635, 100 ) ;
}

boolean b11()
{
  return YouClickedMe(9, 150, 55, 100 ) ;
}
boolean b12()
{
  return YouClickedMe(10, 150, 635, 100 ) ;
}
//skin 1 end
/////////////////////////////////////////////////////////////////////////////


boolean YouClickedMe(int index, int myX, int myY, int myR )//or not
{
  //cálculo para saber se clicou no botao redondo x
  //-formula (x-xa)(x-xa) + (y-xa)(y-xa) < R*R
  int difX = (xval-myX);
  int difY = (yval-myY);

  double difXx2 = pow(difX, 2) ;
  double difYx2 = pow(difY, 2);

  long val = difXx2 + difYx2; 
  long raio2 = myR*myR;
  if (val > 0 )  {  return (val < raio2 ); }
 
  return false;
}
www.saborapalco.com - Companhia de Teatro Amador
www.megavaquinha.com - Uma mega aposta no euromilhões de um grupo de mega amigos

Offline SLBGlorioso

  • Mini Robot
  • *
  • Mensagens: 21
Re: Touch Screen Nintendo
« Responder #16 em: 13 de Abril de 2010, 16:09 »
Olá,

Há uns tempos numas brincadeiras com o arduino e um touchscreen nintendo (penso que o princípio aplica-se a todos touchscreens) resultou o seguinte código.

A ideia era de fazer do touchscreen um "interruptor" para controlar as luzes (nesta experiência com LEDs RGBs) e assim podia mudar de cor como pretendesse (era para um bar).

Comprei na sparkfun o BOB-09170 Nintendo DS Touch Screen Connector Breakout, e na dealxtreme comprei estes touchscreens http://www.dealextreme.com/details.dx/sku.3259

A imagem que coloquei no touchscreen não tenho aqui, mas facilmente se altera o código para outra imagem qualquer.

O código não é o último que tenho e por isso não está acabado (por ex. em vez de utilizar delay, pode utilizar interrupts,..., mas penso que serve para mostrar como se pode controlar um touchscreen com o arduino. Por ex. o código está começado para ser possível definir e utilizar vários skins mas não está finalizado, falta tambem uma função para calibrar,...

Espero que ajude.




Código: [Seleccione]
#define XLOW 10
#define YLOW 10

//initial skin selected
int skinNum = 0;

//total skins
int maxSkins = 1;

boolean toprint = true;

// Output
int redPin   = 11;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 9;  // Blue LED,  connected to digital pin 11


// Program variables
int redVal   = 0; // Variables to store the values to send to the pins
int greenVal = 0;   // Initial values are Red full, Green and Blue off
int blueVal  = 0;

int activeColor = -1;
int maxX = 960;
int maxY = 870;

// variables for input pin and control LED
int Xplus  = 0;//x1
int Xminus = 3; //y2
int Yplus = 1; //
int Yminus = 2; //

int nowTouching = 0;
boolean sendNoTouch = true;

int xval = 0;
int yval = 0;

int stepX = 5;
int oldxval = 0;

void setup(){
  xval = measureX();
  yval = measureY();
  checkTouch();     
 
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
 
  Serial.begin(9600);
  Serial.println("Setup Ok");
 
}

int measureX()
{
 PORTC = (0<<Yplus)|(0<<Yminus)|(1<<Xplus)|(0<<Xminus);
 DDRC  = (0<<Yplus)|(0<<Yminus)|(1<<Xplus)|(1<<Xminus);

 delay(10);
 int aux = analogRead(Yminus);
 if (aux > maxX) { return 0; }
  else { return maxX - aux; }
}

int measureY()
{
 PORTC = (1<<Yplus)|(0<<Yminus)|(0<<Xplus)|(0<<Xminus);
 DDRC  = (1<<Yplus)|(1<<Yminus)|(0<<Xplus)|(0<<Xminus);

 delay(10);
 int aux = analogRead(Xplus);
 if (aux > maxY) { return 0; }
  else { return maxY - aux; }
}

int checkTouch()
{
 PORTC = B0010;
 DDRC = B0001;

 return digitalRead(Xminus);
}

void loop()
{
  //-leitura das coordenadas
  xval = measureX();
  yval = measureY();
  checkTouch();         

  nowTouching = !(xval < XLOW | yval < YLOW);
 
  //-se não está a tocar, verifica se antes tambem não estava
  if (!nowTouching)
  {
    if (sendNoTouch)
    {
      checkTouch();     
      Serial.print("0,0,0");
      Serial.println();
    };
    sendNoTouch = false;
    delay(10);
  } else
  {
    Serial.print("X=");
    Serial.print(xval, DEC);
    Serial.print("; Y=");
    Serial.print(yval, DEC);
    int button = getPlace(xval, yval);
    if (button != -1)
    {
      executeAction (button);
      Paint();
     
      Serial.print("    botao: ");
      Serial.print(button); 
    }
    Serial.println();
    sendNoTouch = true;
   
    oldxval = xval;
  }
}

//-onPanelClick
void executeAction(int button)
{
  if (skinNum == 0)
  {
    executeSkin0(button);
  }; 
}

void nextSkin()
{
  if (skinNum == (maxSkins -1 )) skinNum = -1;
  skinNum = skinNum + 1;
}

void executeSkin0(int button)
{
 
      if (button == 11 )//-all off
      {
        activeColor = -1;
        redVal = 0;
        greenVal = 0;
        blueVal = 0;
      }else if (button == 12 )//all on
      {
        activeColor = -1;
        redVal = 255;
        greenVal = 255;
        blueVal = 255;
      }else if (button == 5 )//red full
      {
        activeColor = 1;
        redVal = 255;
      }else if (button == 6 )//red off
      {
        activeColor = 1;
        redVal = 0;
      }else if (button == 7 )//green full
      {
        activeColor = 2;
        greenVal = 255;
      }else if (button == 8 )//green off
      {
        activeColor = 2;
        greenVal = 0;
      }else if (button == 9 )//blue full
      {
        activeColor = 3;
        blueVal = 255;
      }else if (button == 10 )//blue off
      {
        activeColor = 3;
        blueVal = 0;
      }else if (button == 3 )//up active color
      {
        fadeDown(true);
      }else if (button == 4 )//down active color
      {
        fadeDown(false);
      }
}

void fadeDown(boolean up)
{
  if (!up)
  {
   if (activeColor == 1) { redVal = goodColor(redVal - stepX);}
   else if (activeColor == 2) { greenVal = goodColor(greenVal - stepX);}
   else if (activeColor == 3) { blueVal = goodColor(blueVal - stepX);}
  } else
  {
   if (activeColor == 1) { redVal = goodColor(redVal + stepX);}
   else if (activeColor == 2) { greenVal = goodColor(greenVal + stepX);}
   else if (activeColor == 3) { blueVal = goodColor(blueVal + stepX);}
  } 
}

void nextColor()
{
  activeColor = activeColor + 1;
  if (activeColor == 4 ) {activeColor = 1;};
}

void Paint()
{
  analogWrite(redPin, redVal); 
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin, blueVal); 
 
  Serial.print("redVal: ");
  Serial.print(redVal);
  Serial.print("greenVal: ");
  Serial.print(greenVal);
  Serial.print("blueVal:");
  Serial.print(blueVal);
}

int goodColor(int value){
    if (value<0) {return  0;}
    else if (value>255) {value = 255;}
   
    return value;
}

int getPlace(int x, int y)
{
  if (skinNum == 0) {
    if (b1() ) {return 1;}
    if (b2() ) {return 2;}
    if (b3() ) {return 3;}
    /*
    if (b4() ) {return 4;}
    if (b5() ) {return 5;}
    if (b6() ) {return 6;}
    if (b7() ) {return 7;}
    if (b8() ) {return 8;}
    if (b9() ) {return 9;}
    if (b10() ) {return 10;}
    if (b11() ) {return 11;}
    if (b12() ) {return 12;}
    */
  }
  return -1;
}

/////////////////////////////////////////////////////////////////////////////
//skin 1 begin
boolean b1()
{
  return YouClickedMe(1, 140, 718, 30 ) ;
}

boolean b2()
{
  return YouClickedMe(1, 84, 600, 30 ) ;
}
boolean b3()
{
  return YouClickedMe(1, 25, 560, 30 ) ;
}

boolean b4()
{
  return YouClickedMe(2, 180, 750, 100 ) ;
}

boolean b5()
{
  return YouClickedMe(3, 726, 195, 100 ) ;
}

boolean b6()
{
  return YouClickedMe(4, 726, 465, 100 ) ;
}

boolean b7()
{
  return YouClickedMe(5, 505, 60, 100 ) ;
}
boolean b8()
{
  return YouClickedMe(6, 505, 635, 100 ) ;
}

boolean b9()
{
  return YouClickedMe(7, 325, 70, 100 ) ;
}
boolean b10()
{
  return YouClickedMe(8, 325, 635, 100 ) ;
}

boolean b11()
{
  return YouClickedMe(9, 150, 55, 100 ) ;
}
boolean b12()
{
  return YouClickedMe(10, 150, 635, 100 ) ;
}
//skin 1 end
/////////////////////////////////////////////////////////////////////////////


boolean YouClickedMe(int index, int myX, int myY, int myR )//or not
{
  //cálculo para saber se clicou no botao redondo x
  //-formula (x-xa)(x-xa) + (y-xa)(y-xa) < R*R
  int difX = (xval-myX);
  int difY = (yval-myY);

  double difXx2 = pow(difX, 2) ;
  double difYx2 = pow(difY, 2);

  long val = difXx2 + difYx2; 
  long raio2 = myR*myR;
  if (val > 0 )  {  return (val < raio2 ); }
 
  return false;
}
Muito obrigado

Offline FET_Destroyer

  • Mini Robot
  • *
  • Mensagens: 213
    • Fet Destroyer
Re: Touch Screen Nintendo
« Responder #17 em: 14 de Junho de 2010, 19:38 »


This is how the library reads the X raw coordinate of a touch  :

X+ and X- are in high Z : RA0 and RA2 are inputs
Y+ is set to +5V : RA3 is output high
Y- is set to 0V : RA1 is output low
X+ voltage is read by ADC
Y+ is set to 0V : RA3 is output low
Y- is set to 5V: RA1 is output high
X- voltage is read by ADC and averaged with X+ value, the result is the raw X coordinate.
The same is then done to read the Y coordinate, please see TSlib.c file in .ZIP file above.

X and Y raw coordinates are then adjusted using calibration coordinates to get X and Y coordinates in pixel.

O essencial para ler xy do touchscreen