Working on aliens pulse rifle for the wife for halloween, and need some assistance.
i have 3 buttons:
rifle trigger
grenade launcher trigger
reload
right now only rifle trigger and reload are displayed in the code, as I am unsure how to implement the grenade button to do what i want it to do.
lets assume the pulse rifle is turned on, and the rifle and grenade launcher have full ammo.
rifle ammo starts at 95
grenade starts at 4
push rifle button - rifle round count displays (95) - we are now in rifle mode
next push fires rifle. (lets say trigger is released at a count of 90 displayed on 7 segment)
push grenade button. - grenade round count displays (4) - we are now in grenade launcher mode
push grenade button - grenade round count drops to 3
push rifle button - displays last value (90) - back to rifle mode
push and hold rifle button - count drops again, and is remembered.
push grenade button once again - grenade value shows - you guessed it, back to grenade launcher mode.
so on and so forth, until a reload button reloads either the rifle or grenade launcher (while theyre in their respective modes):
in rifle mode, reload button reloads the rifle ONLY (ammo count 5).
when in grenade launcher mode, it reloads the grenade launcher ONLY (ammo count 4).
so far i have everything setup for the rifle and reload button only. I have yet to implement the grenade button, because i am unsure how to go about doing this. Basically, i dont know what i dont know and not sure where to begin.
#include "SevSeg.h"
SevSeg sevseg;
unsigned int ammo = 0; //full mag
unsigned long previousMillis = 0;
long interval = 60;
int triggerState = 0; //current state of trigger. will be assigned to digital read starting at 0 for off
int reloadState = 0;
unsigned int lastReloadState = 95;
#define triggerPin 5
#define reloadPin 4
void setup(){
byte numDigits = 2;
byte digitPins[] = {3,2};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12};
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
pinMode(triggerPin, INPUT_PULLUP);
pinMode(reloadPin, INPUT_PULLUP);
int triggerState = digitalRead(triggerPin);
sevseg.refreshDisplay();
sevseg.setNumber(00,2);
}
void loop()
{
int triggerState = digitalRead(triggerPin);
int reloadState = digitalRead(reloadPin);
if (reloadState == LOW && triggerState == HIGH && ammo>=0)
{
ammo = lastReloadState;
sevseg.setNumber(ammo,1);
}
if (triggerState == LOW && reloadState == HIGH && ammo<=95)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
sevseg.setNumber(ammo,1);
ammo--;
}
}
sevseg.refreshDisplay();
}