I'm trying to build a small Push button counter for my Airsoft Rifle.
I have successfully made my program count down if I press the Trigger button from 120 (full magazine)
to 0 (Empty Magazine), now my problem is that I want to reset the counter anytime and I don't know how
maybe some of you know how I can reset the counter and can explain it to me, so that I can learn it bec. I'm a bit of a noob.
Example: I have shot 40 times now my OLED show's me that I have 80 rounds left, so I start reloading and press the Reload button and my OLED show's 120 again
I'm using a Arduino Nano, Two Push Buttons and One 128x32 i2c OLED
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int counter = 121; // This is A Full Magazine
const int Trigger = 2; // This will be The Fire Button that is counting down to 0 each Trigger pull
const int Reload = 3; // This will be the Reload Button that Resets the counter to 120
int ButtonStateNow =1; // This reads the New State of the Trigger
int ButtonStateOld =1; // This reads the Old State of the Trigger
void setup() {
pinMode(Trigger, HIGH);
pinMode(Reload, INPUT);
delay(100);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.setTextColor(WHITE);
display.dim(false); // This makes Sure that the OLED is on Full Brightness
}
void loop() {
ButtonStateNow=digitalRead(Trigger);
if ((ButtonStateOld==1 && ButtonStateNow==0)||counter==-1) {
counter--;
if(counter<1) {
counter=0;
}
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0,0);
display.print("Ammo");
display.setTextSize(1);
display.setCursor(45,0);
display.print(counter);
display.display();
}
delay(20);
ButtonStateOld=ButtonStateNow;
}
didn't worked if I pulled the trigger button he wouldn't even count down anymore. Then i just randomly change the code to that one.
You seem to prefer try & error over expanding your knowledge. Well that's one way to finish a project.
Though I doubt you will be faster than with learning the basics. It means to come back to the user-forum for almost every single detail if something does not work: I want to encourage you to give the following tutorial a t least a try.
It is easy to understand and a good mixture between writing about important concepts
and get you going.
You can stop reading after two or three pages.
Anyway even with just asking. You should post your complete code from the very first to very last line and a hand drawn schematic that others can make a suggestion that has a high chance to work.
trying to safe time through posting just a few lines now costs you even more time because another quick guessing from
others has a high chance to not work again but posting your complete has a high chance to make it work.
I'm very patient maybe you need some more rounds of quick asking quick guessings to learn this lesson
maniastonks:
I have successfully made my program count down if I press the Trigger button from 120 (full magazine)
to 0 (Empty Magazine), now my problem is that I want to reset the counter anytime and I don't know how
maybe some of you know how I can reset the counter and can explain it to me, so that I can learn it bec. I'm a bit of a noob.
It seems like just adding a long press capability to the trigger would do what you want. Try a site search on 'long press'. IIRC one or more of the button libraries offers this also.
The value of the constants OUTPUT=1 HIGH=1
So what you are doing is setting the pins to OUTPUT (instead of INPUT) and next create a shortcircuit with your trigger. You are lucky that the pin connections did not burn-out, but I can't guarantee that it will keep working that way.
The problem is that you did not define the electrical connection of the input when the switch is open. So with the trigger not engaged the input is floating and picks up any electrical disturbance.
Your code is looking for a low to high transition, so with the trigger not engaged the input voltage must be low. All it needed was a 10k pull down resistor from the input pin to the ground.
The other solution is to use the internal pull-up resistor, which is what the INPUT_PULLUP is for, but then you must connect the switches to the ground and rewrite your code so that it looks for a high to low transition.