Arduino Sketch MP_GamePadControl
/*
MP_GamePadControl
- Uses gamepad thumbpad to control the setpoint.
- Uses gamepad joystick to move an obect on the screen.
- Uses buttons 1-4 to: Change object image, reset the plot,
take a snapshot and play a WAV vile
Plots analog values of input and setpoint and
digital values of switches and LED state.
The circuit:
* potentiometer connected to A0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital D9 to ground
* Pushbutton swithes on D2 and D3 going to ground.
created 20 July 2012
by Martin Hebel, SelmaWare Solutions
This example code is in the public domain.
*/
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int LEDpin = 9; // Analog output pin that the LED is attached to
const int SW1pin = 2; // Pushbutton switch, raise setpoint
const int SW2pin = 3; // Pushbutton switch, lower setpoint
int sensorValue = 0; // value read from the pot
int setPoint = 500; // Initial value of setPoint
int SW1state = 0; // Store state of SW1
int SW2state = 0; // Store state of SW2
int LEDstate = 0; // Store state of LED
void setup() {
// configure hardware
pinMode(SW1pin, INPUT_PULLUP); // Enable pull-ups on switches
pinMode(SW2pin, INPUT_PULLUP);
pinMode(LEDpin, OUTPUT); // set LED to be an output pin
// initialize serial communications at 9600 bps:
Serial.begin(9600);
config_MP(); // function to configure MakerPlot
}
void loop() {
measureData(); // measure values
gamePadSetPoint(); // Use thumbpadd for setpoint control
gamePadMoveLED(); // Move LED from gamepad
gamePadButtonControl(); // Uses gamepad buttons for various tasks
control(); // control based on settings
plotData(); // display/plot values
// wait 100 milliseconds before the next loop
delay(100);
}
void measureData()
{
// read the analog in value
sensorValue = analogRead(analogInPin);
}
void gamePadSetPoint()
{
int i;
flushBuffer(); // Read POV on gamepad
Serial.println("!READ (gamepad.curPOV)");
i = Serial.parseInt();
if (i==0) // if POV pad up, increase setpoint
setPoint +=10;
if (i==180) // if POV down, decrease setpoint
setPoint -=10;
}
void gamePadMoveLED() // Read right joystick, move object on screen
{
long x,y;
flushBuffer();
Serial.println("!READ (gamepad.curX2)"); // read 0 to 65535 for joystick X position, 32767 center
x = Serial.parseInt();
flushBuffer();
Serial.println("!READ (gamepad.curY2)"); // read 0 to 65535 for joystick Y position, 32767 center
y = Serial.parseInt();
x = 50 + (x/1000 - 32); // convert x and y to 18 to 82
y = 50 - (y/1000 - 32);
Serial.print("!O Target.Move="); // Set LED target object to corrdinates
Serial.print(x);
Serial.print(",");
Serial.println(y);
}
void gamePadButtonControl()
{
long i;
flushBuffer();
Serial.println("!READ (gamepad.Buttons)"); // Read all buttons as 16-bit value
i = Serial.parseInt();
// if button 1 (bit 0) pressed, change target state to 1
if (i & 1 > 0)
Serial.println("!O Target=1");
else
Serial.println("!O Target=0");
// if button 2 (bit 1) pressed, reset plot (no interface effects)
if ((i >> 1) & 1 > 0)
Serial.println("!RSET");
// if button 3 (bit 2) pressed, take snapshot with interface effects
if ((i >> 2) & 1 > 0)
{
Serial.println("!O butSnap=1");
Serial.println("!O butSnap.Run");
Serial.println("!O butSnap=0");
}
// if button 4 (bit 3) pressed, applause!
if ((i >> 3) & 1 > 0)
Serial.println("~PWAV clap");
}
void plotData()
{
// print the analog values formatted for MakerPlot
Serial.print(sensorValue); // send 1st value
Serial.print(","); // send comma delimiter
Serial.println(setPoint); // send 2nd value with carriage return
// print the digital values formatted for MakerPlot
Serial.print("%"); // send binary indicator
Serial.print(SW1state); // send 1/0 for SW1
Serial.print(SW2state); // send 1/0 for SW2
Serial.println(LEDstate); // send 1/0 for LED with carriage return
}
void control()
{
// Check pot value against setpoint, if above light LED
if (sensorValue > setPoint)
{
LEDstate = 1;
}
else
LEDstate = 0;
digitalWrite(LEDpin,LEDstate);
}
void config_MP()
{
delay(2000); // allow connection to stabilize
Serial.println(); // send in case garbage in queue
Serial.println("!RSET"); // Reset the plot
Serial.println("!PPER 75,30"); // Resize plot to move out of way
// create an LED on screen for moving with gamepad, object named Target
Serial.println("!POBJ oImgBut.target=50.,50.,2.,3.,dev\\leds\\g_led_grn_1.GIF,0,dev\\leds\\g_LED_red_1.gif,0");
Serial.println("!SPAN 0,1000"); // set span of Y-axis, 0 to 1000
Serial.println("!TMAX 180"); // Set time to 180 seconds
Serial.println("!GRID 20,20"); // set grid to 20 x 20
Serial.println("!PLOT ON"); // reset plot, send CR as MP value, enable plotting
// clear constant drawings and place constant text on plot
Serial.println("!CLRC(CR)@TEXT 30A,105A,1.5A,(Blue),Gamepad Controlled from Arduino");
}
void flushBuffer()
{
Serial.flush(); // flush serial buffer (may not always flush?)
while (Serial.available()) // manually empty as well, read as long as bytes in there
Serial.read();
}
MP_GamePadControl
- Uses gamepad thumbpad to control the setpoint.
- Uses gamepad joystick to move an obect on the screen.
- Uses buttons 1-4 to: Change object image, reset the plot,
take a snapshot and play a WAV vile
Plots analog values of input and setpoint and
digital values of switches and LED state.
The circuit:
* potentiometer connected to A0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital D9 to ground
* Pushbutton swithes on D2 and D3 going to ground.
created 20 July 2012
by Martin Hebel, SelmaWare Solutions
This example code is in the public domain.
*/
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int LEDpin = 9; // Analog output pin that the LED is attached to
const int SW1pin = 2; // Pushbutton switch, raise setpoint
const int SW2pin = 3; // Pushbutton switch, lower setpoint
int sensorValue = 0; // value read from the pot
int setPoint = 500; // Initial value of setPoint
int SW1state = 0; // Store state of SW1
int SW2state = 0; // Store state of SW2
int LEDstate = 0; // Store state of LED
void setup() {
// configure hardware
pinMode(SW1pin, INPUT_PULLUP); // Enable pull-ups on switches
pinMode(SW2pin, INPUT_PULLUP);
pinMode(LEDpin, OUTPUT); // set LED to be an output pin
// initialize serial communications at 9600 bps:
Serial.begin(9600);
config_MP(); // function to configure MakerPlot
}
void loop() {
measureData(); // measure values
gamePadSetPoint(); // Use thumbpadd for setpoint control
gamePadMoveLED(); // Move LED from gamepad
gamePadButtonControl(); // Uses gamepad buttons for various tasks
control(); // control based on settings
plotData(); // display/plot values
// wait 100 milliseconds before the next loop
delay(100);
}
void measureData()
{
// read the analog in value
sensorValue = analogRead(analogInPin);
}
void gamePadSetPoint()
{
int i;
flushBuffer(); // Read POV on gamepad
Serial.println("!READ (gamepad.curPOV)");
i = Serial.parseInt();
if (i==0) // if POV pad up, increase setpoint
setPoint +=10;
if (i==180) // if POV down, decrease setpoint
setPoint -=10;
}
void gamePadMoveLED() // Read right joystick, move object on screen
{
long x,y;
flushBuffer();
Serial.println("!READ (gamepad.curX2)"); // read 0 to 65535 for joystick X position, 32767 center
x = Serial.parseInt();
flushBuffer();
Serial.println("!READ (gamepad.curY2)"); // read 0 to 65535 for joystick Y position, 32767 center
y = Serial.parseInt();
x = 50 + (x/1000 - 32); // convert x and y to 18 to 82
y = 50 - (y/1000 - 32);
Serial.print("!O Target.Move="); // Set LED target object to corrdinates
Serial.print(x);
Serial.print(",");
Serial.println(y);
}
void gamePadButtonControl()
{
long i;
flushBuffer();
Serial.println("!READ (gamepad.Buttons)"); // Read all buttons as 16-bit value
i = Serial.parseInt();
// if button 1 (bit 0) pressed, change target state to 1
if (i & 1 > 0)
Serial.println("!O Target=1");
else
Serial.println("!O Target=0");
// if button 2 (bit 1) pressed, reset plot (no interface effects)
if ((i >> 1) & 1 > 0)
Serial.println("!RSET");
// if button 3 (bit 2) pressed, take snapshot with interface effects
if ((i >> 2) & 1 > 0)
{
Serial.println("!O butSnap=1");
Serial.println("!O butSnap.Run");
Serial.println("!O butSnap=0");
}
// if button 4 (bit 3) pressed, applause!
if ((i >> 3) & 1 > 0)
Serial.println("~PWAV clap");
}
void plotData()
{
// print the analog values formatted for MakerPlot
Serial.print(sensorValue); // send 1st value
Serial.print(","); // send comma delimiter
Serial.println(setPoint); // send 2nd value with carriage return
// print the digital values formatted for MakerPlot
Serial.print("%"); // send binary indicator
Serial.print(SW1state); // send 1/0 for SW1
Serial.print(SW2state); // send 1/0 for SW2
Serial.println(LEDstate); // send 1/0 for LED with carriage return
}
void control()
{
// Check pot value against setpoint, if above light LED
if (sensorValue > setPoint)
{
LEDstate = 1;
}
else
LEDstate = 0;
digitalWrite(LEDpin,LEDstate);
}
void config_MP()
{
delay(2000); // allow connection to stabilize
Serial.println(); // send in case garbage in queue
Serial.println("!RSET"); // Reset the plot
Serial.println("!PPER 75,30"); // Resize plot to move out of way
// create an LED on screen for moving with gamepad, object named Target
Serial.println("!POBJ oImgBut.target=50.,50.,2.,3.,dev\\leds\\g_led_grn_1.GIF,0,dev\\leds\\g_LED_red_1.gif,0");
Serial.println("!SPAN 0,1000"); // set span of Y-axis, 0 to 1000
Serial.println("!TMAX 180"); // Set time to 180 seconds
Serial.println("!GRID 20,20"); // set grid to 20 x 20
Serial.println("!PLOT ON"); // reset plot, send CR as MP value, enable plotting
// clear constant drawings and place constant text on plot
Serial.println("!CLRC(CR)@TEXT 30A,105A,1.5A,(Blue),Gamepad Controlled from Arduino");
}
void flushBuffer()
{
Serial.flush(); // flush serial buffer (may not always flush?)
while (Serial.available()) // manually empty as well, read as long as bytes in there
Serial.read();
}