• I've tried some code @Gordon posted a few weeks ago but don't seem to be having any luck with that either :/

    var C = {
    PCA9685_SUBADR1 : 0x2,
    PCA9685_SUBADR2 : 0x3,
    PCA9685_SUBADR3 : 0x4,
    PCA9685_MODE1 : 0x0,
    PCA9685_PRESCALE : 0xFE,
    LED0_ON_L : 0x6,
    LED0_ON_H : 0x7,
    LED0_OFF_L : 0x8,
    LED0_OFF_H : 0x9,
    ALLLED_ON_L : 0xFA,
    ALLLED_ON_H : 0xFB,
    ALLLED_OFF_L : 0xFC,
    ALLLED_OFF_H : 0xFD
    };
    function PWMServoDriver(i2c) {
      this.i2c = i2c;
      this.addr = 0x40;  
      this.reset(()=>{
        this.setPWMFreq(1000);
      });
    }
    PWMServoDriver.prototype.write = function(r,d) {
      this.i2c.writeTo(this.addr,r,d);
    };
    PWMServoDriver.prototype.read = function(r) {
      this.i2c.writeTo(this.addr,r);
      return this.i2c.readFrom(this.addr,1)[0];
    };
    PWMServoDriver.prototype.reset = function(callback) {
      this.write(C.PCA9685_MODE1, 0x80);
      if (callback) setTimeout(callback,10);
    };
    PWMServoDriver.prototype.setPWM = function(num,on,off) {
      this.i2c.writeTo(this.addr,
                       C.LED0_ON_L+4*num,
                       on, on>>8,
                       off,off>>8 );
    };
    PWMServoDriver.prototype.setPWMFreq = function(freq) {
     // "Sets the PWM frequency"
      var prescaleval = 25000000.0; // 25MHz
      prescaleval /= 4096.0; // 12-bit
      prescaleval /= freq;
      prescaleval -= 1.0;
      prescale = Math.floor(prescaleval + 0.5);
    
      var data = this.i2c.readFrom(C.PCA9685_MODE1, 1);
    
      var oldmode = data[0];
      var newmode = oldmode & 0x7F | 0x10; // sleep
    
      this.i2c.writeTo(C.PCA9685_MODE1, newmode); // go to sleep
      this.i2c.writeTo(C.PCA9685_PRESCALE, Math.floor(prescale));
      this.i2c.writeTo(C.PCA9685_MODE1, oldmode);
      setTimeout(() => {
        return this.i2c.writeTo(C.PCA9685_MODE1, oldmode | 0x80);
      }, 5000);
    };
    const connect = function(i2c) {
      return new PWMServoDriver(i2c);
    };
    
    
    
    
    
    setTimeout(() => {
    var i2c = new I2C(); // software I2C
    i2c.setup({sda:NodeMCU.D1, scl:NodeMCU.D2});
    var pwm = connect(i2c);
      pwm.setPWMFreq(60);
      var SERVOMIN = 150;
      var SERVOMAX = 600;
    
      setInterval(() => {
        for (let pulselen = SERVOMIN; pulselen < SERVOMAX; pulselen++) {
          pwm.setPWM(0, 0, pulselen);
        }
      }, 1);
    
     
    }, 200);
    
About

Avatar for jameslouiz @jameslouiz started