Welcome, %1$s. Please login or register. - Did you miss your activation email?

Login with username, password and session length
 

Author Topic: My Gundam Code  (Read 248 times)

Offline texastony1946

  • Lieutenant
  • **
  • Posts: 30
My Gundam Code
« on: March 13, 2018, 02:40:21 pm »
This is my code for my Sinanju Stein gundam. Thanks everyone ( especially Spencer) for help on different pieces of code  I used. There are some issues with the sequence, but overall it works. There is something blocking the proper functioning of the thrusters code (it may be I just don't understand how it works).  Feel free to use code as needed, just be sure to share it with our forum members here.
Start sequence
  Base display on then off
  Reactor on, eyes on and reactor in chest on.
  Thrusters on
  Engine ramp to full power.
  Weapon system on, sword and shield light. Shield didn't light in video- may be wiring.
Shutdown sequence
  Weapon systems off
  Engines off
  Reactor off.
Video
  https://www.youtube.com/watch?v=G41yCKMwHkE

Test Code
  #include <elapsedMillis.h>
elapsedMillis timeElapsed;
int lastButtonState = 0;
int buttonState = 0;     // Set all initial states to zero
int Counter = 0;
long currentMillis = 0;
long previousMillis = 0;
float EngineFlicker = 0;
int RandNumber = 0;
int Flicker = 0;
int WeaponState = 0;
int buttonPin = 2;
int Weapons = 3 ;
int Display = 9;         // Identify all LED pin numbers
int Reactor = 10;
int Engine =  11;
int Thruster1 = 4;
int Thruster2 = 5;
int Thruster3 = 6;
int Thruster4 = 7;
int Thruster5 = 8;
unsigned long  Thruster1On = 500;                     
unsigned long  Thruster1Off = 1000;                   
unsigned long  Thruster2On = 750;         
unsigned long  Thruster2Off = 2000;
unsigned long  Thruster3On = 600;
unsigned long  Thruster3Off = 4000;
unsigned long  Thruster4On = 500;
unsigned long  Thruster4Off = 3000;
unsigned long  Thruster5On  = 200;
unsigned long  Thruster5Off = 5000;
unsigned long  ThrusterOff = 0;
unsigned long Thruster1Millis;
unsigned long Thruster2Millis;
unsigned long Thruster3Millis;
unsigned long Thruster4Millis;
unsigned long Thruster5Millis;
boolean Thruster1State = 0;
boolean Thruster2State = 0;
boolean Thruster3State = 0;
boolean Thruster4State = 0;
boolean Thruster5State = 0;
long interval1 = 10;
long interval2 = 25;
long interval3 = 50;
long interval4 = 100;     // Various time intervals
long interval5 = 250;
long interval6 = 500;
long interval7 = 1000;
long interval8 = 2500;
int brightness1 = 0;
int brightness2 = 0;
float brightness3 = 0;      // Various brightness and fade amounts
int brightness4 = 0;
int fadeAmount1 = 1;
int fadeAmount2 = 5;

void setup() {
pinMode(buttonPin,INPUT_PULLUP);
pinMode(Display, OUTPUT);
pinMode(Reactor, OUTPUT);
pinMode(Engine,  OUTPUT);
pinMode(Weapons, OUTPUT);
pinMode(Thruster1, OUTPUT);  // Identify LED outputs
pinMode(Thruster2, OUTPUT);
pinMode(Thruster3, OUTPUT);
pinMode(Thruster4, OUTPUT);
pinMode(Thruster5, OUTPUT);


 Serial.begin(9600);
}

void loop()
//PUSH BUTTON SEQUNCER
//===========================================================================================
{
  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState && timeElapsed > interval6 )
        {
    // if the state has changed, increment the counter                   // Push button sequencer
    if (buttonState == LOW)
        {
      Counter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(Counter);
        }
    else
    {
       Serial.println("off");
    }
    timeElapsed = 0;
   
    currentMillis = millis();
 if(Counter >=11)
    Counter = -1;                                                                                                 // Reset button state
    lastButtonState = buttonState;
        }




  //            Base SpotLight
//======================================================================================================== 
 
   //SPOTLIGHT CODE HERE
 long  currentMillis = millis();
  if (Counter ==1   && brightness1  < 255 && currentMillis - previousMillis > interval4)
       {
  brightness1 = brightness1 + fadeAmount2 ;
  analogWrite(Display, brightness1);                                                            // Search ramp up and ramp down
  previousMillis = currentMillis;
 
 
 //    Serial.println("DisplayMilsUp");
 //    Serial.println(brightness1);
   
        }
 
  if (Counter >=2   && brightness1  > 0 && currentMillis - previousMillis > interval1)
  {
  brightness1 = brightness1 - fadeAmount2 ;
  analogWrite(Display, brightness1);
  previousMillis = currentMillis;
// Serial.println("DisplayMilsDn");
// Serial.println(brightness1);
 timeElapsed = 0;
 
 
  }

 

//              REACTOR
//========================================================================================================

 // REACTOR CODE HERE
  currentMillis = millis();
 
  if (Counter == 3 && brightness2  != 255 && currentMillis - previousMillis > interval3)
  {
  brightness2 = brightness2 + fadeAmount2 ;
   analogWrite(Reactor, brightness2*random(2));
   Serial.println("ReactMilsUp");                                                                    //  Reactor power ramp up and shutdown
   Serial.println(brightness2);
   
   previousMillis = currentMillis;
    currentMillis = millis();
 
  if (Counter == 3 && brightness2  == 255 && currentMillis - previousMillis > interval4)
  {
  brightness2 = brightness2 + fadeAmount2 ;
   analogWrite(Reactor, 255);
   Serial.println("ReactMilsUp");                                                                    //  Reactor power ramp up and shutdown
   Serial.println(brightness2);
   
   previousMillis = currentMillis;
 
  }
  }


 
  currentMillis = millis();
 
  if (Counter >= 9 && brightness2  > 0 && currentMillis - previousMillis > interval2)
  {
  brightness2 = brightness2 - fadeAmount2 ;
   analogWrite(Reactor, brightness2);
//   Serial.println("ReactMilsDn");
//   Serial.println(brightness2);
   
   previousMillis = currentMillis;
  }

 //========================================================================================================== 
 //              THRUSTERS
//========================================================================================================== 
// THRUSTER CODE HERE
 currentMillis = millis();

  if (Counter ==4  && currentMillis - previousMillis > interval4 )
 
  {
 
 
  if (currentMillis - Thruster1Millis > (Thruster1State ? Thruster1On:Thruster1Off)){   // using a conditional operator
    digitalWrite(Thruster1, Thruster1State = !Thruster1State);    // Flip the state and output it
    Thruster1Millis = currentMillis;                           // reset the blinker time
   }

   
 
 
  if (currentMillis - Thruster2Millis > (Thruster2State ? Thruster2On:Thruster2Off)){   // using a conditional operator
    digitalWrite(Thruster2, Thruster2State = !Thruster2State);    // Flip the state and output it
    Thruster2Millis = currentMillis;                           // reset the blinker time
   }
     
 
 
  if (currentMillis - Thruster3Millis > (Thruster3State ? Thruster3On:Thruster3Off)){   // using a conditional operator
    digitalWrite(Thruster3, Thruster3State = !Thruster3State);    // Flip the state and output it
    Thruster3Millis = currentMillis;                           // reset the blinker time
   }
     
 
 
  if (currentMillis - Thruster4Millis > (Thruster4State ? Thruster4On:Thruster4Off)){   // using a conditional operator
    digitalWrite(Thruster4, Thruster4State = !Thruster4State);    // Flip the state and output it
    Thruster4Millis = currentMillis;                           // reset the blinker time
   }
     
 
 
 
  if (currentMillis - Thruster5Millis > (Thruster5State ? Thruster5On:Thruster5Off)){   // using a conditional operator
    digitalWrite(Thruster5, Thruster5State = !Thruster5State);    // Flip the state and output it
    Thruster5Millis = currentMillis;                           // reset the blinker time
   }
 
  }





//              ENGINE
//==========================================================================================================

// ENGINE CODE HERE
  currentMillis = millis();
   
  if (Counter ==5 && brightness3  != 255 && currentMillis - previousMillis > interval3)
  {   
 
  brightness3 = (brightness3 + fadeAmount1)  ;
 
  analogWrite(Engine, brightness3*random(2) ) ;
   
   Serial.println("EngMilsUp");
   Serial.println(brightness3);                                                             // Engine ramp up and shutdown
   
   previousMillis = currentMillis;
 
  }
    if (Counter >=5 && brightness3  == 255 && currentMillis - previousMillis > interval3)
  {   
  analogWrite(Engine, brightness3*random(2) ) ;
   
                                                              // Engine full power flicker
  previousMillis = currentMillis;
 
  }
   currentMillis = millis();
   
  if (Counter >=8 && brightness3  >0 && currentMillis - previousMillis > interval2)
  {
  brightness3 = brightness3 - fadeAmount2 ;
   analogWrite(Engine, brightness3);
//   Serial.println("EngMilsDn");
//   Serial.println(brightness3);
   
    previousMillis = currentMillis;
  }


 


 


//              WEAPONS
//==========================================================================================================

     //WEAPON CODE
  currentMillis = millis();
 
  if (Counter ==6 && currentMillis - previousMillis > interval3)
       {
         
 
      if (WeaponState == 0) {
      WeaponState = 1;
    } else {
      WeaponState = 0;
    }                                             // Weapons on and off
    digitalWrite(Weapons, WeaponState);
     Serial.println("Weapons");
     Serial.println(WeaponState);
   
  previousMillis = currentMillis;
 
 
   
   
        }
  currentMillis = millis();
  if (Counter >=7 && currentMillis - previousMillis > interval3)
  {
   
  digitalWrite(Weapons,0);
 
   previousMillis = currentMillis;
  }



//========================================================================================================== 
   { 
   if(Counter >=11)
    Counter = -1;                                                                                                 // Reset button state
    lastButtonState = buttonState;
   } currentMillis = millis();
  if (Counter >=7 && currentMillis - previousMillis > interval4)
  {
   
  analogWrite(Weapons,0);
 
   previousMillis = currentMillis;
  }

}

