Mini-Putt, created by Christie Dierk, Lucy Corippo, and Sita Kumar

Dependencies:   MMA8451Q USBDevice mbed

Fork of mini_putt by Suburban Putt

Committer:
lkc
Date:
Fri Sep 26 19:33:16 2014 +0000
Revision:
3:3061cfea7442
Parent:
2:377d98176230
Mini-Putt Game created by Christie Dierk, Lucy Corippo, and Sita Kumar;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bjo3rn 0:c9bb3c9d5ce8 1 #include "mbed.h"
bjo3rn 0:c9bb3c9d5ce8 2 #include "MMA8451Q.h"
cdierk 1:c5189364947e 3 #include "USBMouse.h"
cdierk 1:c5189364947e 4
cdierk 1:c5189364947e 5 USBMouse mouse;
bjo3rn 0:c9bb3c9d5ce8 6
bjo3rn 0:c9bb3c9d5ce8 7 // define I2C Pins and address for KL25Z. Taken from default sample code.
bjo3rn 0:c9bb3c9d5ce8 8 PinName const SDA = PTE25;
bjo3rn 0:c9bb3c9d5ce8 9 PinName const SCL = PTE24;
bjo3rn 0:c9bb3c9d5ce8 10 #define MMA8451_I2C_ADDRESS (0x1d<<1)
bjo3rn 0:c9bb3c9d5ce8 11
bjo3rn 0:c9bb3c9d5ce8 12 //serial connection to PC via USB
bjo3rn 0:c9bb3c9d5ce8 13 Serial pc(USBTX, USBRX);
bjo3rn 0:c9bb3c9d5ce8 14
cdierk 1:c5189364947e 15 DigitalIn button1(D8);
cdierk 1:c5189364947e 16 DigitalIn button2(D7);
cdierk 1:c5189364947e 17
bjo3rn 0:c9bb3c9d5ce8 18 int main(void)
bjo3rn 0:c9bb3c9d5ce8 19 {
cdierk 1:c5189364947e 20 AnalogIn xPin(A3);
cdierk 1:c5189364947e 21 AnalogIn yPin(A4);
cdierk 1:c5189364947e 22 AnalogIn zPin(A5);
cdierk 1:c5189364947e 23 button1.mode(PullUp);
cdierk 1:c5189364947e 24 button2.mode(PullUp);
cdierk 1:c5189364947e 25
bjo3rn 0:c9bb3c9d5ce8 26 //configure on-board I2C accelerometer on KL25Z
bjo3rn 0:c9bb3c9d5ce8 27 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
bjo3rn 0:c9bb3c9d5ce8 28
cdierk 1:c5189364947e 29 Serial pc(USBTX, USBRX);
bjo3rn 0:c9bb3c9d5ce8 30
bjo3rn 0:c9bb3c9d5ce8 31 while (true) {
cdierk 1:c5189364947e 32
cdierk 1:c5189364947e 33 float xValue = xPin.read();
cdierk 1:c5189364947e 34 float yValue = yPin.read();
cdierk 1:c5189364947e 35 float zValue = zPin.read();
lkc 2:377d98176230 36 pc.printf("%1.2f, %1.2f, %1.2f \r\n", xValue - 0.5, yValue - 0.48, zValue);
lkc 2:377d98176230 37 mouse.move(-150 * (xValue - 0.5), 150 * (yValue - 0.48));
cdierk 1:c5189364947e 38
cdierk 1:c5189364947e 39 if (!button1) {
cdierk 1:c5189364947e 40 mouse.click(MOUSE_LEFT);
lkc 3:3061cfea7442 41 wait(1);
cdierk 1:c5189364947e 42 }
cdierk 1:c5189364947e 43 if (!button2) {
cdierk 1:c5189364947e 44
cdierk 1:c5189364947e 45 float yValueMax = yPin.read();
cdierk 1:c5189364947e 46 yValue = yPin.read();
cdierk 1:c5189364947e 47 while(true){
cdierk 1:c5189364947e 48 if (yValue > yValueMax){
cdierk 1:c5189364947e 49 yValueMax = yValue;
cdierk 1:c5189364947e 50 yValue = yPin.read();
cdierk 1:c5189364947e 51 } else if ((yValue + .30) <= yValueMax){
cdierk 1:c5189364947e 52 mouse.click(MOUSE_LEFT);
cdierk 1:c5189364947e 53 break;
cdierk 1:c5189364947e 54 } else {
cdierk 1:c5189364947e 55 yValue = yPin.read();
cdierk 1:c5189364947e 56 }
cdierk 1:c5189364947e 57 }
cdierk 1:c5189364947e 58 /*while(true){
cdierk 1:c5189364947e 59 if (yValue <= .50){
cdierk 1:c5189364947e 60 yValue = yPin.read();
cdierk 1:c5189364947e 61 } else {
cdierk 1:c5189364947e 62 while(true){
cdierk 1:c5189364947e 63 yValue = yPin.read();
cdierk 1:c5189364947e 64 if (yValue < .40){
cdierk 1:c5189364947e 65 mouse.click(MOUSE_LEFT);
cdierk 1:c5189364947e 66 break;
cdierk 1:c5189364947e 67 }
cdierk 1:c5189364947e 68 }
cdierk 1:c5189364947e 69 }
cdierk 1:c5189364947e 70 break;
cdierk 1:c5189364947e 71 }*/
cdierk 1:c5189364947e 72 }
bjo3rn 0:c9bb3c9d5ce8 73 }
bjo3rn 0:c9bb3c9d5ce8 74 }
bjo3rn 0:c9bb3c9d5ce8 75
bjo3rn 0:c9bb3c9d5ce8 76 /*
bjo3rn 0:c9bb3c9d5ce8 77 // Graphing sketch for Processing
bjo3rn 0:c9bb3c9d5ce8 78
bjo3rn 0:c9bb3c9d5ce8 79
bjo3rn 0:c9bb3c9d5ce8 80 // This program takes ASCII-encoded strings containing floating point numbers
bjo3rn 0:c9bb3c9d5ce8 81 // from the serial port at 9600 baud and graphs them. It expects values in the
bjo3rn 0:c9bb3c9d5ce8 82 // range -1.0 to 1.0, followed by a newline, or newline and carriage return
bjo3rn 0:c9bb3c9d5ce8 83
bjo3rn 0:c9bb3c9d5ce8 84 // Created 20 Apr 2005
bjo3rn 0:c9bb3c9d5ce8 85 // Updated 18 Jan 2008 by Tom Igoe
bjo3rn 0:c9bb3c9d5ce8 86 // Adapted 16 Sep 2014 by Bjoern Hartmann for mbed
bjo3rn 0:c9bb3c9d5ce8 87 // This example code is in the public domain.
bjo3rn 0:c9bb3c9d5ce8 88
bjo3rn 0:c9bb3c9d5ce8 89 import processing.serial.*;
bjo3rn 0:c9bb3c9d5ce8 90
bjo3rn 0:c9bb3c9d5ce8 91 Serial myPort; // The serial port
bjo3rn 0:c9bb3c9d5ce8 92 int xPos = 1; // horizontal position of the graph
bjo3rn 0:c9bb3c9d5ce8 93
bjo3rn 0:c9bb3c9d5ce8 94 float minVal=-1.0;
bjo3rn 0:c9bb3c9d5ce8 95 float maxVal=1.0;
bjo3rn 0:c9bb3c9d5ce8 96
bjo3rn 0:c9bb3c9d5ce8 97 void setup () {
bjo3rn 0:c9bb3c9d5ce8 98 // set the window size:
bjo3rn 0:c9bb3c9d5ce8 99 size(400, 300);
bjo3rn 0:c9bb3c9d5ce8 100
bjo3rn 0:c9bb3c9d5ce8 101 // List all the available serial ports
bjo3rn 0:c9bb3c9d5ce8 102 println(Serial.list());
bjo3rn 0:c9bb3c9d5ce8 103 // Open whatever port is the one you're using.
bjo3rn 0:c9bb3c9d5ce8 104 myPort = new Serial(this, "/dev/tty.usbmodem1412", 9600);
bjo3rn 0:c9bb3c9d5ce8 105 // don't generate a serialEvent() unless you get a newline character:
bjo3rn 0:c9bb3c9d5ce8 106 myPort.bufferUntil('\n');
bjo3rn 0:c9bb3c9d5ce8 107 // set inital background:
bjo3rn 0:c9bb3c9d5ce8 108 background(0);
bjo3rn 0:c9bb3c9d5ce8 109 }
bjo3rn 0:c9bb3c9d5ce8 110 void draw () {
bjo3rn 0:c9bb3c9d5ce8 111 // everything happens in the serialEvent()
bjo3rn 0:c9bb3c9d5ce8 112 }
bjo3rn 0:c9bb3c9d5ce8 113
bjo3rn 0:c9bb3c9d5ce8 114 void serialEvent (Serial myPort) {
bjo3rn 0:c9bb3c9d5ce8 115 // get the ASCII string:
bjo3rn 0:c9bb3c9d5ce8 116 String inString = myPort.readStringUntil('\n');
bjo3rn 0:c9bb3c9d5ce8 117
bjo3rn 0:c9bb3c9d5ce8 118 if (inString != null) {
bjo3rn 0:c9bb3c9d5ce8 119 // trim off any whitespace:
bjo3rn 0:c9bb3c9d5ce8 120 inString = trim(inString);
bjo3rn 0:c9bb3c9d5ce8 121 // convert to an int and map to the screen height:
bjo3rn 0:c9bb3c9d5ce8 122 float inFloat = float(inString);
bjo3rn 0:c9bb3c9d5ce8 123 float screenY = map(inFloat, minVal, maxVal, 0, height);
bjo3rn 0:c9bb3c9d5ce8 124
bjo3rn 0:c9bb3c9d5ce8 125 // draw the line from bottom of screen to desired height
bjo3rn 0:c9bb3c9d5ce8 126 stroke(127, 34, 255);
bjo3rn 0:c9bb3c9d5ce8 127 line(xPos, height, xPos, height - screenY);
bjo3rn 0:c9bb3c9d5ce8 128
bjo3rn 0:c9bb3c9d5ce8 129 // at the edge of the screen, go back to the beginning:
bjo3rn 0:c9bb3c9d5ce8 130 if (xPos >= width) {
bjo3rn 0:c9bb3c9d5ce8 131 xPos = 0;
bjo3rn 0:c9bb3c9d5ce8 132 background(0);
bjo3rn 0:c9bb3c9d5ce8 133 } else {
bjo3rn 0:c9bb3c9d5ce8 134 // increment the horizontal position:
bjo3rn 0:c9bb3c9d5ce8 135 xPos++;
bjo3rn 0:c9bb3c9d5ce8 136 }
bjo3rn 0:c9bb3c9d5ce8 137 }
bjo3rn 0:c9bb3c9d5ce8 138 }
bjo3rn 0:c9bb3c9d5ce8 139 */