Friday, November 27, 2015

Mini Project turning into Final Project

For my mini project/final project i discovered it would be easier to use joysticks as long as i could figure out how to use it as a mouse control....which i did (see video below)



With this i feel that although using a joystick would be easier it also helps push my concept even more because of the direct human interaction.

*what i liked about the importing the arduino sketch into processing is that you dont have to work around or worry about editing your code to be blended with the processing code. You can leave them as two separate entities and just tie them together with the import declaration.

// Global varibles:
int lastButtonState = LOW;        // state of the button last time you checked
int lastButton2State = LOW;       // state of the other button last time you checked
boolean mouseIsActive = false;    // whether or not the Arduino is controlling the mouse

void setup() {
  // initialize mouse control:
  Mouse.begin();
  // initialize serial communication:
  Serial.begin(9600);
  // make pin 2 an input, using the built-in pullup resistor:
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  // read the first pushbutton:
  int buttonState = digitalRead(2);

  // if it's changed and it's high, toggle the mouse state:
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      // if mouseIsActive is true, make it false;
      // if it's false, make it true:
      mouseIsActive = !mouseIsActive;
      Serial.print("Mouse control state" );
      Serial.println(mouseIsActive);
    }
  }
  // save button state for next comparison:
  lastButtonState = buttonState;


  // read the analog sensors:
  int sensor1 = analogRead(A0);
  delay(1);
  int sensor2 = analogRead(A1);

  int xAxis = map(sensor1, 0, 1023, -5, 5);
  int yAxis = map(sensor2, 0, 1023, -5, 5);
  // print their values. Remove this when you have things working:
  Serial.print(xAxis);
  Serial.print("  ");
  Serial.println(yAxis);


  if (mouseIsActive == true) {
    Mouse.move(xAxis, yAxis, 0);

    // read the second pushbutton:
    int button2State = digitalRead(3);

    // if it's changed and it's high, toggle the mouse state:
    if (button2State != lastButton2State) {
      if (button2State == LOW) {
        Serial.println("mouse pressed");
        Mouse.press();
      }
      else {
        Serial.println("mouse released");
        Mouse.release();
      }
    }
    // save second button state for next comparison:
    lastButton2State = button2State;
  }
}

No comments:

Post a Comment