You are reading a single comment by @Robin and its replies. Click here to read the full conversation.
  • Hi!
    I have an IR intervalometer for a Sony a7 camera on an Arduino Nano. It works great, but I need to implement the same on esp32.

    Arduino simplified code:

    unsigned long TimeBetweenShots = 2000;
    int IRled = 2;
    int Begin = 13;
    
    int SCodeBits[] = {1,0,1,1,0,1,0,0,1,0,1,1,1,0,0,0,1,1,1,1­};
    
    void setup()
    {
      pinMode(IRled, OUTPUT);
      pinMode(Begin, OUTPUT);
    }
     
    void loop()
    {
      static int delta = 0;
    
      delta+=100;
      
      digitalWrite(Begin, HIGH);
      
      for (int i=1; i <= 3; i++)
      {
        header();
        for (int i=0; i <= 19; i++)
        {
              if(SCodeBits[i] == 1)
              {
                Data_is_One();
              }
              else
              {
                Data_is_Zero();
              }
        }
        delay(11);
      }
      digitalWrite(Begin, LOW);
      delay(TimeBetweenShots+delta);
    }
    
    void burst()
    {
      digitalWrite(IRled, HIGH);
      delayMicroseconds(10);  
      digitalWrite(IRled, LOW);
      delayMicroseconds(8);
    }
    
    void quiet()
    {
      digitalWrite(IRled, LOW);
      delayMicroseconds(18);
    }
    
    void header() 
    {
        for (int i=1; i <= 96; i++){
          burst();
        }
        for (int i=1; i <= 24; i++){
          quiet();
        }
    }
    
    void Data_is_One()
    {
        for (int i=1; i <= 48; i++){
          burst();
        }
        for (int i=1; i <= 24; i++){
          quiet();
        }
    }
    
    void Data_is_Zero()
    {
        for (int i=1; i <= 24; i++){
          burst();
        }
        for (int i=1; i <= 24; i++){
          quiet();
        }
    }
    

    This is JS for Espruino, just for one shot:

    const IRled = D17;
    
    let SCodeBits = [1,0,1,1,0,1,0,0,1,0,1,1,1,0,0,0,1,1,1,1­];
    
    function shot(){
      pinMode(IRled,"output");
    
      function burst(){
        digitalPulse(IRled,1,[0.01,0.008]);
      }
    
      function quiet(){
        digitalPulse(IRled,0,0.018);
      }
    
      function header(){
        for (let i=0; i < 96; i++){
          burst();
        }
        for (let i=0; i < 24; i++){
          quiet();
        }
      }
      function  Data_is_One(){
        for (let i=0; i < 48; i++){
          burst();
        }
        for (let i=0; i < 24; i++){
          quiet();
        }
      }
      function Data_is_Zero(){
        for (let i=0; i < 24; i++){
          burst();
        }
        for (let i=0; i < 24; i++){
          quiet();
        }
      }
    
      for (let i=0; i < 3; i++){
        setTimeout(function() {
          header();
          for (let j=0; j < 20; j++){
              if(SCodeBits[j] === 1){
                Data_is_One();
              }
              else{
                Data_is_Zero();
              }
          }
          console.log('IR');
        }, 11);
      }
    }
    
    function onInit() {
      setTimeout(function() {
        shot();
      }, 1000);
    }
    

    And this code doesn't work... Where did I go wrong?

  • Sun 2021.09.12

    Hi @lshk it appears you may be the first with this specific device functionality.

    There seems to be an apparent mis-understanding of how Javascript timer events work in comparrison with Arduino delays.

    While Arduino code continually loops and toggles a pin with a delay, the timeout L57-L61 that triggers the L58 shot() function, will run after the timeout interval has elapsed.

    This will take a bit of finagling, but first check out with it's live online 'Try It' windows

    https://www.w3schools.com/js/js_timing.a­sp

  • Ok, but that's not the problem here. I removed all delays, commented out digitalPulse, but these loops are very slow. At this cycle rate, the pulse transmission frequency does not correspond to the required.

About

Avatar for Robin @Robin started