Skip to content

Instantly share code, notes, and snippets.

@KaiaKitten
Last active August 29, 2015 14:24
Show Gist options
  • Save KaiaKitten/c1a69a80c16ec6c5005d to your computer and use it in GitHub Desktop.
Save KaiaKitten/c1a69a80c16ec6c5005d to your computer and use it in GitHub Desktop.
Arduino Uno Mouse
const int LEFT_BUTTON =2; //Input pin for the left button
const int MIDDLE_BUTTON =3; //Input pin for the middle button
const int RIGHT_BUTTON =4; //Input pin for the right button
const int X_AXIS =0; //Joystick x-axis analog pin
const int Y_AXIS =1; //Joystick y-axis analog pin
int val; //For holding mapped pot value
int readJoystick(int axis)
{
int val = analogRead(axis); //Read analog value
//val = map(val, 0, 1023, -10, 10); //Map the reading
if (val <= 2 && val >= -2) //Create dead zone to stop mouse drift
return 0;
else //Return scaled value
return val;
}
void setup()
{
pinMode(LEFT_BUTTON, INPUT);
pinMode(MIDDLE_BUTTON, INPUT);
pinMode(RIGHT_BUTTON, INPUT);
Serial.begin(9600); //Start Serial
}
void loop()
{
int xVal = analogRead(X_AXIS);
int yVal = analogRead(Y_AXIS);
Serial.print(xVal);
Serial.print(",");
Serial.print(yVal);
Serial.print(",");
Serial.print((boolean)digitalRead(LEFT_BUTTON));//Send value
Serial.print(",");
Serial.print((boolean)digitalRead(MIDDLE_BUTTON));
Serial.print(",");
Serial.println((boolean)digitalRead(RIGHT_BUTTON));
delay(50); //Delay so we don't flood the computer
}
/*
Exploring Arduino - Code Listing 6-7: Processing Code to Read Data and Change Color on the Screen
http://www.exploringarduino.com/content/ch6
Copyright 2013 Jeremy Blum ( http://www.jeremyblum.com )
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
*/
//Processing Sketch to Read Value and Change Color on the Screen
//Import and initialize serial port library
import processing.serial.*;
import java.lang.Math;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
Serial port;
String data = " ";
String[] values;
Robot rob;
void setup()
{
try{
rob = new Robot();
} catch (AWTException e){
e.printStackTrace();
}
port = new Serial(this, "/dev/ttyACM0", 9600); //Set up serial
port.bufferUntil('\n'); //Set up port to read until newline
}
void draw()
{
values = data.split(",");
if(int(values[2]) == 1)
{
rob.mousePress(InputEvent.BUTTON1_MASK);
}
}
void serialEvent (Serial port)
{
try{
data = port.readStringUntil('\n'); //Gets val
}catch(RuntimeException e) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment