Skip to content
This repository was archived by the owner on Mar 20, 2020. It is now read-only.

Commit

Permalink
Add examples for utilization of GPIO
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoxw committed Oct 15, 2017
1 parent 5dfba58 commit 680b631
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
48 changes: 48 additions & 0 deletions examples/GPIO/DCmotors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "GPIOlib.h"

using namespace GPIO;

int main()
{
init();

//Move forward
controlLeft(FORWARD,50);
controlRight(FORWARD,50);
delay(1000);

//Stop
stopLeft();
stopRight();
delay(1000);

//Move backward
controlLeft(BACKWARD,50);
controlRight(BACKWARD,50);
delay(1000);

//Stop
stopLeft();
stopRight();
delay(1000);

//2 motors can work at different speeds.
controlLeft(FORWARD,30);
controlRight(FORWARD,40);
delay(1000);

//Stop
stopLeft();
stopRight();
delay(1000);

//Even directions can differ from each other.
controlLeft(BACKWARD,35);
controlRight(FORWARD,20);
delay(1000);

//Don't forget to stop all motors before exiting.
stopLeft();
stopRight();
return 0;
}
35 changes: 35 additions & 0 deletions examples/GPIO/decoderISR.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <cstdio>
#include <cmath>
#include "GPIOlib.h"

using namespace std;
using namespace GPIO;

int readingLeft=0,readingRight=0;

int main()
{
init();

controlLeft(FORWARD,30);
controlRight(BACKWARD,40);

for(int i=0;i<10;i++)
{
resetCounter();
delay(1000);
getCounter(&readingLeft,&readingRight);
if(readingLeft==-1||readingRight==-1)
{
printf("Error!\n");
continue;
}
//Distance is in mm.
double distanceLeft=readingLeft*63.4*M_PI/390;
double distanceRight=readingRight*63.4*M_PI/390;
printf("Left wheel moved %.2lf cm, right wheel moved %.2lf cm in last second.\n",distanceLeft/10,distanceRight/10);
}
stopLeft();
stopRight();
return 0;
}
18 changes: 18 additions & 0 deletions examples/GPIO/servo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "GPIOlib.h"

using namespace GPIO;

int main()
{
init();

//THIS IS THE SAFE RANGE!
for(int i=-45;i<=45;i+=15)
{
turnTo(i);
delay(1000);
}

turnTo(0);
return 0;
}

0 comments on commit 680b631

Please sign in to comment.