digitalWrite to 2 pins, but they alternate instead of both in same cycle?

So have a simple program to generate paired pulses every 100 milliseconds. At least I thought straight forward from looking at examples.

Program should be producing 2 short pulses with onsets 100ms apart, and do it every ten seconds on pins 3 and 4. For some reason it seems to alternate between the pins (so one set of pulses on pin 3, 10 seconds later a pair of pulses on pin four, ten seconds a pair of pulses on pin 3, making 20 seconds between each set of pulses on a pin - see pic attached).

Tried disconnecting one pin to see if it was a load/read error on the other side, but just then get the paired pules still every 20 seconds on the one channel.

/* Simple paired pulse stim generator  Terry Kremin
 *    Stim unit 1 should be connected to pin 3, unit 2 to pin 4
 *    'wide' is the width of each pulse, kept small to be just long enough to trigger stim unit 
 *    'isi' = inter stim interval, how far apart inside the pair of pulses. 
 *    'interval' = time between onset of each paired pulse.  
 *    timing must be adjusted to keep everything in line and timed from onset of the first of a pair. 
 *      (so adjust for pulse widths, and for ISI for teh over all interval)
 *    Onboard LED  (L) on during interval, off during stims. 
 *    
 */



int stim1 = 3;
int stim2 = 4;
int wide = 5;
int isi = 95; 
int interval = 9890;

void setup() {
  // put your setup code here, to run once:

  pinMode( stim1, OUTPUT);
  pinMode( stim2, OUTPUT); 
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:

  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(stim1, HIGH);
  digitalWrite(stim2, HIGH); 
  delay(wide);
  digitalWrite(stim1, LOW);
  digitalWrite(stim2, LOW); 
  delay(isi);
  digitalWrite(stim1, HIGH);
  digitalWrite(stim2, HIGH); 
  delay(wide);
  digitalWrite(stim1, LOW);
  digitalWrite(stim2, LOW);
  digitalWrite(LED_BUILTIN, HIGH); 
  delay(interval);
  
  


  

}

This could work okay if I set the delay shorter to 5 seconds, but not sure exactly why it is doing it in an alternating fashion, and would rather understand and make sure it is actually coded correctly!

Thanks for any help you can provide!

The first thing to do is to check the LED wiring. Do you have current limiting resistors in place ?

What are pins 3 and 4 connected to?
Where (and how) are you taking the measurements of the output signals?

The code looks like it should do what you want. I can't explain what you are measuring but it appears to be the output of a different sketch. Are you sure the new sketch is uploading without error?

They are not going to LEDs, they will eventually be connected to stimulus generators to trigger pulses.

Right now they are connected through the BNC inputs inputs on a Micro1400 DAU ( http://ced.co.uk/products/mic3in) and recorded using SPike2 (in the picture attached) (http://ced.co.uk/products/spkovin).

And the code looks right, right? I’m not totally off here? And it is working perfectly if I wanted it to alternate between each interval!

I tried uploading the sketch a couple of times, but I will try again. Doesn’t cost anything, right?

tk

So tried resetting a few times, powering down and restarting a few times with multiple reloads and checks of the sketch.

and the connections basically make it connected to an expensive recording oscilloscope right now. Still get the same alternation pattern.

I could get maybe the two digitalWrite commands back to back making it skip/miss the second, but why it alternates and not just consistently misses one is beyond me!

Follow on - the units should be fully able to record this. I have tried disconnecting one or the other and all it does it eliminate one trace, and the other still goes at every 20 seconds (2*interval), so it doesn’t seem to be a recording error.

And the code looks right, right?

Yes. I would focus on the measurement and plotting methods.

How are you connected to the Micro 1401?

What Arduino are you using? Is it 16MHz?

I tried the sketch, with Serial prints to check the port status before each delay, and on an UNO the outputs are as expected, so not sure what is going on with your measurements.

You might try this sketch, it is the same as yours but directly writes the output ports, so that both outputs change simultaneously.

/* Simple paired pulse stim generator  Terry Kremin
      Stim unit 1 should be connected to pin 3, unit 2 to pin 4
      'wide' is the width of each pulse, kept small to be just long enough to trigger stim unit
      'isi' = inter stim interval, how far apart inside the pair of pulses.
      'interval' = time between onset of each paired pulse.
      timing must be adjusted to keep everything in line and timed from onset of the first of a pair.
        (so adjust for pulse widths, and for ISI for teh over all interval)
      Onboard LED  (L) on during interval, off during stims.

*/

const byte stim1 = 3;
const byte stim2 = 4;
const byte portmask = 1 << PIND3 | 1 << PIND4;
const int wide = 5;
const int isi = 95;
const int interval = 9890;

void setup() {
  pinMode( stim1, OUTPUT);
  pinMode( stim2, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, LOW);
  PORTD |= portmask;
  delay(wide);
  PORTD &= ~portmask;
  delay(isi);
  PORTD |= portmask;
  delay(wide);
  PORTD &= ~portmask;
  digitalWrite(LED_BUILTIN, HIGH);
  delay(interval);
}

It is an UNO R3.

And running them through the ADC ins on the 1401 seems to be the issue, hooked them to the event channels and they both show up just fine like they are supposed to!

Thanks to everyone for the brainstorming!