Output happening when it's not supposed to - variable scope problem?

Edit – I’m adding the code into this post. Initially it was on pastebin.

This is my first program that I’ve written from scratch, so I apologize if it’s coded poorly or hard to read.

I’m using an Arduino Uno.

I’m working on a circuit that will have an RF receiver and a 4-button remote control. It’s to control a metal sculpture of a dragon that will have glowing eyes and breathe fire. When the “A” button on the remote is pressed, the Arduino will trigger the fire sequence.

There are 3 outputs that I’m using:
a) LEDs in the eyes.
b) Electric sparking igniter
c) Solenoid valve that releases propane

Fire sequence when the A button is pressed is supposed to go like this:

  1. All outputs are shut off
  2. Sparking igniter turns on
  3. The LEDs in the eyes gradually increase in brightness
  4. Once at full brightness, the propane is released and the dragon builds fire

I built a test circuit for dubugging that instead of using actual solenoids or igniters, I just use LEDs instead.

What I find is that when I turn the arduino on, the first time I do a “fire sequence” it works perfectly.

The second time I do it, this is what happens:

  1. All outputs are shut off (OK)
  2. Sparking igniter turns on (OK)
  3. The LEDs in the eyes flash on and off a few times very quickly, then gradually increase in brightness the way they are supposed to (problem)
  4. Once at full brightness, the propane is released and the dragon builds fire (OK)

Here’s a link to the code..

The fire sequence function starts at line 181. The behavior suggests to me that the x variable in the for loop in that function persists even after the fireSequence function executes, the program goes to loop, and then returns for the second time I call up the fire sequence. I had a friend look at the program and he thinks that is not the problem, but he hasn’t done a lot of programming for a few years and never used and Arduino, so I thought I’d check if anyone here had an idea.

Can anyone guess why the “LEDpin” output may be going high when it’s not supposed to?

Thank you!

dragon_fire.ino (6.76 KB)

if (digitalRead (cButt)==HIGH)
        throbTheEyes;

Oops.

TO DO:
c) Put () at each function call.

if (digitalRead(dButt)==HIGH)
        blinkEyes;

And again.

for (int x=0; x<255; x++){    //makes the eyes gradually glow brighter takes ~5 seconds
        if (x>255) {x=255;}         //Max analogWrite is 255. Resets variable to avoid overflow.

Don’t you trust your for loop?

Here's a link to the code..

AWOL's braver than me. You can attach your code to your post, using the Additional Options.. link just below the post/reply text entry box.

AWOL:
Oops.

TO DO:
c) Put () at each function call.

I fixed those. Thanks.

AWOL:

for (int x=0; x<255; x++){    //makes the eyes gradually glow brighter takes ~5 seconds

if (x>255) {x=255;}         //Max analogWrite is 255. Resets variable to avoid overflow.




Don't you trust your for loop?

I don’t trust anything that I’ve programmed myself. LOL. My friend said the same thing. about that line. I took it out.

The program is still flickering the “LEDpin” output a few times when I trigger the “fireSequence” function. It happens just before the LED starts gradually growing brighter. Again, it seems like there’s still a value of 255 in there or something. It happens so quickly that it’s hard to tell. I’ll post the code in a separate post below so you don’t need to visit pastebin.

void checkInputs()  //Function for checking inputs during an active sequence.
{
  if (digitalRead (aButt)==HIGH)
    fireSequence();
    
  if (digitalRead(bButt)==HIGH){
    idleBeast();
    loop();   // <---------- NOOOOOOOOOOOOOOOOOOO!
  }                           
  
  if (digitalRead (cButt)==HIGH)
    throbTheEyes();
    
}

http://www.gammon.com.au/forum/?id=12153#trap21

Nice spot, Nick.