byte last_channel_3; volatile int channel_3; unsigned long timer, current_time, loop_timer, esc_loop, esc_timer; unsigned long time, elapsedTime, timePrev; unsigned long Est = 10000000; void setup() { //Declare pins 4-7 as outputs DDRD |= B11110000; /*========== Remember to have your motors wired the same way as mine ============= top left --> 4 top right --> 5 bottom left --> 7 bottom right --> 6 channel 3 --> */ PCICR |= (1 << PCIE0); //Same thing as the function "attachInterrupt" but this runs faster and looks nerdier PCMSK0 |= (1 << PCINT2); //Attach interrupt on pin 10 loop_timer = micros(); } void loop() { // put your main code here, to run repeatedly: while (micros()- loop_timer < 4000); //Wait here for 4 milliseconds to pass so we can start sending a new signal loop_timer = micros(); esc_timer = loop_timer + channel_3; PORTD |= B11110000; //Same as digitalWrite pins 4-7, but way faster and turns them all on at the same time while (PORTD >= 16){ esc_loop = micros(); if(esc_timer <= esc_loop){ PORTD &= B00001111; //Turns them all off simultaneously } } } ISR(PCINT0_vect){ //Interrupt routine, basically triggers if there is a change of state current_time = micros() % Est; //makes sure that timer does not overflow (after around 72 hours, unsigned long overflows with the micros() timer) if(last_channel_3 == 0 && PINB & B00000100){ last_channel_3 = 1; timer = current_time; } else if (last_channel_3 == 1 && !(PINB & B00000100)){ last_channel_3 = 0; channel_3 = ((current_time - timer) > 0) ? (current_time - timer) : (Est - timer + current_time); } }