Jump to content


Papkee

Member Since 09 Mar 2012
Offline Last Active Jun 02 2014 08:23 PM

Posts I've Made

In Topic: [WIP] Stampede ACB, the Arduino-powered Stampede ECS

12 August 2013 - 09:00 AM

Ok, here is the code for my pair of relay-controlled stampedes.
I'll apologize now because it is both horribly commented and way more than you need for what you want to do.
This project had an LCD display that showed ammo for both stampedes, had configurable burst fire, semi, and fully automatic fire modes, and maintained preferences when powering on and off.
here is the bit that managed the relay and stampede fire cycle switch.

void pullTrigger(int barrel)
{//0 neither, 1 upper, 2 lower
  if(barrel == 1)
  {
    while(digitalRead(UpperSensor) == HIGH)
    {//check and see if the firing cycle has taken over
      Serial.println("firing interval");
      digitalWrite(RelayUpper, LOW);//turn the relay on 
      delay(FIRING_INTERVAL);//wait a little bit
      digitalWrite(RelayUpper, HIGH);//turn the relay off
    }
  }
  else if(barrel == 2)
  {
    while(digitalRead(LowerSensor) == HIGH)
    {//check and see if the firing cycle has taken over
      Serial.println("firing interval");
      digitalWrite(RelayLower, LOW);//turn the relay on 
      delay(FIRING_INTERVAL);//wait a little bit
      digitalWrite(RelayLower, HIGH);//turn the relay off
    }    
  }
}

Like i said, i had two stampedes, but there you can see how it handled firing via relay.


The only issue I have with that is that I would like my LED to be flashing even when firing. A delay wouldn't allow that. Now, adding a whole timing function is a lot more work, but I'd really rather not have the whole code stop for firing anyway.

In Topic: [WIP] Stampede ACB, the Arduino-powered Stampede ECS

07 August 2013 - 04:14 PM

Alright, this project is still in business. It was a mistake to order a $5 soldering iron as the tip died after two days. I'm still waiting on batteries and a few other small parts, and I'm probably going to recode the software entirely.

@Zorns Lemma: I think I do. Is it because the function only runs when the led is off and therefore can never turn it on.

Progress is slowly being made (mostly because I put the gun in my closet and forgot about it) so expect to hear from me soon with new updates.

In Topic: [WIP] Stampede ACB, the Arduino-powered Stampede ECS

26 June 2013 - 10:38 AM

Here's my version, with different variable names to make things unambiguous and easier for myself:

// SOMEWHERE OUTSIDE loop()
  LED_is_on = (digitalRead(led) == HIGH);   // reads the current LED status
                                            // might be unnecessary if the LED always starts off
// IN loop()
  // MAIN VARIABLE HANDLERS
  ledstate = safetyOn; // I'm not sure why you have an ledstate variable if it just always
                        // takes on the value of safetyOn but sure w/e
  if (safetyOn) { // 0 is false, nonzero is true, so if(x == 0) is redudant
    digitalWrite(relay, LOW);
  }
  else {
    digitalWrite(relay, HIGH);
  }
  
  // LED FLASH CODE
  if (!ledstate) { //Remember, if(x) is same as if(x<>0), if(!x) is same as if(x==0)
	now = millis();
    if (LED_is_on && (now - then) > LED_blinkFor) {
        then = now;
        LED_is_on = 0;
        digitalWrite(led, LOW);
    } else if (!LED_is_on && (now - then) > LED_nextBlink) {
        then = now;
        LED_is_on = 1;
        digitalWrite(led, HIGH);
    }
}


Edit: My point about the LED toggle not functioning properly may be invalid if hardware limitations and code execution delay means that you will actually cover 10 ms of real-time while executing within one conditional. So your original code might work, but relying on hardware limitation for software functionality is pretty poor practice.


What I'm doing is setting small timers to avoid using the delay() function and stopping everything. It's starting a timer with millis(), then using the duration variable to compare to the elapsed time. Although I do see the memory issue; it would make more sense to reset the timer to 0 every time.


For what he's doing it shouldn't be a problem. He's not using the delay function anywhere, and there's nothing going on inside the loop that would hold things up. It wouldn't be a terrible idea, but if he's depending on us to debug is code without interrupts, he's going to have a hard time using them.

Edit: Incidentally, Work in Progress threads aren't allowed here. This sort of thing shouldn't really be posted until it's done. However, we're usually cool with threads asking for help if you are clear and provide photos and enough info to go on, so for now, I'm handling this as a help thread.


You guys have given me plenty of help in this thread so technically it IS a help thread.

In Topic: [WIP] Stampede ACB, the Arduino-powered Stampede ECS

22 June 2013 - 01:13 PM

Also, here's the rough code I've written so far.

const int button = 1;
const int led = 3;
const int relay = 5;
int safety = 1;
int ledstate = 1; // LED is solid, safety on
int leddelay = 1000; // Time between LED blinks
int ledon = 10; //Time the LED is on
long previousMillis = 0;

void setup() {
  pinMode(button, INPUT); // Just a button
  pinMode(led, OUTPUT); // Just an LED
  pinMode(relay, OUTPUT); // Output to relay for safety switch
  digitalWrite(button, HIGH); // Use internal pullup resistor
  safety = 1; // Turn safety on
}

void loop() {
  
  // BUTTON CODE
  if (digitalRead(button) == LOW) {
    if (safety == 1) {
      safety = 0;
    }
    else {
      safety = 1;
    }
  }
  
  // MAIN VARIABLE HANDLERS
  if (safety == 0) { // If the safety is off
    ledstate = 0; // Blink the LED
    digitalWrite(relay, HIGH); // Close the Relay, safety is officially off
  }
  else {
    ledstate = 1; // LED is solid
    digitalWrite(relay, LOW); // Open the relay, safety is on
  }
  
  // LED FLASH CODE
  if (ledstate == 0) {
    unsigned long currentMillis = millis();
    if(currentMillis - previousMillis > leddelay) {
      // Reset the timer
      previousMillis = currentMillis;
      
      // Turn on the LED
      digitalWrite(led, HIGH);
      
      // Keep the LED on until the ledon time is over
      if(currentMillis - previousMillis > ledon) {
        
        // Reset the timer again
        previousMillis = currentMillis;
        
        // Turn the LED off
        digitalWrite(led, LOW);
      }
    }
  }
}

For those of you who are programmers, anything seem wrong here? It compiles okay.

In Topic: [WIP] Stampede ACB, the Arduino-powered Stampede ECS

22 June 2013 - 12:00 PM

I've already ordered the relay. It's specially-made for the arduino so I'm good there. I was thinking if I soldered two wires to the motor leads and wired them as a sensor (of course limiting the current to acoid frying the arduino), then the arduino would be able to sense whenever the motor is activated. Then it could open and close the relay according to how many shots need to be fired. (boy, this code is adding up fast)

I'm guessing this will all be trial and error on my part. I'm probably going to try all of your methods and see which one works best.