Hide minor edits - Show changes to markup
// function that calculates the slope of the line that passes through the points
// función que calcula la pendiente de la linea que pasa a través de los puntos
// function that calculates in which of the 8 possible zones is the coordinate x y, given the center cx, cy
// Función que calcula en cual de las 8 posibles zonas corresponde la cordenada x,y, considerando el centro cx,cy
int alpha = calculateSlope(x,y, cx,cy); // slope of the segment betweent the point and the center
int alpha = calculateSlope(x,y, cx,cy); // pendiente del segmento entre el punto y el centro
if (y > cy) // first cuadrant
if (y > cy) // primer cuadrante
if (alpha > 1) // The slope is > 1, thus higher part of the first quadrant
if (alpha > 1) // La pendiente es > 1, por tanto parte mas alta del primer cuadrante
return 1; // Otherwise the point is in the lower part of the first quadrant
return 1; // En otro caso el punto está en la parte baja del primer cuadrante
else // second cuadrant
else // segundo cuadrante
if (y < cy) // third cuadrant
if (y < cy) // tercer cuadrante
else // fourth cuadrant
else // cuarto cuadrante
digitalWrite(ledVerde, HIGH); // flag to know we entered the loop, you can erase this if you want
digitalWrite(ledVerde, HIGH); // bandera para conocer si entramos en un bucle, puedes eliminar esto si quieres
// reads the value of the variable resistors
// lectura de los valores de las resistencias variables
// 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
// 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)
serialWrite(32); // print space
serialWrite(32); // visualiza un espacio
serialWrite(32); // print space
serialWrite(32); // visualiza un espacio
serialWrite(32); // print space
serialWrite(32); // visualiza un espacio
// But this is necesary so, don't delete it!
// Pero esto si es necesario, no lo borres!
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 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
// Asignment of the pins
// Asignación de los pines
* posibles zonas possible zones belogns the coordinate (x,y)
* posibles zonas possible zones corresponde la coordenada(x,y)
* Finally it ligths up the LED which is placed in the * detected zone
* Finalmente ilumina el LED que está ubicado en la * zona detectada
// Declaration of Variables
int ledPins [] = { 2,3,4,5,6,7,8,9 }; // Array of 8 leds mounted in a circle
// Declaracion de Variables
int ledPins [] = { 2,3,4,5,6,7,8,9 }; // Array de 8 leds montados en un circulo
int espera = 40; // Time you should wait for turning on the leds
int espera = 40; // Tiempo que espera para poner en encendido los leds
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.
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.
* This program controles a cirle of 8 LEDs through a joystick
* Este programa controla un circulo de 8 LEDS a través de un joystick
* First it reads two analog pins that are connected * to a joystick made of two potentiometers
* Primero lee dos pines analógicos que están conectados * a un jojoystick hecho con dos potenciómetros
* This input is interpreted as a coordinate (x,y)
* Esta entrada es interpretada como una coordenada (x,y)
* The program then calculates to which of the 8 * possible zones belogns the coordinate (x,y)
* El programa calcula a cual de las 8 * posibles zonas possible zones belogns the coordinate (x,y)
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).
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).
http://static.flickr.com/35/94946013_ba47fe116e.jpg
http://static.flickr.com/39/94946015_aaab0281e8.jpg
http://static.flickr.com/42/94946020_2a1dd30b97.jpg
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
/* 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
}
else // second cuadrant
{
if (alpha > -1)
return 2;
else
return 3;
}
}
else
{
if (y < cy) // third cuadrant
{
if (alpha > 1)
return 4;
else
return 5;
}
else // fourth cuadrant
{
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
coordX = analogRead(joyPin1);
coordY = analogRead(joyPin2);
// 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