We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
For a project I'm trying to make a game with processing and arduino. I'm making a game where you can move a basketball hoop to the left and right off the screen to catch falling basketballs. I'm trying to get the hoop to move to the right when the button is pressed and to the left when it isn't. For my arduino i'm using the 'button' code from the examples library.
My problem is that the hoop only moves to the left and doesn't move to the right when I press the button. Can somebody please see what I'm doing wrong?
my processing code is as follows:
///////////////////////////////////////////////////////////////////////
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int buttonPin = 2;
int buttonState = 0;
int Xbas = 0; // X coördinate for basket
void setup() {
size(1350, 900);
smooth();
arduino = new Arduino(this, Arduino.list()[0], 100);
arduino.pinMode(buttonState, Arduino.INPUT);
}
void Basket (int x, int y) {
background(100);
strokeWeight(1);
pushMatrix();
translate(width/2, height);
for (int i = Xbas - 100; i <= Xbas + 40; i += 20) {
line(i, -150, i + 30, -10);
}
for (int i = Xbas - 40; i <= Xbas + 100; i += 20) {
line(i, -150, i - 30, - 10);
}
line(Xbas + 60, - 150, Xbas + 80, - 70);
line(Xbas + 80, - 150, Xbas + 90, - 110);
line(Xbas - 60, - 150, Xbas - 80, - 70);
line(Xbas - 80, - 150, Xbas - 90, - 110);
fill(#CE3419);
ellipse(Xbas, -150, 200, 30);
fill(100);
ellipse(Xbas, -150, 130, 10);
popMatrix();
}
void draw() {
Basket (0, 0);
if (arduino.digitalRead(buttonState) == Arduino.LOW) {
Xbas += 2;
} else if (arduino.digitalRead(buttonState) == Arduino.HIGH) {
Xbas -= 2;
}
}
//////////////////////////////////////////////////////////////////
Answers
I guess you have your button at pin 2 on the arduino? You have a variable that says so, but you never use it. Replacing "buttonState" with "buttonPin" everywhere could fix that.
Thanks for your response! However it still doesn't work.
My new code is:
The code I use with arduino is as follows:
Ok, you use a custom sketch on your arduino and the firmata-library in processing. That won't work, you have to upload the firmata-firmware to your arduino.
From the arduino-example code:
Okay, and what do I have to change to the processing code after uploading the firmata firmware? The if statement still doesn't work.
Ok, i see that the firmata examples use a baudrate of 57600, so try this:
arduino = new Arduino(this, Arduino.list()[0], 57600);
If that does not work, verify that you choose the right serial-port.
Oh man you are amazing! It works! Thanks for your help, I really appreciate it
As per your post and the my experience with these thing it is looking that your button at pin 2 on the arduino? You have a variable that says so, but you never use it. Replacing "buttonState" with "buttonPin" everywhere could fix that. Also you have the need to follow this change arduino = new Arduino(this, Arduino.list()[0], 57600);
assembly and pcb