Coding divide-by-2 square waveform

Hi,

I’ve a variable square waveform signal coming on pin 2 and I want to output half of its frequency on pin 3?

In other words, I want to create a divide-by-2 circuit. I know I can use a simple flip-flop ckt but that’s beyond my needs.

Can it be down and if so, how?

TIA

void setup() {
  attachInterrupt(digitalPinToInterrupt(2), ISR, RISING); 
}

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

}

void ISR() {
  digitalWrite(3, !digitalRead(3));
}

or, for a UNO:

void ISR() {
  PORTD ^= 1<<PD3;
}

Paul,

As I understand it, the output signal state changes/flips when a rising edge is detected on the input signal creating the desired division. Slick!

I don’t have an o-scope at hand at the moment so I generated an internal square waveform signal to test the theory and visually (using pin 13) it’s working.

Thank you much.

/*
  Uno, Nano, Mini, other 328-based  interrupt pins: 2 & 3
  Mega, Mega2560, MegaADK  interrupt pins: 2, 3, 18, 19, 20, 21
  ATtiny  interrupt pins: http://arduino.stackexchange.com/questions/3929/attiny85-interrupt-id-vs-pin-when-programming-with-arduino
*/

// to test internal generated signal -> jumper between pin 2 and 4

const byte interruptPin = 2;
const byte signalPin = 4;
const byte ledPin = 13;
volatile byte state = LOW;
int bypass; // used to bypass signal through chip

void setup() {
  pinMode(signalPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  //pinMode(interruptPin, INPUT_PULLUP);
  pinMode(interruptPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(interruptPin), divide, RISING);
}

void loop() {

  genSignal(); // generate square waveform
  //byPass(); // by-pass signal through micro

}

void genSignal() {
  digitalWrite(signalPin, LOW);
  delay(2000);
  digitalWrite(ledPin, state);
  digitalWrite(signalPin, HIGH);
  delay(2000);
  digitalWrite(ledPin, state);
}

void byPass() {
  bypass = digitalRead(signalPin);
  digitalWrite(ledPin, bypass);
}

void divide() {
  state = !state;
}