Skip to content

Instantly share code, notes, and snippets.

@mithi
Last active Aug 29, 2015
Embed
What would you like to do?
edge avoidance improved version for sparki. improved version.
#include <Sparki.h>
class EdgeAvoidance{
int threshold;
int angle;
int distBack;
int leftEdge;
int rightEdge;
int wait;
public:
EdgeAvoidance(int vThresh, int vAngle, int vDistBack, int vWait){
threshold = vThresh;
angle = vAngle;
distBack = vDistBack;
wait = vWait;
}
void checkEdge(){
leftEdge = sparki.edgeLeft();
rightEdge = sparki.edgeRight();
}
boolean inEdge(int edgeValue){
return edgeValue < threshold;
}
boolean inCorner(){
return inEdge(rightEdge) && inEdge(leftEdge);
}
void avoid(){
sparki.moveBackward(distBack);
inEdge(leftEdge) ? sparki.moveLeft(angle) : sparki.moveRight();
}
void act(){
checkEdge();
showValues();
if(inEdge(leftEdge) || inEdge(rightEdge)){
avoid();
}else{
sparki.moveForward();
delay(wait);
}
}
void actForever(){
while(true){
act();
}
}
void showValues(){
sparki.clearLCD();
sparki.print("leftEdge:");
sparki.println(leftEdge);
sparki.print("rightEdge:");
sparki.println(rightEdge);
sparki.updateLCD();
}
};
EdgeAvoidance edgeAvoid(200, 30, 15, 100);
void setup(){
}
void loop(){
edgeAvoid.act();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment