Hola!

Registrándote como bakuno podrás publicar, compartir y comunicarte en privado con otros bakuos :D

Regístrame ya!

Ayuda con código de arduino

speckles

Becerro
Desde
3 Ago 2011
Mensajes
11
Buen día foro,
les comento que estoy realizando un proyecto involucrando la placa de arduino Leonardo
pero tengo el inconveniente de que no se mucho sobre programación, y me gustaría que me ayudaran
a comprender el código que arduino me ofrece como parte de sus ejemplos

se basa en el control de mouse por medio de un Joystick, las conexiones y pruebas que e realizado
me han resultado favorables, pero quiero entender mejor el código y poder realizar cambios.

Código:
// set pin numbers for switch, joystick axes, and LED:
const int switchPin = 2;      // switch to turn on and off mouse control
const int mouseButton = 3;    // input pin for the mouse pushButton
const int xAxis = A0;         // joystick X axis  
const int yAxis = A1;         // joystick Y axis
const int ledPin = 5;         // Mouse control LED 

// parameters for reading the joystick:
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range/4;      // resting threshold
int center = range/2;         // resting position value

boolean mouseIsActive = false;    // whether or not to control the mouse
int lastSwitchState = LOW;        // previous switch state

void setup() {
  pinMode(switchPin, INPUT);       // the switch pin
  pinMode(ledPin, OUTPUT);         // the LED pin  
 // take control of the mouse:
  Mouse.begin();
}

void loop() {
  // read the switch:
  int switchState = digitalRead(switchPin);
  // if it's changed and it's high, toggle the mouse state:
  if (switchState != lastSwitchState) {
    if (switchState == HIGH) {
      mouseIsActive = !mouseIsActive;
      // turn on LED to indicate mouse state:
      digitalWrite(ledPin, mouseIsActive);
    } 
  }
  // save switch state for next comparison:
  lastSwitchState = switchState;

  // read and scale the two axes:
  int xReading = readAxis(A0);
  int yReading = readAxis(A1);

  // if the mouse control state is active, move the mouse:
  if (mouseIsActive) {
    Mouse.move(xReading, yReading, 0);
  }  

  // read the mouse button and click or not click:
  // if the mouse button is pressed:
  if (digitalRead(mouseButton) == HIGH) {
    // if the mouse is not pressed, press it:
    if (!Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.press(MOUSE_LEFT); 
    }
  } 
  // else the mouse button is not pressed:
  else {
    // if the mouse is pressed, release it:
    if (Mouse.isPressed(MOUSE_LEFT)) {
      Mouse.release(MOUSE_LEFT); 
    }
  }

  delay(responseDelay);
}

/*
  reads an axis (0 or 1 for x or y) and scales the 
 analog input range to a range from 0 to <range>
 */

int readAxis(int thisAxis) { 
  // read the analog input:
  int reading = analogRead(thisAxis);

  // map the reading from the analog input range to the output range:
  reading = map(reading, 0, 1023, 0, range);

  // if the output reading is outside from the
  // rest position threshold,  use it:
  int distance = reading - center;

  if (abs(distance) < threshold) {
    distance = 0;
  } 

  // return the distance for this axis:
  return distance;
}

he leído mucho sobre como funciona arduino, sé sobre los pines y hacer códigos simples,
pero de este código ciertas cosas no las entiendo.

tengo conflicto en esta sección, no se como interpretar la funcion de esas variables
Código:
// parameters for reading the joystick:
int range = 12;               // output range of X or Y movement
int responseDelay = 5;        // response delay of the mouse, in ms
int threshold = range/4;      // resting threshold
int center = range/2;         // resting position value


Gracias, por su ayuda y quizá alguien tenga alguna versión mas simple.

Saludos.
 
De una "pasada" rápida al programa te puedo decir que responseDelay está al final del loop principal.. le da un "descanso" de 5 ms para iniciar el siguiente ciclo.
Sobre range, checa la linea que dice reading = map(reading, 0, 1023, 0, range);.. map "convierte" un valor de una escala a otra. Por ejemplo reading estará entre 0 y 1024 y lo convertirá a un valor proporcional entre 0 y range.
Te recomiendo imprimir en el puerto serial cualquier valor que te interese usando Serial.Print... es lo bonito del Arduino, experimentar.
Saludos y mucha suerte con tu proyecto.
 
Volver
Arriba