## Tutorial.ControleLEDcircleWithJoystick History

June 04, 2010, at 11:37 PM by Equipo Traduccion -
Changed line 71 from:
```  // function that calculates the slope of the line that passes through the points
```
to:
```  // función que calcula la pendiente de la linea que pasa a través de los puntos
```
Changed line 78 from:
```  // function that calculates in which of the 8 possible zones is the coordinate x y, given the center cx, cy
```
to:
```  // Función que calcula en cual de las 8 posibles zonas corresponde la cordenada x,y, considerando el centro cx,cy
```
Changed line 81 from:
```    int alpha = calculateSlope(x,y, cx,cy); // slope of the segment betweent the point and the center
```
to:
```    int alpha = calculateSlope(x,y, cx,cy); // pendiente del segmento entre el punto y el centro
```
Changed line 85 from:
```      if (y > cy) // first cuadrant
```
to:
```      if (y > cy) // primer cuadrante
```
Changed line 87 from:
```        if (alpha > 1) // The slope is > 1, thus higher part of the first quadrant
```
to:
```        if (alpha > 1) // La pendiente es > 1, por tanto parte mas alta del primer cuadrante
```
Changed line 90 from:
```          return 1;    // Otherwise the point is in the lower part of the first quadrant
```
to:
```          return 1;    // En otro caso el punto está en la parte baja del primer cuadrante
```
Changed line 92 from:
```      else // second cuadrant
```
to:
```      else // segundo cuadrante
```
Changed line 103 from:
```      if (y < cy) // third cuadrant
```
to:
```      if (y < cy) // tercer cuadrante
```
Changed line 110 from:
```      else // fourth cuadrant
```
to:
```      else // cuarto cuadrante
```
Changed line 121 from:
```    digitalWrite(ledVerde, HIGH); // flag to know we entered the loop, you can erase this if you want
```
to:
```    digitalWrite(ledVerde, HIGH); // bandera para conocer si entramos en un bucle, puedes eliminar esto si quieres
```
Changed line 123 from:
```    // reads the value of the variable resistors
```
to:
```    // lectura de los valores de las resistencias variables
```
Changed lines 135-136 from:
```   // we print int the terminal, the cartesian value of the coordinate, and the zone where it belongs.
//This is not necesary for a standalone version
```
to:
```   // visualiza en el terminal, el valor cartesiano de la coordenada y la zona a la que pertenece
//Esto no es necesario para la versión standalone (independiente)
```
Changed line 138 from:
```    serialWrite(32); // print space
```
to:
```    serialWrite(32); // visualiza un espacio
```
Changed line 140 from:
```    serialWrite(32); // print space
```
to:
```    serialWrite(32); // visualiza un espacio
```
Changed line 146 from:
```    serialWrite(32); // print space
```
to:
```    serialWrite(32); // visualiza un espacio
```
Changed line 151 from:
```  // But this is necesary so, don't delete it!
```
to:
```  // Pero esto si es necesario, no lo borres!
```
June 04, 2010, at 11:22 PM by Equipo Traduccion -
Changed lines 49-53 from:
```  int joyPin1 = 0;                 // slider variable connecetd to analog pin 0
int joyPin2 = 1;                 // slider variable connecetd to analog pin 1
int coordX = 0;                  // variable to read the value from the analog pin 0
int coordY = 0;                  // variable to read the value from the analog pin 1
int centerX = 500;               // we measured the value for the center of the joystick
```
to:
```  int joyPin1 = 0;                 // potenciometro conectado al pin 0 analógico
int joyPin2 = 1;                 // potenciómetro conectado al pin 1 analógico
int coordX = 0;                  // variable para leer el valor del pin 0 analógico
int coordY = 0;                  // variable para leer el valor del pin 1 analógico
int centerX = 500;               // medimos el valos para la posición central del joystick
```
Changed line 58 from:
```  // Asignment of the pins
```
to:
```  // Asignación de los pines
```
June 04, 2010, at 11:16 PM by Equipo Traduccion -
Changed line 34 from:
```  * posibles zonas possible zones belogns the coordinate (x,y)
```
to:
```  * posibles zonas possible zones corresponde la coordenada(x,y)
```
Changed lines 36-37 from:
```  * Finally it ligths up the LED which is placed in the
* detected zone
```
to:
```  * Finalmente ilumina el LED que está ubicado en la
```
Changed lines 44-46 from:
```  // Declaration of Variables
```

