dont know what you mean
Try it with this edit:
const int BUTTON1_PIN = 2;
const int BUTTON2_PIN = 3;
const int BUTTON3_PIN = 4;
const int LED_PIN = 5;
const int buttons[] = {BUTTON1_PIN, BUTTON2_PIN, BUTTON3_PIN};
int buttonState[3];
void setup()
{
for (int i = 0; i < 3; i++)
{
pinMode(buttons[i], INPUT);
buttonState[i] = buttons[i];
}
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
}
void loop()
{
bool ledState = false;
delay(100);
int buttonCount = 0;
for (int i = 0; i < 3; i++)
{
// Check if button pressed
if((digitalRead(buttons[i]) == LOW) && (buttonState[i] != -1))
{
buttonState[i] = -1;
buttonCount++;
}
}
if ((ledState == false) && (buttonCount == 3))
{
// All buttons were pressed
digitalWrite(LED_PIN, HIGH);
}
if ((ledState == true) &&(buttonCount > 0))
{
// Button pressed after LED on
digitalWrite(LED_PIN, LOW);
while(1);
}
}
ahh you mean code. Yeah now the LED lights up, but without any press on the push button
Sorry, but me trying to edit someone else's code won't work. Wait for @cedarlakeinstruments to return.
no problem! Thanks!
just for notice. This is the Target for me. Want to hit every target before the servo motor pull down the target bank. Reset Switch behind to come up again.
Oh, I'm here, observing the proceedings with interest.
At no point was this expected to be fully tested, Known Good working code. It was intended as a (hopefully almost complete) base for OP to start from and to illustrate a concept. I expected that OP would be interested in understanding how it worked and what was needed to fit into whatever system they were building.
I may have been wrong about that.
Well, I'm still confused as to whether the OP wants to push, latch and release each button or press all at the same time or... Maybe he/she will come back and clarify.
no i want to hit the button with a ball... so latch and release..but arduino must stil count as a hit for the target
OK. Here's a simplified version. Yes, I tested it and I think that I may have misunderstood this
One button more for LED goes off.
So I compensated for it with a separate button to reset the LED
Have fun.
const int BUTTON1_PIN = 2;
const int BUTTON2_PIN = 3;
const int BUTTON3_PIN = 4;
const int RESET_LED_PIN = 5;
const int NUM_BUTTONS = 3;
const int buttons[NUM_BUTTONS] = {BUTTON1_PIN, BUTTON2_PIN, BUTTON3_PIN};
byte buttonState = 0;
void setup()
{
for (int i = 0; i < NUM_BUTTONS; i++)
{
pinMode(buttons[i], INPUT_PULLUP);
}
pinMode(RESET_LED_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop()
{
static bool ledState = false;
delay(100);
for (int i = 0; i < NUM_BUTTONS; i++)
{
// Check if button pressed
if(digitalRead(buttons[i]) == LOW)
{
buttonState |= (1 << i);
}
}
// Check if all buttons pressed
if ((ledState == false) && (buttonState == ((1 << NUM_BUTTONS) - 1) ))
{
// All buttons were pressed
digitalWrite(LED_BUILTIN, HIGH);
ledState = true;
}
// Check if LED should go off
if ((ledState == true) &&(digitalRead(RESET_LED_PIN) == LOW))
{
// Button pressed after LED on
digitalWrite(LED_BUILTIN, LOW);
ledState = false;
buttonState = 0;
}
}
okay work yet! Thank you so much!!! Problem was my wiring
do you think instead of an LED you can easy bring in a servo Motor?
I could, but I'll leave that detail to you ![]()
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
