Hi, I'm a beginner and figured that starting off with a pre-exisiting project would help understand concepts from the ground up.
So I chose to do a simple game controller (Specifically to play angry birds) using a flex sensor and a potentiometer.
LINK: Angry Bird Game Controller using Arduino, Flex Sensor and Potentiometer
So basically after I copy/pasted and followed instructions, I found that it didn't really work. I began to dig through the code and kinda understood some stuff. At first execution the mouse cursor was just stuck. I modified the code a little bit as far as I could understand and actually got the flex sensor to take care of the movement along the x-axis. But I'm stumped with the pot. The flex sensor detects pressure and is used as to register clicks and pull back the bird following which the y-axis is to be manipulated to set the direction for the it.
My problem I think is that once a click is registered, I am unable to read the updated values from the pot. Any guidance would be helpful!
The original code for the project is given in the link above, and here is the code that I modified a bit.
Arduino Code:
void setup() {
Serial.begin(9600); //Transmit at 9600 Baud Rate
}
boolean Mclick = false;
void loop() {
int potValue = analogRead(A0); //variable to store potValue
int FlexValue = analogRead(A1); //variable to store Flex Value
//Serial.print("Flex: ");
//Serial.println(FlexValue);
//Serial.print("POT: ");
//Serial.println(potValue);
if (FlexValue>=600 && FlexValue<=675) //my flex sensor range
{
FlexValue = map (FlexValue,600,675,0,100); //based on bending convert to 0 to 100
Serial.println(FlexValue);
if (FlexValue>=40)
{
Mclick=true;
Serial.write(1); //1 is sent to make mouse left click
Serial.write(FlexValue); //Flex value is the distance to move mouse in X direction
delay(5000); //added these 3 lines to original code
Mclick=false;
Serial.write(0);
}
else
{
Mclick=false;
Serial.write(0);}
}
else
{
Mclick=false;
Serial.write(0);}
if (potValue<=2000)
{
potValue = map(potValue,0,1024,101,201); //Based in turn convert to 101 to 201
Serial.write(potValue); //Pot value is the distance to move mouse in Y direction
}
delay(500); //stability delay
}
Processing Code:
import java.awt.*;
import java.awt.event.*;
import processing.serial.*;
Serial port;
int data,a,b;
int Pull, Turn, crntX, crntY;
boolean click;
Robot robot;
PFont pfont;
Point save_p;
void setup() {
size(320, 240);
port = new Serial(this,Serial.list()[0],9600); //Read the serial values from 0th COM (llok for device manager on your computer to modify this)
println((Object[])Serial.list()); //COM ports are listed for reference
try {
robot = new Robot();
robot.setAutoDelay(0);
}
catch (Exception e) {
e.printStackTrace();
}
pfont = createFont("Impact", 32);
}
void draw() {
background(#ffffff);
fill(#000000);
if (port.available()>0)
{
data=port.read();
println(data); //Read the data from COM port and save it in data
}
if (data>=101 && data<=201) //If the value if from 101 to 201 then it must be from Potentiometer
{
Turn = int (map (data,101,201,0,100)); //USe that value to turn the catapullt
}
if (data>=5 && data <=100) //If the value if from 5 to 100 then it must be from Flex Sensor
{ Pull = int (map(data,5,100,0,100));} //USe that value to pull the catapullt
if (data == 1)
click = true; //USe that value to press the mouse button
if (data == 0)
click = false; //USe that value to release the mouse button
Point p = getGlobalMouseLocation();
textFont(pfont);
text("now x=" + (int)p.getX() + ", y=" + (int)p.getY(), 10, 32); //display the position of X and Y
text ("Pull =" + Pull, 10,100); //Display the value fo pull (Flex Sesnor)
text ("Turn =" + Turn, 10,150); //Display the value fo Turn (Potentiometer)
text ("Click =" + click, 10,200); //Display the status of mouse click
if (click == false) //when Flex Sesnor is not pulled
{
crntX = (int)p.getX() ; crntY = (int)p.getY() ;
//if (Pull>50)
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); //Release the mouse button
}
if (Pull<5)
{
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); //Release the mouse button
delay(500);
}
if (click == true) //when Flex Sesnor is pulled
{
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); //Press the mouse Button
robot.mouseMove(crntX-Pull, crntY+Turn); //Move the mouse based on the Flex and POT value
}
}
Point getGlobalMouseLocation() {
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
Point p = pointerInfo.getLocation();
return p;
}
PS: Most of the modifications I did were just on the range that our sensors and pot differed by in the map function.