# How to break button 2 when button 3 is hit

Hi

I be working on a game named kop van just, its a funfair game for kids just like on a funfair, you must push the hammer on a button and pin is going upstairs. first, when get a good hit, push button 2 is going on (high) and led2 is burning, but when you have a greater hit with the hammer he goes to button 3 and led 3 is going on. so far so good.

But the question, how can i make it so that when i hit button 3 that led1 and led2 are burning for delay time 2000 and after that everything gonne a be low.

I hope someone can help me and hope the question is clear.

thanks a lot.

this is what i have made for now, its the delay from button 2 what get the hold for button 3

``````// set pin numbers:
const int buttonPin2 = 2;     // the number of the pushbutton pin
const int buttonPin3 = 3;     // the number of the pushbutton pin
const int ledPin13 =  13;      // the number of the LED pin
const int ledPin12 =  12;      // 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(ledPin12, OUTPUT);
pinMode(ledPin13, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}

void loop(){
// read the state of the pushbutton value:
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin13, HIGH);
delay(2000);
digitalWrite(ledPin13, LOW);
}
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin12, HIGH);
delay(2000);
digitalWrite(ledPin12, LOW);
}

}
``````

You need to avoid the use of delays. You’ll need to use millis to manage your delays instead and that will allow you to react quickly to turn led2 off when led3 comes on.

The blink without delay example illustrates how you can use millis to manage time controlled events.

okay

so must used milliseconds and not delay. can i used also a potentialmeter for milliseconden to change this and how program it

i have other prints program it with potentialmeter and the delays, i know how thats works, but can i use a meter also for miliseconds?

You can certainly use a pot to control, say, the length of time the leds stay on. Use the pot as part of a voltage divider and analogRead to get a value to use to control that time.

I'd suggest though that you get the thing working with a constant delay first. People generally take a little time to get the hang of millis instead of delay - no need to add a pot into any initial confusion.

okay thanks,

look like this?

``````// constants won't change. They're used here to
// set pin numbers:
int sensorPin = A5;    // select the input pin for the potentiometer
const int buttonPin2 = 2;     // the number of the pushbutton pin
const int buttonPin3 = 3;     // the number of the pushbutton pin
const int ledPin13 =  13;      // the number of the LED pin
const int ledPin12 =  12;      // the number of the LED pin
int sensorValue = 0;  // variable to store the value coming from the sensor

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin12, OUTPUT);
pinMode(ledPin13, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}

void loop(){
// read the state of the pushbutton value:
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin13, HIGH);
delayMicroseconds (sensorValue);
digitalWrite(ledPin13, LOW);
}
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin12, HIGH);
digitalWrite(ledPin13, HIGH);
delayMicroseconds (sensorValue);
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, LOW);

}

}
``````

What I'm suggesting is that you use the millis() function to control your LEDs. When you get a high signal on either of your buttons, store the time as well as turning on the appropriate LED. Don't use delays, whether delay or delaymicroseconds. Using the subtraction method you see in "blink without delay", establish whether two seconds have passed since you turned an led on. If so, turn them both off.

Once you have that figured out, add the pot so that you can use it to vary how long the leds stay on - if that's what you were trying to use it for.

Oke,

I think i understand what you mean. Firstly working only with mili seconds to turn leds on, not delay.

Is there a topic or working example, maby search query to read information about that? So i can find hows that works? I have search somethings but what i found its not working on a simulator, that what i try first before i program it on the breadbaord

Thanks allot

millis() documentation: http://arduino.cc/en/Reference/Millis#.UwRg1vldXQA

i didn’t get very well what your program should do, but this one should be similar to what you want to do without using delays:

// constants won’t change. They’re used here to
// set pin numbers:
int sensorPin = 5; // select the input pin for the potentiometer
const int buttonPin2 = 2; // the number of the pushbutton pin
const int buttonPin3 = 3; // the number of the pushbutton pin
const int ledPin13 = 13; // the number of the LED pin
const int ledPin12 = 12; // the number of the LED pin
int sensorValue = 0; // variable to store the value coming from the sensor

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

unsigned long timeLed12=4294967295L;
unsigned long timeLed13=4294967295L;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin12, OUTPUT);
pinMode(ledPin13, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
}

void loop(){
// check if the pushbutton is pressed.