include
include
include
include
define PIN 10
Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_TSL2561_Unified tsl1 = Adafruit_TSL2561_Unified(TSL2561_ADDR_LOW, 12345); Adafruit_TSL2561_Unified tsl2 = Adafruit_TSL2561_Unified(TSL2561_ADDR_HIGH, 67890);
int enable1 = 11; int enable2 = 12;
float prev_time;
void left_forward (int speed){ analogWrite(9, speed); analogWrite(7, speed); analogWrite(8, 0); analogWrite(6,0); } void left_reverse (int speed){ analogWrite(9, speed); analogWrite(7, speed); analogWrite(8, 255); analogWrite(6,255); } void right_forward (int speed ){ analogWrite(4, speed); analogWrite(2, speed); analogWrite(5, 255); analogWrite(3,255); } void right_reverse (int speed ){ analogWrite(5, speed); analogWrite(3, speed); analogWrite(4, 0); analogWrite(2,0); }
void displaySensorDetails(void) { //blank }
void configureSensor(void) { /* You can also manually set the gain or enable auto-gain support / // tsl.setGain(TSL2561_GAIN_1X); / No gain ... use in bright light to avoid sensor saturation / // tsl.setGain(TSL2561_GAIN_16X); / 16x gain ... use in low light to boost sensitivity / tsl1.enableAutoRange(true); / Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) / tsl1.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); / fast but low resolution / // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); / medium resolution and speed / // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); / 16-bit data but slowest conversions */
/* You can also manually set the gain or enable auto-gain support / // tsl.setGain(TSL2561_GAIN_1X); / No gain ... use in bright light to avoid sensor saturation / // tsl.setGain(TSL2561_GAIN_16X); / 16x gain ... use in low light to boost sensitivity / tsl2.enableAutoRange(true); / Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) / tsl2.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); / fast but low resolution / // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); / medium resolution and speed / // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); / 16-bit data but slowest conversions */
}
void setup(void) { Serial.begin(9600); strip.begin(); strip.show(); // Initialize all pixels to 'off'
pinMode(enable1, OUTPUT); digitalWrite(enable1, LOW); pinMode(enable2, OUTPUT); digitalWrite(enable2, HIGH);
if(!tsl1.begin()) { Serial.print("Ooops, no 1 not detected"); while(1); } if(!tsl2.begin()) { Serial.print("Ooops, no 2 not detected"); while(1); }
displaySensorDetails(); configureSensor(); Serial.println(""); }
void loop(void) { for (int i = 0; i < 65; i++){ strip.setPixelColor(i,0,0,0); } strip.setPixelColor(18, 0, 0, 30); strip.setPixelColor(21, 0, 0, 30); strip.setPixelColor(41, 30, 0, 0); strip.setPixelColor(50, 30, 0, 0); strip.setPixelColor(53, 30, 0, 0); strip.setPixelColor(52, 30, 0, 0); strip.setPixelColor(51, 30, 0, 0); strip.setPixelColor(46, 30, 0, 0); strip.show(); float dt = (float)(micros() - prev_time)/1e6;
if (dt > 0.03) //params.pid_dt { prev_time = micros();
/* Get a new sensor event / sensors_event_t event1; tsl1.getEvent(&event1); sensors_event_t event2; tsl2.getEvent(&event2); / Display the results (light is measured in lux) */
float rSensor = event1.light; float lSensor = event2.light;
Serial.println(rSensor);
Serial.print(lSensor); Serial.print(" || "); Serial.println(rSensor);
if (lSensor - rSensor > 5) { //Go left left_reverse(122); right_forward(255); for (int i = 0; i < 65; i++){ strip.setPixelColor(i,0,0,0); }
strip.setPixelColor(19, 0, 0, 30); strip.setPixelColor(22, 0, 0, 30); strip.setPixelColor(41, 30, 0, 0); strip.setPixelColor(50, 30, 0, 0); strip.setPixelColor(53, 30, 0, 0); strip.setPixelColor(52, 30, 0, 0); strip.setPixelColor(51, 30, 0, 0); strip.setPixelColor(46, 30, 0, 0); strip.setPixelColor(33, 30, 0, 0); strip.setPixelColor(38, 30, 0, 0); strip.show(); } if (lSensor - rSensor < -5) { //Go right left_forward(255); right_reverse(122); for (int i = 0; i < 65; i++){ strip.setPixelColor(i,0,0,0); }
strip.setPixelColor(17, 0, 0, 30); strip.setPixelColor(20, 0, 0, 30); strip.setPixelColor(41, 30, 0, 0); strip.setPixelColor(50, 30, 0, 0); strip.setPixelColor(53, 30, 0, 0); strip.setPixelColor(52, 30, 0, 0); strip.setPixelColor(51, 30, 0, 0); strip.setPixelColor(46, 30, 0, 0); strip.show();
} else { //Go forward left_forward(255); right_forward(255); }
}
}
Log in or sign up for Devpost to join the conversation.