Arduino input to move object when button is pressed

edited October 2015 in Arduino

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:

    import processing.serial.*;
    import cc.arduino.*;
    
    Arduino arduino;
    
    
    int buttonPin = 2;
    
    
    
    int Xbas = 0;  // X coördinate for basket
    
    
    
    void setup() {
      size(1350, 900);
      smooth();
      arduino = new Arduino(this, Arduino.list()[0], 9600);
      arduino.pinMode(buttonPin, 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(buttonPin) == Arduino.LOW) {
        Xbas += 2;
      } else if (arduino.digitalRead(buttonPin) == Arduino.HIGH) {
        Xbas -= 2;
      }
    }
    

    The code I use with arduino is as follows:

    /*
      Button
    
     Turns on and off a light emitting diode(LED) connected to digital
     pin 13, when pressing a pushbutton attached to pin 2.
    
    
     The circuit:
     * LED attached from pin 13 to ground
     * pushbutton attached to pin 2 from +5V
     * 10K resistor attached to pin 2 from ground
    
     * Note: on most Arduinos there is already an LED on the board
     attached to pin 13.
    
    
     created 2005
     by DojoDave <http://www.0j0.org>;
     modified 30 Aug 2011
     by Tom Igoe
    
     This example code is in the public domain.
    
     http://www.arduino.cc/en/Tutorial/Button
     */
    
    // constants won't change. They're used here to
    // set pin numbers:
    const int buttonPin = 2;     // the number of the pushbutton pin
    const int ledPin =  13;      // the number of the LED pin
    
    // variables will change:
    int buttonState = 0;         // variable for reading the pushbutton status
    
    void setup() {
      // initialize the LED pin as an output:
      pinMode(ledPin, OUTPUT);
      // initialize the pushbutton pin as an input:
      pinMode(buttonPin, INPUT);
      Serial.begin(9600);
    }
    
    void loop() {
      // read the state of the pushbutton value:
      buttonState = digitalRead(buttonPin);
    
      // check if the pushbutton is pressed.
      // if it is, the buttonState is HIGH:
      if (buttonState == HIGH) {
        // turn LED on:
        digitalWrite(ledPin, HIGH);
      }
      else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
      }
    }
    
  • Answer ✓

    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:

    Using the Arduino software, upload the StandardFirmata example (located in Examples > Firmata > StandardFirmata) to your Arduino board.

  • Okay, and what do I have to change to the processing code after uploading the firmata firmware? The if statement still doesn't work.

  • Answer ✓

    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

  • edited November 2015

    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

Sign In or Register to comment.