You can also do it like this as described in post #4. You still have to answer the question about the spaces between the pulses. I've assumed a 50% duty cycle below.
#include <TimerOne.h> // you may have to install this
int touch1 = 13;
int touch2 = 11;
unsigned long touch1_MARK_ms = 63 ; // tune these parameters to suit your application
unsigned long touch1_SPACE_ms = 63 ;
unsigned long touch2_MARK_ms = 16 ;
unsigned long touch2_SPACE_ms = 16 ;
void Timer_ISR() {
// called every 1ms
static unsigned int touch1_counter = 0 ;
static unsigned int touch2_counter = 0 ;
// touch1
if( touch1_counter == 0 ) digitalWrite(touch1, HIGH );
else if ( touch1_counter == touch1_MARK_ms ) digitalWrite(touch1, LOW );
touch1_counter ++ ;
if ( touch1_counter == touch1_MARK_ms + touch1_SPACE_ms ) touch1_counter = 0 ;
// touch2
if( touch2_counter == 0 ) digitalWrite(touch2, HIGH );
else if ( touch2_counter == touch2_MARK_ms ) digitalWrite(touch2, LOW );
touch2_counter ++ ;
if ( touch2_counter == touch2_MARK_ms + touch2_SPACE_ms ) touch2_counter = 0 ;
}
void setup() {
pinMode(touch1, OUTPUT);
pinMode(touch2, OUTPUT);
Timer1.initialize(1000); // initialize timer1 1000 uS period
Timer1.attachInterrupt(Timer_ISR); // attaches Timer_ISR() as a timer overflow interrupt
}
void loop() { }