```  int ledPins [] = { 2,3,4,5,6,7,8,9 };    // Array of 8 leds mounted in a circle
```
to:
```  // Declaracion de Variables
```

```  int ledPins [] = { 2,3,4,5,6,7,8,9 };    // Array de 8 leds montados en un circulo
```
Changed line 48 from:
```  int espera = 40;                 // Time you should wait for turning on the leds
```
to:
```  int espera = 40;                 // Tiempo que espera para poner en encendido los leds
```
June 04, 2010, at 11:09 PM by Equipo Traduccion -
Changed lines 17-20 from:

Como sabes por el tutorial Conectando un Joysrick, el joystick proporciona coordenadas (x,y) de retorno al Arduino. Como puedes ver al contemplar el joystick, el espacio en el que este se mueve es un círculo. Este círculo será desde ahora nuestra 'tarta' (ver debajo a la derecha de la primera imagen).

The only thing we need now to understand is that we have divided our Pie in 8 pieces. To each piece will correspond an LED. (See figure below). This way, when the joystick gives us a coordinate, it will necesarilly belong to one of the pies. Then, the program always lights up the LED corresponding to the pie in which the joystick is.

to:

Como sabes por el tutorial Conectando un Joysrick, el joystick proporciona coordenadas (x,y) de retorno al Arduino. Como puedes ver al contemplar el joystick, el espacio en el que este se mueve es un círculo. Este círculo será desde ahora nuestra 'Tarta' (ver debajo a la derecha de la primera imagen).

L único que tenemos que entender ahora consiste en que tenemos dividida nuestra Tarta en 8 pedazos. A cada pedazo corresponderá un LED. (Mirar el gráfico a continuación). Por esto, cuando el joystick nos da una coordenada, necesariamente tiene que pertenecer a una de las porciones. Por tanto, el programa siempre encenderá el LED correspondiente a la porción en la cual se encuentre el joystick.

Changed line 23 from:

to:

### Código

Changed line 26 from:
```  * This program controles a cirle of 8 LEDs through a joystick
```
to:
```  * Este programa controla un circulo de 8 LEDS a través de un joystick
```
Changed lines 28-29 from:
```  * First it reads two analog pins that are connected
* to a joystick made of two potentiometers
```
to:
```  * Primero lee dos pines analógicos que están conectados
* a un jojoystick hecho con dos potenciómetros
```
Changed line 31 from:
```  * This input is interpreted as a coordinate (x,y)
```
to:
```  * Esta entrada es interpretada como una coordenada (x,y)
```
Changed lines 33-34 from:
```  * The program then calculates to which of the 8
* possible zones belogns the coordinate (x,y)
```
to:
```  * El programa calcula a cual de las 8
* posibles zonas possible zones belogns the coordinate (x,y)
```
June 04, 2010, at 10:56 PM by Equipo Traduccion -
Changed lines 1-3 from:

to:

## Controlando un círculo de LEDs con un Joystick

### El circuito completo:

Changed line 7 from:

to:

### Detalle del cableado del LED

Changed line 11 from:

to:

### Detalle del cableado del Arduino

Changed lines 15-18 from:

### How this works

As you know from the Interfacing a Joystick tutorial, the joystick gives a coordinate (x,y) back to arduino. As you can see looking to the joystick is that the space in which he moves is a circle. This circle will be from now on our 'Pie' (see bottom right of the first image).

to:

### Como funciona

Como sabes por el tutorial Conectando un Joysrick, el joystick proporciona coordenadas (x,y) de retorno al Arduino. Como puedes ver al contemplar el joystick, el espacio en el que este se mueve es un círculo. Este círculo será desde ahora nuestra 'tarta' (ver debajo a la derecha de la primera imagen).

February 03, 2006, at 05:31 PM by 193.49.124.107 -

## Controlling a circle of LEDs with a Joystick

### The whole circuit:

http://static.flickr.com/35/94946013_ba47fe116e.jpg

### Detail of the LED wiring

http://static.flickr.com/39/94946015_aaab0281e8.jpg

### Detail of the arduino wiring

http://static.flickr.com/42/94946020_2a1dd30b97.jpg

### How this works

