Skip to content

Instantly share code, notes, and snippets.

@christrotter
Created November 9, 2021 00:12
Show Gist options
  • Save christrotter/e0f870fb01d8c40aad4494fb66b96a01 to your computer and use it in GitHub Desktop.
Save christrotter/e0f870fb01d8c40aad4494fb66b96a01 to your computer and use it in GitHub Desktop.
Arduino Nerf gun - the orange one
/*
So sorry this code has no loops or parameterization, it's terrible, just putting it somewhere that won't get lost.
*/
#include <Bounce2.h>
const int buttonPin = 7; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
int buttonValue;
Bounce2::Button button = Bounce2::Button();
int chargedLEDs = 0;
int counter = 0;
int countModifier = 50;
int newCount;
int fadeAmount = 10;
int brightness = 0;
int LED1 = 3;
int LED2 = 5;
int LED3 = 6;
int LED4 = 9;
int LED5 = 10;
byte brightnessLED1 = 0;
byte brightnessLED2 = 0;
byte brightnessLED3 = 0;
byte brightnessLED4 = 0;
byte brightnessLED5 = 0;
int countLED1 = 1;
int countLED2 = 2;
int countLED3 = 3;
int countLED4 = 4;
int countLED5 = 5;
int countLED;
bool LED1charged { false };
bool LED2charged { false };
bool LED3charged { false };
bool LED4charged { false };
bool LED5charged { false };
bool LED1charging { false };
bool LED2charging { false };
bool LED3charging { false };
bool LED4charging { false };
bool LED5charging { false };
int bodyLEDpin = 12;
int flasherDelay = 100;
int flashDurationCount = 25;
bool firstRun = true;
void setup() {
Serial.begin(115200);
pinMode(LED1, OUTPUT); // initialize all five LED pins as outputs:
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(bodyLEDpin, OUTPUT);
// TODO clean up debounce duplication here
// initialize the pushbutton pin as an input:
button.attach( buttonPin , INPUT_PULLUP ); // Attach the debouncer to a pin with INPUT_PULLUP mode
button.interval(5); // Use a debounce interval of 25 milliseconds
button.setPressedState(HIGH);
}
int ledSequence = 1;
bool isLedCharging(bool LEDcharging);
bool isLedCharged(int LEDbrightness, bool LEDcharged);
bool chargeTheLed(int led, int brightness);
void flashBodyLEDs(int pin, int ledState);
int stepDelay = 30;
int turboModeDuration = 10000;
int ledState = LOW;
const int piezoPin = 2;
void incrementLedBrightness(int led, int brightness) {
// in here we are only going to ACT on the led using the brightness
//Serial.println((String)"LED:"+led+" Incrementing brightness to: "+brightness);
//brightness = constrain(brightness, 0, 255); // both resetting and constraining the value so we don't overflow the brightness setting
tone(piezoPin, brightness, 50);
analogWrite(led, brightness); // what actually changes the LED brightness; i think this has to do with how we declared the pins
//brightness = 0;
return 0;
}
bool isLedCharged (int brightness, bool LEDcharged) {
bool skipThisLed = false;
//brightness = 0;
//brightness = LEDbrightness;
Serial.println((String)" Brightness is: " + brightness);
if (brightness == 255) {
skipThisLed = true;
//brightness = 0; // this must be reset for the next led
//LEDbrightness = 0;
Serial.println((String)"Brightness is 255!!!!!! Setting skipThisLed ("+skipThisLed+") to true.");
} else {
Serial.println((String)"Brightness is not 255. Setting skipThisLed ("+skipThisLed+") to LEDcharged ("+LEDcharged+")");
skipThisLed = LEDcharged;
}
//Serial.println((String)" skipThisLed: " + skipThisLed);
return skipThisLed;
}
// our function to modify the LED state during the loop
// we pass in the led (pin), the brightness var (which increases each loop), the LED's sequence number, and whether or not it is 'charged'
void setBrightness(int led, int brightness, int countLED, bool LEDcharging, bool LEDcharged) {
if (!LEDcharging && LEDcharged) {
Serial.println((String)"LED:"+led+" not charging, charged; skip this led.");
//brightness = 0;
return 0;
} else if (LEDcharging && LEDcharged) {
Serial.println((String)"LED:"+led+" charging and charged.");
//brightness = 0;
return 0;
} else if (LEDcharging && !LEDcharged) {
Serial.println((String)"LED:"+led+" charging but not charged; incrementing by " + brightness);
incrementLedBrightness(led, brightness);
return 0;
} else if (!LEDcharging && !LEDcharged) {
Serial.println((String)"LED:"+led+" not charging, not charged.");
// TODO this will be where we deal with 'which one is next'
return 0;
} else {
Serial.println((String)"LED:"+led+" EXCEPTION!!! something went wrong...");
return 1;
}
}
void ledFlasher(int pin, int flasherDelay) {
digitalWrite(pin, HIGH);
delay(flasherDelay);
digitalWrite(pin, LOW);
delay(flasherDelay);
return;
}
void flashBodyLEDs(int pin, int flasherDelay, int flashDurationCount) {
int i = 0;
int piezoTone = 100;
while (i < flashDurationCount) {
Serial.println((String)"Flashing body LEDs...");
ledFlasher(pin, flasherDelay);
piezoTone = piezoTone + (flashDurationCount / 2);
tone(piezoPin, piezoTone, 500);
piezoTone = piezoTone + (flashDurationCount / 1.5);
tone(piezoPin, piezoTone, 500);
i++;
}
return;
}
// A void function performs a task, and then control returns back to the caller--but, it does not return a value.
void loop() {
// TODO i think we have some duplication from introducing debounce
button.update(); // read the state of the pushbutton value
int buttonValue = button.read();
buttonState = digitalRead(buttonPin); // HIGH = button is pushed
if (buttonValue == LOW ) {
counter = counter + countModifier; // something is up here...
if (firstRun) {
Serial.println((String)"First run, setting counter (" + counter + ") to 0.");
counter = 0;
firstRun = false;
}
counter = constrain(counter, 0, 255); // never let this hit more than 255
// TODO a lot of loops we can do, multidimensional arrays
// set each LED's brightness var to 'counter' (i.e. a number that is incremented every loop)
// we pass in the led pin, brightness var, led position, and charged state bool
if (ledSequence == 1) {
Serial.println((String)"LED sequence = "+ ledSequence);
if (LED1charged) {
Serial.println((String)"LED:"+LED1+" is charged, we can move on.");
delay(stepDelay);
} else {
LED1charging = true;
brightnessLED1 = counter;
Serial.println((String)"LED brightness = "+ brightnessLED1);
Serial.println((String)"LED:"+LED1+" is not charged, we must charge it.");
setBrightness(LED1, brightnessLED1, countLED1, LED1charging, LED1charged);
LED1charged = isLedCharged(brightnessLED1, LED1charged);
if (LED1charged) {
Serial.println((String)"LED:"+LED1+" has been marked as charged, performing one-time increment function of ledSequence which is currently: " + ledSequence );
ledSequence++;
LED2charging = true; // good, this helps, but brightness still not set correctly.
counter = 0;
}
delay(stepDelay);
}
}
if (ledSequence == 2) {
Serial.println((String)"LED sequence = "+ ledSequence);
if (LED2charged) {
Serial.println((String)"LED:"+LED2+" is charged, we can move on.");
delay(stepDelay);
} else {
brightnessLED2 = counter;
Serial.println((String)"LED brightness = "+ brightnessLED2);
Serial.println((String)"LED:"+LED2+" is not charged, we must charge it.");
setBrightness(LED2, brightnessLED2, countLED2, LED2charging, LED2charged);
LED2charged = isLedCharged(brightnessLED2, LED2charged);
//Serial.println((String)"LED:"+LED2+" is not charged, we must charge it.");
if (LED2charged) {
Serial.println((String)"LED:"+LED2+" has been marked as charged, performing one-time increment function of ledSequence which is currently: " + ledSequence );
ledSequence++;
LED3charging = true; // good, this helps, but brightness still not set correctly.
counter = 0;
}
delay(stepDelay);
}
}
if (ledSequence == 3) {
Serial.println((String)"LED sequence = "+ ledSequence);
if (LED3charged) {
Serial.println((String)"LED:"+LED3+" is charged, we can move on.");
delay(stepDelay);
} else {
brightnessLED3 = counter;
Serial.println((String)"LED brightness = "+ brightnessLED3);
Serial.println((String)"LED:"+LED3+" is not charged, we must charge it.");
setBrightness(LED3, brightnessLED3, countLED3, LED3charging, LED3charged);
LED3charged = isLedCharged(brightnessLED3, LED3charged);
//Serial.println((String)"LED:"+LED3+" is not charged, we must charge it.");
if (LED3charged) {
Serial.println((String)"LED:"+LED3+" has been marked as charged, performing one-time increment function of ledSequence which is currently: " + ledSequence );
ledSequence++;
LED4charging = true; // good, this helps, but brightness still not set correctly.
counter = 0;
}
delay(stepDelay);
}
}
delay(stepDelay);
if (ledSequence == 4) {
Serial.println((String)"LED sequence = "+ ledSequence);
if (LED4charged) {
Serial.println((String)"LED:"+LED4+" is charged, we can move on.");
delay(stepDelay);
} else {
brightnessLED4 = counter;
Serial.println((String)"LED brightness = "+ brightnessLED4);
Serial.println((String)"LED:"+LED4+" is not charged, we must charge it.");
setBrightness(LED4, brightnessLED4, countLED4, LED4charging, LED4charged);
LED4charged = isLedCharged(brightnessLED4, LED4charged);
//Serial.println((String)"LED:"+LED4+" is not charged, we must charge it.");
if (LED4charged) {
Serial.println((String)"LED:"+LED4+" has been marked as charged, performing one-time increment function of ledSequence which is currently: " + ledSequence );
ledSequence++;
LED5charging = true; // good, this helps, but brightness still not set correctly.
counter = 0;
}
delay(stepDelay);
}
}
delay(stepDelay);
if (ledSequence == 5) {
Serial.println((String)"LED sequence = "+ ledSequence);
if (LED5charged) {
Serial.println((String)"LED:"+LED5+" is charged, we can move on.");
delay(stepDelay);
} else {
brightnessLED5 = counter;
Serial.println((String)"LED brightness = "+ brightnessLED5);
Serial.println((String)"LED:"+LED5+" is not charged, we must charge it.");
setBrightness(LED5, brightnessLED5, countLED5, LED5charging, LED5charged);
LED5charged = isLedCharged(brightnessLED5, LED5charged);
//Serial.println((String)"LED:"+LED5+" is not charged, we must charge it.");
if (LED5charged) {
Serial.println((String)"LED:"+LED5+" has been marked as charged, performing one-time increment function of ledSequence which is currently: " + ledSequence );
ledSequence++;
counter = 0;
}
delay(stepDelay);
}
}
delay(stepDelay);
if (ledSequence == 6) {
Serial.println((String)"LED sequence = "+ ledSequence);
tone(piezoPin, 200, 500);
Serial.println((String)"Flashing body LEDs now...");
flashBodyLEDs(bodyLEDpin, flasherDelay, flashDurationCount);
Serial.println((String)"Done flashing body LEDs...");
ledSequence++;
return;
}
return;
} else {
// turn LED off:
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(bodyLEDpin, LOW);
// reset the counter
counter = 0;
ledSequence = 1;
LED1charged = false;
LED2charged = false;
LED3charged = false;
LED4charged = false;
LED5charged = false;
LED1charging = true;
LED2charging = false;
LED3charging = false;
LED4charging = false;
LED5charging = false;
return;
}
// end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment