Starship Arduino Code (Down and dirty but it works)

jamstraz

Active Member
Button 1 control a torpedo fire: Brighteness ramps up, flashes and then goes off
Button 2 controls a start up sequence: New LEDs can be added (albeit rather sloppily) with new integers. I'm sure there is a better way but, only a week.
Button 3 controls a "Damage" routine causing all pins to flicker randomly
There is a blinker involved here too that blinks fast 2x and slower 2x

xl97 code is still in here

Code:
//Starship Base Code
//Based on a myriad of sources, I take very little credit
//Ver 2.0b 12 April 2020
//Ver 3.0a 18 April 2020
//Torpedo pin numbers
const int buttonPin = 2; //One press to brighten, flash and turn off
//Power up sequence pin numbers
const int buttonPin2 = 4; // Press to power up the model
//Damage flicker button
const int buttonPin3 = 7; //random light flickers
const int threshold = 255;
unsigned long interval = 500;
unsigned long previousMillis = 0;
const unsigned long intervalSlow = 250;
const unsigned long intervalFast = 750;

//Set up variables for Torpedo
int buttonState = 0; // current state of the 1st Button
int led = 9;                 // PWM for Torpedo
//Set up variables for startup sequence
int buttonState2 = 0; // current state of 2nd button
int led2 = 10; //PWM for interior lights
int led3 = 11; //PWM for engines
int fadeThreshold = 100; //127 originally
int fadeSpeed = 10;
//Setup variables for damage
int buttonState3 = 0; //PWM for damage
int led4 = 6;
int ledState = 150; //LOW
int flashstate = 255; //HIGH
int counter = 0;
int counterbright = 0;
void setup() {
// initialize the button pin as a input:
//pinMode(buttonPin, INPUT);
//digitalWrite(buttonPin, HIGH);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP); //added 4/18/2020
  // initialize the LED as an output:
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT); //added 4-12-2020
pinMode(led3, OUTPUT); //added 4/18/2020
  pinMode(led4, OUTPUT);
  // initialize serial communication:
Serial.begin(9600);
counter = 0;
counterbright = 0;
interval = intervalSlow;
Serial.println(counter);
  Serial.println(interval);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

//blink routine
  unsigned long currentMillis = millis();
Serial.println(counter);
delay(100);
//analogWrite(led2, 255);
  //analogWrite(led3, 255);
  if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
    previousMillis = currentMillis;
    // if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
counter ++;
}
digitalWrite(led4, ledState);
  }
  if (counter == 2) {
//reset counter and swap interval
counter = 0;
if ( interval == intervalSlow) {
interval = intervalFast;
}
else {
      interval = intervalSlow;
    }
  }
  // if the state has changed, increment the counter

//BUTTONS START HERE
if (buttonState == HIGH) {
// if the current state is LOW then the button has been pressed
    Serial.println("pressed");
    for (int i = 0; i <= fadeThreshold; i++) {
//update led with new analog value (brightness)
analogWrite(led, i);
delay(fadeSpeed);
    }
    //jump to 255 (fully on)
    analogWrite(led, 255);
  } else {
analogWrite(led, 0);
//do nothing
  }
  // Delay a little bit to avoid bouncing
  delay(100);
  if (buttonState2 == HIGH) {
analogWrite (led3, 0);
analogWrite (led2, 0);
delay (500);
    // if the current state is LOW then the button has been pressed
    for (int j = 0; j <= fadeThreshold; j++) {
//update led with new analog value (brightness)
analogWrite(led2, j);
delay(fadeSpeed);
}
for (int l = 0; l <= fadeThreshold; l++) {
analogWrite(led3, l);
delay(fadeSpeed);
}
analogWrite(led4, HIGH);
delay(fadeSpeed);
  }

if (buttonState3 == HIGH) {
// if the current state is LOW then the button has been pressed
    Serial.println("pressed 3");
    for (int k = 0; k <= 100; k++) {
Serial.println(k);
analogWrite(led2, random(0, 400));
delay(random(50));
analogWrite(led3, random(0, 400));
delay(random(50));
analogWrite(led4, random(0, 400));
delay(random(50));
if ( k == 50) {
analogWrite (led2, HIGH);
        analogWrite (led3, HIGH);
      }
}
}
}

I'm sure there is a better way to do this but it works....
 
Last edited:
1587258796624.png
 
Back
Top