Skip to content

Instantly share code, notes, and snippets.

@catatonicTrepidation
Forked from KaiaKitten/MouseArduino
Created July 23, 2015 21:56
Show Gist options
  • Select an option

  • Save catatonicTrepidation/d359b89b6244e2f93e0a to your computer and use it in GitHub Desktop.

Select an option

Save catatonicTrepidation/d359b89b6244e2f93e0a to your computer and use it in GitHub Desktop.
Arduino Uno Mouse
/*
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.Robot;
Serial port;
String data = " ";
String[] values;
float brightness = 0; //For holding value from pot
float red = 0;
float green = 0;
float blue = 0;
void setup()
{
size(500,500); //Window size
port = new Serial(this, "COM4", 9600); //Set up serial
port.bufferUntil('\n'); //Set up port to read until newline
}
void draw()
{
if(data != null && data.length() > 0)
{
values = data.split(",");
}
background(Float.parseFloat(values[0])*Float.parseFloat(values[3]),Float.parseFloat(values[1])*Float.parseFloat(values[3]),Float.parseFloat(values[2])*Float.parseFloat(values[3]));
}
void serialEvent (Serial port)
{
try{
data = port.readStringUntil('\n'); //Gets val
}catch(RuntimeException e) {
}
}
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment