Code problem with digitalWrite(HIGH) staying HIGH

i`m having difficulty with a digitalWrite going HIGH and then turning immediately to LOW. should this be happening....is this how digitalWrite works?

let me put it this way here`s the flow of the program // while z > 500

if a > b then do this (digital pin 1, HIGH)

else if c > d then do this (digital pin 2, HIGH) (delay 150) (digital pin 2, LOW) (digital pin 1, LOW) //

the question is, (assuming z > 500) digital pin 1 goes HIGH when a > b BUT why is digital pin 1 going LOW when c is not greater than d.

thats as simple as i can get the question. i hope someone can answer it. i would love to hear something akin to "it shouldnt be going LOW when c is not greater than d" or "that is the behavior we expect from digital pin 1 -- to go LOW when it is not being told to do so because .. . . . "

The IDE has a handy code formatting tool, accessed by pressing Ctrl-T. It usually improves indentation, making code more legible.

int firefunhouse = 8;
int firemillion = 9;
int openmillion = 11;
int openfunhouse = 12;
int pleasefirefunhouse = 0;
int ballinmillion = 1;
int ballinfunhouse = 2;
int cpufiredmillion = 3;
int cpufiredfunhouse = 4;
int pleasefiremillion = 5;

void setup()
{
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  ////Serial.begin(9600);
}


void loop()
{
  while (analogRead(ballinmillion) > 500)
  { //Serial.println ("millionswitch");
    if (analogRead(cpufiredmillion) > 200)
    { //Serial.println ("detectedCPUfired_MillionSOLENOID");
      digitalWrite(openmillion, HIGH);
      delay (150);
      //Serial.println ("millionCPUswitchOPENED");
    }
    else if (analogRead(pleasefiremillion) > 300)
    {  //Serial.println ("millionFIRErequest");
      digitalWrite(firemillion, HIGH);
      //Serial.println ("millionFIRED");
      delay (150);
      digitalWrite(firemillion, LOW);
      //Serial.println ("millionSolenoidOFF");
      delay (250);
      digitalWrite(openmillion, LOW);
      //Serial.println ("millionCPUswitchCLOSED");
      //Serial.println ("million....STANDING-BY");
    }  
  }

  while (analogRead(ballinfunhouse) > 500)
  { //Serial.println ("funhouseswitch");
    if (analogRead(cpufiredfunhouse) > 200)
    {  //Serial.println ("detectedCPUfired_FunhouseSOLENOID");
      digitalWrite(openfunhouse, HIGH);
      delay (150);
      //Serial.println ("funhouseCPUswitchOPENED");
    }
    else if (analogRead(pleasefirefunhouse) > 300)
    {  //Serial.println ("funhouseFIRErequest");
      digitalWrite(firefunhouse, HIGH);
      //Serial.println ("funhouseFIRED");
      delay (150);
      digitalWrite(firefunhouse, LOW);
      //Serial.println ("funhouseSolenoidOFF");
      delay (250);
      digitalWrite(openfunhouse, LOW);
      //Serial.println ("funhouseCPUswitchCLOSED");
      //Serial.println ("funhouse....STANDING-BY");

    }  
  }
}

The loop function is called in an endless loop, so

In this routine, the “if (analogRead(cpufiredmillion) > 200) & if analogRead(cpufiredfunhouse) > 200” should only happen one time

does not seem like a realistic expectation.

Inside the loop() function, you have two while loops. The while statement says “as long as some condition is true, execute this block of code, over and over”.

The first while loop will execute as long as the value read from the “ballinmillion” pin is greater than 500. Now, ballinmillion might suggest to you what kind of sensor is attached to the pin, but it does not mean anything to me.

During that while loop, you read an analog value from the “cpufiredmillion” sensor. Again, that name does not begin to suggest what kind of sensor that might be, so I went back and read the comments to try to figure it out. That didn’t take long, as there weren’t any.

If the value read from the cpufiredmillion pin is greater than 200, you set the openmillion (whatever that is) pin HIGH, and delay for about 1/6 of a second. You did not set the openmillion pin to be an OUTPUT pin. You did set some numbered pins to OUTPUT. Whether they were the right pins, I didn’t bother checking. If you are going to use names for the pins, use them everywhere.

If the value read from the cpufiremillion pin is not greater than 200, and the value read from the pleasefiremillion pin is greater than 300, you set the firemillion pin HIGH for 1/6 of a second, then set it LOW. Then, you set the openmillion pin LOW.

The strange names and complete lack of comments makes it difficult to tell what is really supposed to be happening.

If you add comments, explain the names, and use the correct names in the pinMode statements, I’ll look at the code again.

Ive reduced the redundancy of the code and tried to make things clearer with comments. I hope this will help you to help me. Thanks for taking the time. Problem is, once the "open_switch" is (HIGH) it needs to stay HIGH until the LOW condition is met. Should that be what is happening here? Its not....I don`t know why.

int fire_solenoid = 9;  //digital pin for relay to fire solenoid
int open_switch = 11;  //digital pin for relay to open switch
int ball_switch_closed = 1; //analog pin for sensing if switch is closed 
int cpu_sent_signal = 3; //analog pin for sensing if the computer fired a relay
int signal_from_button = 5; //analog pin for sensing if a button has been pressed

void setup()
{
pinMode(fire_solenoid, OUTPUT);  //sets pin 9 to output
pinMode(open_switch, OUTPUT);    // sets pin 11 to output
}


void loop()
{
while (analogRead(ball_switch_closed) > 500)  //ball is against switch, switch is closed
{ 
if (analogRead(cpu_sent_signal) > 200) //cpu senses ball in switch
  { 
  digitalWrite(open_switch, HIGH);  //opens "ball in switch" connection to cpu
    delay (150);
}
else if (analogRead(signal_from_button) > 300) //user presses button
 {  
 digitalWrite(fire_solenoid, HIGH); //fire the solenoid "ball is kicked away from switch"
  delay (150);
  digitalWrite(fire_solenoid, LOW); //turn off the solenoid
  delay (250);
  digitalWrite(open_switch, LOW);  //closes switch, which allows cpu to sense the ball switch (for next time).
}  
}
}

These names are much better. The comments are an improvement, although some of them state the obvious. They clearly indicate that you are barking up the wrong tree.

int ball_switch_closed = 1; //[glow]analog[/glow] pin for sensing if [glow]switch[/glow] is closed

Analog pins are not meant for reading digital switches. Is the switch an on/off switch? If so, it should be connected to a digital pin.

actually, that part of the program works fine.

If you start off in the wrong direction, you can't get where you want to go.

Lets get you pointed in the right direction, first. Are the switches that you are reading with analogRead digital switches?

PaulS, In short, the reply to your question is no. They are not digital switches. I suppose philisophically in terms of 1 and 0 they could be referred to digital in some sense of "on" behavior and "off" behavior....but.... I think your assumption here is that the switch refered to receives a constant voltage that is friendly to the digital inputs. In a stable world designed around Arduinos I would tend to agree with you. However, in this case the switch is friendly only to the analog input for reasons I could go into in great depth with you if you so wish. I´m looking for help with the flow of the code and the processing of that digitalWrite (HIGH) and its failure to stay HIGH until told to go LOW. Thanks. arduiYES

Well, I interested in know what kinds of switches they are, since that seems to be fundamental to the issue you are having.

The last code you posted could have Serial.print() statements added, to print out the values read from the ball_switch_closed, cpu_sent_signal, and signal_from_button pins. That would quickly show you where, when, and why the code is turning off your output pin “prematurely”.

Thanks PaulS, that really helped.