switched 12v+ to separate momentary on & off pulses

How would I edit this code to do the following.

// What I need is:

// Pushbutton Switch with pull UP resistor for testing.(OPTOCOUPLER-NPN INPUT)

// Analog 0 is held HIGH by 10k resistor to 5V+. (switched 12v+ in the OFF state)

// When Analog 0 changes from High to LOW, OUTPUT 9 stays on for 1 second.

// Whilst Analog 0 is held LOW both OUTPUTS are OFF.(switched 12v+ in the ON state)

// When Analog 0 changes from LOW to HIGH, OUTPUT 8 stays on for 1 second.

// Whilst Analog 0 is held HIGH both OUTPUTS are OFF.(switched 12v+ in the OFF state)

// Picture of my setup http://i50.tinypic.com/2m5xte0.jpg

// Code in it’s current state:

// Pushbutton Switch with pull DOWN resistor for testing.

// Analog 0 is held LOW by 10k resistor to GND. (switched 12v+ in the OFF state)

// At start up OUTPUT 9 is OFF & OUTPUT 8 is OFF.

// Tap button & OUTPUT 9 turns ON for 1 second followed by OUTPUT 8 for 1 second.(pulse of 12v+)

// Whilst button is held OUTPUT 9 is ON & output 8 is OFF.(switched 12v+ in the ON state)

// When button is released OUTPUT 9 stays on for 1 second, then OUTPUT 8 turns on for 1 second.

// Thank you to Kenny From the Netherlands.

// Define what is going to be used
#define Analog_0 A0
#define Digital_8 8
#define Digital_9 9

void setup()
{
  //inputs and outputs
  pinMode(Analog_0, INPUT);
  pinMode(Digital_8, OUTPUT);
  pinMode(Digital_9, OUTPUT);  
}

//Use this so the switching can only happen once every switching state
boolean Dig_8_Allowed;
boolean Dig_9_Allowed;

void loop()
{
  if (digitalRead(Analog_0) == LOW)
  {
    Dig_9_Allowed = true;
    if (Dig_8_Allowed == true)
    {
      Dig_8_Allowed = false;
      digitalWrite(Digital_8, HIGH);
      delay(1000);
      digitalWrite(Digital_8, LOW);
    }
}
  if (digitalRead(Analog_0) == HIGH)
  {
    Dig_8_Allowed = true;
    if (Dig_9_Allowed == true)
    {
      digitalWrite(Digital_9, HIGH);
      delay(1000);
      digitalWrite(Digital_9, LOW);
    }
   }
  
}

Haven’t looked at the rest yet, but you should definitely connect a 22k resistor between the base and emitter of the two relay driving transistors. This is to prevent thermal runaway: see http://en.wikipedia.org/wiki/Thermal_ru … .28BJTs.29

Thank you for the info.

The pic is more just a quick mock up to help anyone reading.

I will actually be using:

http://www.ebay.com.au/itm/5V-2-Channel … 0690017735

It’s more the code I’m stuck on, Ive tried getting something to work using the switch/case statements example. (among other things)

But the code above is closer to my needs than anything I managed.

The helpful champ who wrote the code didn’t have much spare time, but he did tell me “Look into the blink without delay example, you will need it”.

I understand “This means that other code can run at the same time without being interrupted by the LED code”.

Unsure though how I would adapt this for my needs.

Why are you using an analog input for your optocoupler? Try a digital input instead.

I’m not really familiar with arduino, but there are some libraries that might help you, such as:

http://arduino.cc/playground/Code/Timer

It might also help to initialize the booleans, unless that’s done for you by the environment.

Thanks sethcim that is very helpful info.

To Philba the other digital pins are all used as toggle on/off outputs.

Also still new to this, after many many attempts the code above is still closer to my needs than anything I can manage on my own.

I would be willing to sacrifice a digital pin if I could get it to work.