I have added code for a light seeking robot. I saw little point to create code for a light avoider; the code is similar just inverted. I opened the previous project and created a new tab called _02_LightSensitive and added the below code;
Complete Code Listing
/* * Light Sensitive algorithm for the 4Tronix Bit:Bot * Author David Bradshaw 2018 */
while (referenceLevel >= 900 ) { //Bitbot will be stuck in this loop as long as the room is too bright //The neopixels will be cyan in colour when the room is too bright setColourRight(0,128,128); setColourLeft(0,128,128); referenceLevel = getAverageValues(); Serial.print("REF: "); Serial.println(referenceLevel); }
//When the neopixels turn pink it indicates that the room is dark enough //and a good reference level has been recorded neoPixels.clear(); setColourRight(128,0,128); setColourLeft(128,0,128);
//Follower if (avgBrightness >= (referenceLevel + 50)) //The difference between ambient light and the follower light must be 50 or more { neoPixels.clear(); setColourRight(0,64,0); //Turn the neopixels green to let the user know it can see light setColourLeft(0,64,0);
int _leftSpeed = (detectLight("LEFT") / 4); //divide by 4 max val for motors is 255 max val from sensors can be 1023 int _rightSpeed = (detectLight("RIGHT") / 4);