Last active
August 29, 2015 14:23
-
-
Save mithi/b6e9bf1ac7ef75b7ad23 to your computer and use it in GitHub Desktop.
Code for Arcbotics Sparki Light Sensor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Sparki.h> | |
class LightSense{ | |
int lightLeft; | |
int lightCenter; | |
int lightRight; | |
int del; | |
public: | |
LightSense(int d){ | |
del = d; | |
} | |
void update(){ | |
lightLeft = sparki.lightLeft(); | |
lightCenter = sparki.lightCenter(); | |
lightRight = sparki.lightRight(); | |
} | |
boolean isBrighter(int light1, int light2){ | |
return light1 > light2; | |
} | |
boolean center(){ | |
return isBrighter(lightCenter, lightLeft) && isBrighter(lightCenter, lightRight); | |
} | |
boolean left(){ | |
return isBrighter(lightLeft, lightCenter) && isBrighter(lightLeft, lightRight); | |
} | |
boolean right(){ | |
return isBrighter(lightRight, lightCenter) && isBrighter(lightRight, lightLeft); | |
} | |
void displayValues(){ | |
sparki.clearLCD(); | |
sparki.print("L: "); | |
sparki.println(lightLeft); | |
sparki.print("C: "); | |
sparki.println(lightCenter); | |
sparki.print("R: "); | |
sparki.println(lightRight); | |
sparki.updateLCD(); | |
delay(del); | |
} | |
void draw(){ | |
sparki.clearLCD(); | |
sparki.print("L: "); | |
sparki.println(left() ? "======[]" : "=[]"); | |
sparki.print("C: "); | |
sparki.println(center() ? "======[]" : "=[]"); | |
sparki.print("R: "); | |
sparki.println(right() ? "======[]" : "=[]"); | |
sparki.updateLCD(); | |
delay(del); | |
} | |
void follow(){ | |
if (left()){ | |
sparki.moveLeft(); | |
}else if (center()){ | |
sparki.moveForward(); | |
}else if (right()){ | |
sparki.moveRight(); | |
} | |
delay(100); | |
} | |
void avoid(){ | |
if (left()){ | |
sparki.moveRight(); | |
} else if (center()){ | |
sparki.moveBackward(); | |
} if (right()){ | |
sparki.moveLeft(); | |
} | |
delay(100); | |
} | |
}; | |
LightSense lightSense(100); | |
void setup(){ | |
} | |
void loop(){ | |
lightSense.update(); | |
lightSense.avoid(); // or lightSense.follow() | |
lightSense.draw(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment