Hello,
I plan to make targets for airsoft practise using buttons and I need to write a program to count and add the points when the bullet hits the button. Also, I would like for the program to send the data via bluetooth and a way to reset the points. Any help would be much appreciated!
Thanks,
Thomas.
what kind of help?
Start with the digital read examples built into the IDE. Then look at the state change example. Start out with one button that you press with your finger. Once that's working and you know how to code around that then move on to the next step and add more buttons. Keep going one small step at a time and you'll get there.
Or you can also do what a lot of people do and go find three or four examples of code that sort of do something close to what you want and spend a few months trying to cobble them together without any understanding so you can get it all done with all the pieces at once. That will take a lot longer. This thread will grow into the hundreds of responses. And after some time you'll decide it isn't going to work and move on to something else.
Thanks for the tips. I was wondering if anyone knows a basic code I can develop further.
My state change for active low inputs tutorial has code examples and schematics for reading a button to count button presses (state change detection).
If there are long wires between the switch(es) and the processor it may be necessary to use an external resistor of a lower value (10K or less, 1K minimum) to provide a stronger pullup. Also add the 0.1uF cap to bypass electrical noise picked up by the wire.
Like I said, start with the built in examples in the IDE. Don't try to start from something too complicated that you don't understand it.
How much coding / electronics experience do you have?
}
This is the code I'm starting with (with two buttons) as my knowledge in coding is at a basic level.
void setup()
{
pinMode(2, INPUT);
pinMode(12, OUTPUT);
pinMode(3, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (digitalRead(2) == HIGH) {
digitalWrite(12, HIGH);
delay(1000); // Wait for 100 millisecond(s)
digitalWrite(12, LOW);
delay(500); // Wait for 100 millisecond(s)
Serial.println(" Player 1 ");
Serial.println(" 20 ");
Serial.println(" 10 ");
}
if (digitalRead(3) == HIGH) {
digitalWrite(13, HIGH);
delay(1000); // Wait for 100 millisecond(s)
digitalWrite(13, LOW);
delay(500); // Wait for 100 millisecond(s)
Serial.println(" 3 ");
}
}
It's usually better to wire your buttons so they connect to ground and read low when pressed. Then you can use pinMode INPUT_PULLUP and you don't need an extra resistor for the button.
fun stuff..
instead of normal led and bluetooth, used neopixel rings and small display..
added a reset button and used 4 rings/buttons..
used arrays so can be expanded..
maybe it gives you some ideas..
#include <Adafruit_NeoPixel.h>
#include <TM1637.h>
#define NUM_RINGS 4
#define RESET_BTN 12
//display pins
#define DISPLAY_CLK A3
#define DISPLAY_DIO A4
TM1637 display;
//leds per ring
#define RING1 8
#define RING2 16
#define RING3 24
#define RING4 32
#define LED_PIN A5
#define LED_COUNT RING1+RING2+RING3+RING4
Adafruit_NeoPixel rings(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
const byte buttonPins[NUM_RINGS] = {2, 3, 4, 5};
const byte pointValues[NUM_RINGS] = {100, 50, 25, 10};
unsigned long buttonTimes[NUM_RINGS];
const unsigned long intervalDebounce = 50ul;
unsigned long ringTimes[NUM_RINGS];
const unsigned long intervalLedOn = 1000ul;
byte buttonStates[NUM_RINGS] = {1, 1, 1, 1};
byte ringStates[NUM_RINGS] = {1, 1, 1, 1};
volatile uint32_t totalPoints;
unsigned long lastReset;
byte stateReset = 1;
void setup()
{
Serial.begin(115200);
//init detection buttons
for (int i = 0; i < NUM_RINGS; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
//init reset button
pinMode(RESET_BTN, INPUT_PULLUP);
//init the rings
rings.begin();
rings.show();
rings.setBrightness(255);
//light up rings..
int i;
for (i = 0; i < 8; i++) {
rings.setPixelColor(i, rings.Color(255, 0, 0));
}
rings.show();
delay(500);
for (i = 8; i < 24; i++) {
rings.setPixelColor(i, rings.Color(0, 255, 0));
}
rings.show();
delay(500);
for (i = 24; i < 48; i++) {
rings.setPixelColor(i, rings.Color(0, 0, 255));
}
rings.show();
delay(500);
for (i = 48; i < 80; i++) {
rings.setPixelColor(i, rings.Color(255, 255, 0));
}
rings.show();
//init scoreboard display
display.begin(DISPLAY_CLK, DISPLAY_DIO, 4);
display.displayClear();
display.displayInt(0);
delay(500);
}
void loop()
{
unsigned long now = millis();
for (int i = 0; i < NUM_RINGS; i++) {
if (now - buttonTimes[i] >= intervalDebounce) {
byte val = digitalRead(buttonPins[i]);
if (val != buttonStates[i]) {
buttonTimes[i] = now;//start debouncing..
buttonStates[i] = val;
if (val == LOW) {
totalPoints += pointValues[i];//add the points based on preset values
Serial.print("Hit Button: "); Serial.println(i + 1);
Serial.print("Total Points: "); Serial.println(totalPoints);
switch (i) {
case 0: {
for (int i = 0; i < 8; i++) {
rings.setPixelColor(i, rings.Color(255, 0, 0));
}
rings.show();
} break;
case 1: {
for (int i = 8; i < 24; i++) {
rings.setPixelColor(i, rings.Color(0, 255, 0));
}
rings.show();
} break;
case 2: {
for (int i = 24; i < 48; i++) {
rings.setPixelColor(i, rings.Color(0, 0, 255));
}
rings.show();
} break;
case 3: {
for (int i = 48; i < 80; i++) {
rings.setPixelColor(i, rings.Color(255, 255, 0));
}
rings.show();
} break;
}
ringTimes[i] = now; //start led timer
ringStates[i] = 1;
display.displayInt(totalPoints);
}
}
}
//see if it's time to turn off a ring
if (now - ringTimes[i] >= intervalLedOn && ringStates[i] == 1) {
ringStates[i] = 0;
switch (i) {
case 0: {
for (int i = 0; i < 8; i++) {
rings.setPixelColor(i, rings.Color(0, 0, 0));
}
rings.show();
} break;
case 1: {
for (int i = 8; i < 24; i++) {
rings.setPixelColor(i, rings.Color(0, 0, 0));
}
rings.show();
} break;
case 2: {
for (int i = 24; i < 48; i++) {
rings.setPixelColor(i, rings.Color(0, 0, 0));
}
rings.show();
} break;
case 3: {
for (int i = 48; i < 80; i++) {
rings.setPixelColor(i, rings.Color(0, 0, 0));
}
rings.show();
} break;
}
}
}
//handle reset button
if (now - lastReset >= intervalDebounce) {
byte r = digitalRead(RESET_BTN);
if (r != stateReset) {
lastReset = now;
stateReset = r;
if (stateReset == LOW) {
totalPoints = 0;
display.displayInt(0);
}
}
}
}
have fun.. ~q
int addButtonPin2 = 2; // Pin for the "add points" button
int addButtonPin3 = 3;
int addButtonPin4 = 4;
int addButtonPin5 = 5;
int points = 0; // Initialize a variable to store points
void setup() {
pinMode(addButtonPin2, INPUT);
pinMode(addButtonPin3, INPUT);
pinMode(addButtonPin4, INPUT);
pinMode(addButtonPin5, INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(addButtonPin2) == HIGH) {
points += 10;
delay(500);
Serial.println ("Points");
Serial.println(points);
}
if (digitalRead(addButtonPin3) == HIGH) {
points += 20;
delay(500);
Serial.println ("Points");
Serial.println(points);
}
if (digitalRead(addButtonPin4) == HIGH) {
points += 50 ;
delay(500);
Serial.println ("Points");
Serial.println(points);
}
if (digitalRead(addButtonPin5) == HIGH) {
points += 100 ;
delay(500);
Serial.println ("Points");
Serial.println(points);
}
if (Serial.available() > 0) {
char data = Serial.read();
switch (data){
case '1':
points = 0;
delay(500);
Serial.println ("Points");
Serial.println(points);
}
}
}
type or paste code here
Here is the code i wrote. In tinkercad it work well. Do you think it will work in reality as well.
Thanks
Thomas
Do you have pull-down resistors for your buttons? If you wire them the other way round you won't need them. But you have to be able to wrap your head around LOW meaning "pressed" which seems to confuse noobs for some reason.
Yes. I use 1k resistor. Is that ok? Tinkercut works the reverse so I change it before inserting into Arduino ID. I have also added a buzzer and led. What would be the best resistance for 5V output from arduino.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.