loading

arduino program led to light up on pin 13 if i press a pushbutton 3 times?

I need to know the function to use and an example would be very helpful thanks and any help is appreciated

sort by: active | newest | oldest

This is a bit long winded but should help.

I like to use a tracking variable. So you can use the basic button sketch and add a variable to track the number of presses. Something like this: (keep in mind i'm editing this on the fly and don't have access to the Arduino IDE at the moment to verify it will compile correctly.)

/*

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

int tracker = 0; //tracking variable to track number of button presses

// 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);

}



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 and track < 3) {
track ++;
}
if (track == 3) {
track = 0

// turn LED on:

digitalWrite(ledPin, HIGH);

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

}

As you can see we change the initial checking of the button state to also check the value of the tracking variable. So if the button has been pressed and the tracking variable isn't where we want it. (in this case 3) We will add 1 to the tracking variable and move on. It then checks the state of just the tracking variable. If it equals what we want it will light the LED and resets the tracking variable. Otherwise the LED stays off.

Using a couple of tracking variables like this will help you get to your goal of a tic-tac-toe game. In the case of your game you'll want to check the state of the tracking variable and perform the correct actions. You'll probably want to heave the arduino keep track of what is light know when a player has won or not. This way the game can be reset when it's finished.

I would suggest looking at a couple of other arduino based games to help understand some other programming techniques that may help. For instance you can check out my Arduino Simon Says game. I tried really hard to break down each part of the code so beginners could understand how it works. You can also check out the improved version of the game. There is a link to it at the end of the first instructable. It adds a 2 player version of Simon to the equasion.

https://www.instructables.com/id/Arduino-Simon-Says...

BTW... If you have any questions please post them here or on the instructable so everyone can benefit from the conversation. Please don't message me.

im trying to follow your code and i get a

if (buttonState == HIGH and track < 3) {

"track" was not declared in this scope

Like i said didn't have the IDE on hand at the time. So the like should be

if(buttonState == HIGH && track<3){

if (button==true) ctr++;

while (button==true) do;

if (ctr==3) {

LED=true;

ctr=0;}