As you know from the Interfacing a Joystick tutorial, the joystick gives a coordinate (x,y) back to arduino. As you can see looking to the joystick is that the space in which he moves is a circle. This circle will be from now on our 'Pie' (see bottom right of the first image).

The only thing we need now to understand is that we have divided our Pie in 8 pieces. To each piece will correspond an LED. (See figure below). This way, when the joystick gives us a coordinate, it will necesarilly belong to one of the pies. Then, the program always lights up the LED corresponding to the pie in which the joystick is.

http://static.flickr.com/19/94946024_f7fd4b55ec.jpg

### Code

``` /* Controle_LEDcirle_with_joystik
* ------------
* This program controles a cirle of 8 LEDs through a joystick
*
* First it reads two analog pins that are connected
* to a joystick made of two potentiometers
*
* This input is interpreted as a coordinate (x,y)
*
* The program then calculates to which of the 8
* possible zones belogns the coordinate (x,y)
*
* Finally it ligths up the LED which is placed in the
* detected zone
*
* @authors: Cristina Hoffmann and Gustavo Jose Valera
* @hardware: Cristina Hofmann and Gustavo Jose Valera
* @context: Arduino Workshop at medialamadrid
*/
```

```  // Declaration of Variables
```

```  int ledPins [] = { 2,3,4,5,6,7,8,9 };    // Array of 8 leds mounted in a circle
int ledVerde = 13;
int espera = 40;                 // Time you should wait for turning on the leds
int joyPin1 = 0;                 // slider variable connecetd to analog pin 0
int joyPin2 = 1;                 // slider variable connecetd to analog pin 1
int coordX = 0;                  // variable to read the value from the analog pin 0
int coordY = 0;                  // variable to read the value from the analog pin 1
int centerX = 500;               // we measured the value for the center of the joystick
int centerY = 500;
int actualZone = 0;
int previousZone = 0;
```

```  // Asignment of the pins
void setup()
{
int i;
beginSerial(9600);
pinMode (ledVerde, OUTPUT);
for (i=0; i< 8; i++)
{
pinMode(ledPins[i], OUTPUT);
}
}
```

```  // function that calculates the slope of the line that passes through the points
// x1, y1 and x2, y2
int calculateSlope(int x1, int y1, int x2, int y2)
{
return ((y1-y2) / (x1-x2));
}
```

```  // function that calculates in which of the 8 possible zones is the coordinate x y, given the center cx, cy
int calculateZone (int x, int y, int cx, int cy)
{
int alpha = calculateSlope(x,y, cx,cy); // slope of the segment betweent the point and the center
```

```    if (x > cx)
{
if (y > cy) // first cuadrant
{
if (alpha > 1) // The slope is > 1, thus higher part of the first quadrant
return 0;
else
return 1;    // Otherwise the point is in the lower part of the first quadrant
}
{
if (alpha > -1)
return 2;
else
return 3;
}
}
```

```    else
{
if (y < cy) // third cuadrant
{
if (alpha > 1)
return 4;
else
return 5;
}
{
if (alpha > -1)
return 6;
else
return 7;
}
}
}
```

```   void loop() {
digitalWrite(ledVerde, HIGH); // flag to know we entered the loop, you can erase this if you want
```

```    // reads the value of the variable resistors
```

```    // We calculate in which x
actualZone = calculateZone(coordX, coordY, centerX, centerY);
```

```    digitalWrite (ledPins[actualZone], HIGH);
```

```    if (actualZone != previousZone)
digitalWrite (ledPins[previousZone], LOW);
```

```   // we print int the terminal, the cartesian value of the coordinate, and the zone where it belongs.
//This is not necesary for a standalone version
serialWrite('C');
serialWrite(32); // print space
printInteger(coordX);
serialWrite(32); // print space
printInteger(coordY);
serialWrite(10);
serialWrite(13);
```

```    serialWrite('Z');
serialWrite(32); // print space
printInteger(actualZone);
serialWrite(10);
serialWrite(13);
```

```  // But this is necesary so, don't delete it!
previousZone = actualZone;
// delay (500);
```

``` }
```

@idea: Cristina Hoffmann and Gustavo Jose Valera

@code: Cristina Hoffmann and Gustavo Jose Valera

@pictures and graphics: Cristina Hoffmann

@date: 20051008 - Madrid - Spain