B4R Code Snippet Adafruit Motor Shield V1 for Arduino...

Dear friends,
Here is the code , using inline C, for controlling DC motors via Adafruit Motor Shield V1 . Adafruit library is attached herewith.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Dim PWM As Int
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    RunNative("M1_stop",Null)
    PWM=255
    AddLooper("Looper")
End Sub

Sub Looper
    Log("FORWARD")
    RunNative("M1_drive_fwd",Null)
    Delay(5000)
    RunNative("M1_stop",Null)
    Log("STOPPED")
    Delay(1000)
    Log("BACKWARD")
    RunNative("M1_drive_rev",Null)
    Delay(5000)
    Log("STOPPED")
    RunNative("M1_stop",Null)
    Delay(1000)
End Sub



#if C
#include <AFMotor.h>
//AF_DCMotor M1(1); //M1 with default PWM frequency of 1KHz
AF_DCMotor M1(1, MOTOR12_64KHZ);
//AF_DCMotor M1(1, MOTOR12_8KHZ);
//AF_DCMotor M1(1, MOTOR12_2KHZ);
//AF_DCMotor M1(1, MOTOR12_1KHZ);

void M1_stop (B4R::Object* o){
M1.run(RELEASE);
}
void M1_drive_fwd (B4R::Object* o) {
   M1.run(FORWARD);
    M1.setSpeed(b4r_main::_pwm);
}
void M1_drive_rev (B4R::Object* o) {
   M1.run(BACKWARD);
    M1.setSpeed(b4r_main::_pwm);
}

#End if
 

Attachments

  • Adafruit-Motor-Shield-library.zip
    14.7 KB · Views: 532
Top