//========================================================================================================== 
 
Thanks for viewing. All comments are welcome.



Offline MarkW

  • Plastic Surgeon
  • Admiral
  • *****
  • Posts: 1347
    • MW ModelWerks
Re: My Gundam Code
« Reply #1 on: March 18, 2018, 07:26:45 am »
Very cool!  I'd love to see how all the LEDs and wiring were worked in the actual model.  Do you still have access to the Arduino?  Can you still fiddle with the program?
MarkW
"I can cross stab Hitler to death off the list of cool crap I thought I'd never do"
--Brock Sampson
NEW WEB ADDRESS

Offline texastony1946

  • Lieutenant
  • **
  • Posts: 30
Re: My Gundam Code
« Reply #2 on: March 18, 2018, 10:05:28 am »
Thanks for the comments MarkW. Yes I can still program the Gundam through the base. I am going to post a build review on Scale Model Addict which has pictures of the Arduino installation. I will try to post a schematic of the wiring also. I attached a photo of the wiring in the basr. Also next time I am going to use an ATTiny 85 or ATTiny84 which will eliminate the external wiring with the exception of the power wires. Thanks again for comments.



« Last Edit: March 18, 2018, 05:18:54 pm by texastony1946 »

Offline MarkW

  • Plastic Surgeon
  • Admiral
  • *****
  • Posts: 1347
    • MW ModelWerks
Re: My Gundam Code
« Reply #3 on: March 18, 2018, 12:59:08 pm »
I'd very much like to see that.  The AT Tiny solution would be really cool as well.  I'm assuming you'd be combining the thrusters since you have far fewer pins to play with?  It looks like you are using 3 PWM?
MarkW
"I can cross stab Hitler to death off the list of cool crap I thought I'd never do"
--Brock Sampson
NEW WEB ADDRESS

Offline texastony1946

  • Lieutenant
  • **
  • Posts: 30
Re: My Gundam Code
« Reply #4 on: March 18, 2018, 02:20:50 pm »
Yes I would have to use  several of the ATTiny85 . I could sync with the sequencer switch going to multiple devices. The ATTiny84 could handle most of the I/O. I have loaded the code into both devices and it runs. You have to change I/O numbers to support the chip. The chips internal clock is running at 8MHZ, not sure if will be overloaded. Thanks for the comments.

 




Enterprise © Bloc