Coding trouble for a Neewbie

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
KrynnDragon
 
Posts: 1
Joined: Wed Jul 11, 2012 10:25 pm

Coding trouble for a Neewbie

Post by KrynnDragon »

I am trying to light one of my Model Spacecraft, I have the flickering lights set up and running, the constant lights for the bridges.
I am having trouble with the front gun light. The begining of my code works for the Engine flicker, and the bridge lights.
when i tried to add code for the pull down switch to detect firing of the gun, everything goes wrong.
the gun lights, then goes thru its delay, interrupting the engine flicker, and they just bounce back and forth.
the gun lights, delay, the engine led's flicker once, then back to the gun light again.
Im a bit lost and still learning, any help would be appreciated.
Thanks

Code: Select all

/*
   Argo Lights 
   Bridge - Green X2 on Pin 4
   Engines- Red   X3 on Pin 5
   Gun    - White X1 on Pin 3
 */
int gunled = 3; // Gun Led Connected to pin 3
int guntrig = 7; // Gun switch connected to pin 7

void setup() {                
  // initialize the digital pin as an output or input
  pinMode(7, INPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  
}

 void loop(){
  digitalWrite(5, HIGH);   // set the LED on
  delay(50);              // wait
  digitalWrite(5, LOW);    // set the LED off  Flicker for engine lights
  delay(20);              // wait
  digitalWrite(4, HIGH);  // Bridge lights always on
  digitalRead(guntrig);   // Read trigger button for fire sequence
  if (guntrig == HIGH);
  analogWrite(gunled, 255);
  delay(2000);
  for(int fade = 255; fade>= 0; fade -=5)
  analogWrite(gunled, fade);
  delay(30);
  
  }

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Coding trouble for a Neewbie

Post by adafruit_support_bill »

A couple things:
First. Do you have a pull-down resistor on the button that fires the gun? If not it is likely to fire randomly.
Second. You don't want to use delay() if you are trying to animate multiple effects simultaneously. As you have discovered, everything stops when you call delay(). Instead, you need to design your program around timers. Check out the SImpleTimer library over at http://arduino.cc/playground/Main/GeneralCodeLibrary

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”