English
Español
PCBWAY PCB service

PCBWAY PCB service

PCBONLINE PCB service






Arduino drone V2.0 - 1 Ch example

You can download the 1 Ch example code here


Unzip the file. Open the .ino file and upload it to the Arduino UNO/NANO. Make THIS connection aopen the serial monitor once Uploaded. Remember to select 250000 baud rate or change the speed in the code.




Go back:




Or copy this code:

/*One channel PWM read example; author: ELECTRONOOBS 
 * Subscribe: http://www.youtube.com/c/ELECTRONOOBS
 * Thank you
*/

//We create variables for the time width values of each PWM input signal
unsigned long counter_1, current_count;

//We create 4 variables to stopre the previous value of the input signal (if LOW or HIGH)
byte last_CH1_state;

//To store the 1000us to 2000us value we create variables and store each channel
int Ch1;      //In my case channel 1 of the receiver and pin D8 of arduino



void setup() {
  /*
   * Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. 
   * The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports:
     -B (digital pin 8 to 13)
     -C (analog input pins)
     -D (digital pins 0 to 7)
   
  //All Arduino (Atmega) digital pins are inputs when you begin...
  */  
   
  PCICR |= (1 << PCIE0);    //enable PCMSK0 scan                                                 
  PCMSK0 |= (1 << PCINT0);  //Set pin D8 trigger an interrupt on state change. 
                                                
                                               
  
  
  //Start the serial in order to see the result on the monitor
  //Remember to select the same baud rate on the serial monitor
  Serial.begin(250000);  

}

void loop() {
  /*
   * Ok, so in the loop the only thing that we do in this example is to print
   * the received values on the Serial monitor. The PWM values are read in the ISR below.
   */ 

    Serial.print("Ch1: ");
    Serial.print(Ch1);
    Serial.println("    ");
   
  
}




//This is the interruption routine
//----------------------------------------------

ISR(PCINT0_vect){
//First we take the current count value in micro seconds using the micros() function
  
  current_count = micros();
  ///////////////////////////////////////Channel 1
  if(PINB & B00000001){                              //We make an AND with the pin state register, We verify if pin 8 is HIGH???
    if(last_CH1_state == 0){                         //If the last state was 0, then we have a state change...
      last_CH1_state = 1;                            //Store the current state into the last state for the next loop
      counter_1 = current_count;                     //Set counter_1 to current value.
    }
  }
  else if(last_CH1_state == 1){                      //If pin 8 is LOW and the last state was HIGH then we have a state change      
    last_CH1_state = 0;                              //Store the current state into the last state for the next loop
    Ch1 = current_count - counter_1;   //We make the time difference. Channel 1 is current_time - timer_1.
  }



  
 
}
 






Go back: