B4R Code Snippet PCA9685 16-channel, 12-bit PWM SERVO board....

Dear friends,
Yesterday I received the breakout board for PCA9685 (I2C,16-channel,12-bit PWM) for servo. Here is the B4R code using inline c . Please install the attached Arduino library.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private timer1 As Timer
    Dim CH,POS As Int
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    timer1.Initialize("timer1_Tick",5000)
    RunNative("setup",Null)
    CH=0
    POS=0
    RunNative("reset",Null)
    timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
    CH=0
    For POS=0 To 420
    RunNative("drive",Null)
    Delay(10)
    Next

    Delay(1000)
    For POS=420 To 0 Step -1
        RunNative("drive",Null)
        Delay(10)
    Next
    Delay(1000)
End Sub


#if C
#include "HCPCA9685.h"
#define  I2CAdd 0x40
HCPCA9685 HCPCA9685(I2CAdd);
void setup(B4R::Object* o){
HCPCA9685.Init(SERVO_MODE);
HCPCA9685.Sleep(false);
}
void reset (B4R::Object* o) {
    HCPCA9685.Servo(b4r_main::_ch,b4r_main::_pos);
}
void drive (B4R::Object* o) {
    HCPCA9685.Servo(b4r_main::_ch,b4r_main::_pos);
}

#End if
The I2C default address for my board is 0x40.
I have tested it with Wemos D1 R2 board.
IMG_20170822_104525.jpg
 

Attachments

  • HCPCA9685.zip
    9.8 KB · Views: 713
Last edited:
Top