Go Down

### Topic: button counting (Read 1 time)previous topic - next topic

#### Ashleysmith

##### Mar 05, 2012, 12:48 pm
how do i do a "button pressed" counter?

i'm using the PSX2 controller and would like to make my arduino have some lights on it, but not sure on the programming...

basically i want  the cross, triangle, square and circle to each have a pair (poss 2 pairs) of leds...

#### PaulS

#1
##### Mar 05, 2012, 12:54 pm
Quote
how do i do a "button pressed" counter?

Code: [Select]
`int buttunPressedCount = 0;`
Or 3 or 9 or -17...

Quote
i'm using the PSX2 controller

To push the switches?

Quote
and would like to make my arduino have some lights on it,

No, wait, you are using the PSX2 controller to install the LEDs?

Quote
basically i want  the cross, triangle, square and circle to each have a pair (poss 2 pairs) of leds...

What cross? What triangle? What square? What circle?

#### kenny_-_

#2
##### Mar 05, 2012, 12:56 pmLast Edit: Mar 05, 2012, 12:59 pm by AWOL Reason: 1
Or do you mean button basics like the one in the arduino community?
Code: [Select]
`/*  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 pinconst int ledPin =  13;      // the number of the LED pin// variables will change:int buttonState = 0;         // variable for reading the pushbutton statusvoid setup() {  // initialize the LED pin as an output:  pinMode(ledPin, OUTPUT);        // initialize the pushbutton pin as an input:  pinMode(buttonPin, INPUT);     }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);  }}`

Moderator edit: CODE TAGS

#### Ashleysmith

#3
##### Mar 05, 2012, 12:59 pm

i have basically got an arduino mega attached to a wireless reciever and servo output.... the board is inside a remote controlled car

voila

and i want to put headlighs and brake lights in the car...

on the wireless controller i have a cross, triangle, square and circle buttons, i would like to use them as push buttons to controll the LEDs

#### kenny_-_

#4
##### Mar 05, 2012, 01:20 pm
Why is the code posted above not working for you than? You asked about an input that is high (controller) and it should make an output high (leds)?
it sounds like the button example
What am i missing here?

#### PaulS

#5
##### Mar 05, 2012, 01:23 pm
Quote
on the wireless controller i have a cross, triangle, square and circle buttons, i would like to use them as push buttons to controll the LEDs

So, what is it you need to count?

#### Ashleysmith

#6
##### Mar 05, 2012, 01:27 pm
the buttons only work when pressed, so i want to press it once and the LEDs stay on and press again and the LEDs turn off, then it resets

#### PaulS

#7
##### Mar 05, 2012, 01:59 pm
Quote
the buttons only work when pressed, so i want to press it once and the LEDs stay on and press again and the LEDs turn off, then it resets

So, on the Arduino, you need to detect when the switch transitions from not pressed to pressed. When that happens, increment a variable.

When the value in the variable is odd, do one thing. When it is even, do another.