Skip to content

Instantly share code, notes, and snippets.

@mithi
Last active Aug 29, 2015
Embed
What would you like to do?
Code for Arcbotics Sparki Light Sensor
#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