Light Sensitive Robot (Arduino IDE)

Go to https://bitbot.l33t.uk/arduino-ide/ultrasonic-example-arduino-ide for instructions on how to configure Arduino IDE for use with the BBC Micro:bit if you haven’t already. This code builds on the code found at https://bitbot.l33t.uk/arduino-ide/porting-bitbot-code-from-micropython-to-c

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;

[pastacode lang=”c” manual=”%2F*%0A%20*%20Light%20Sensitive%20algorithm%20for%20the%204Tronix%20Bit%3ABot%0A%20*%20Author%20David%20Bradshaw%202018%0A%20*%2F%0A%0Avoid%20startLightSensitive()%0A%7B%0A%20%20%20%20uint32_t%20referenceLevel%20%3D%20getAverageValues()%3B%0A%0A%20%20%20%20while%20(referenceLevel%20%3E%3D%20900%20)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2FBitbot%20will%20be%20stuck%20in%20this%20loop%20as%20long%20as%20the%20room%20is%20too%20bright%0A%20%20%20%20%20%20%20%20%2F%2FThe%20neopixels%20will%20be%20cyan%20in%20colour%20when%20the%20room%20is%20too%20bright%0A%20%20%20%20%20%20%20%20setColourRight(0%2C128%2C128)%3B%20%0A%20%20%20%20%20%20%20%20setColourLeft(0%2C128%2C128)%3B%0A%20%20%20%20%20%20%20%20referenceLevel%20%3D%20getAverageValues()%3B%0A%20%20%20%20%20%20%20%20Serial.print(%22REF%3A%20%20%20%22)%3B%0A%20%20%20%20%20%20%20%20Serial.println(referenceLevel)%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F%2FWhen%20the%20neopixels%20turn%20pink%20it%20indicates%20that%20the%20room%20is%20dark%20enough%0A%20%20%20%20%2F%2Fand%20a%20good%20reference%20level%20has%20been%20recorded%0A%20%20%20%20neoPixels.clear()%3B%0A%20%20%20%20setColourRight(128%2C0%2C128)%3B%20%0A%20%20%20%20setColourLeft(128%2C0%2C128)%3B%0A%0A%20%20%20%20while(1)%20%2F%2Finfinite%20loop%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20lightFollow(referenceLevel)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Avoid%20lightFollow(uint32_t%20referenceLevel)%0A%7B%0A%20%20%20%20uint32_t%20avgBrightness%20%3D%20getAverageValues()%3B%20%20%2F%2Fget%20average%20light%20values%0A%0A%20%20%20%20%2F%2FFollower%0A%20%20%20%20if%20(avgBrightness%20%3E%3D%20(referenceLevel%20%2B%2050))%20%20%2F%2FThe%20difference%20between%20ambient%20light%20and%20the%20follower%20light%20must%20be%2050%20or%20more%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20neoPixels.clear()%3B%0A%20%20%20%20%20%20%20%20setColourRight(0%2C64%2C0)%3B%20%2F%2FTurn%20the%20neopixels%20green%20to%20let%20the%20user%20know%20it%20can%20see%20light%0A%20%20%20%20%20%20%20%20setColourLeft(0%2C64%2C0)%3B%0A%0A%20%20%20%20%20%20%20%20int%20_leftSpeed%20%3D%20(detectLight(%22LEFT%22)%20%2F%204)%3B%20%20%2F%2Fdivide%20by%204%20max%20val%20for%20motors%20is%20255%20max%20val%20from%20sensors%20can%20be%201023%0A%20%20%20%20%20%20%20%20int%20_rightSpeed%20%3D%20(detectLight(%22RIGHT%22)%20%2F%204)%3B%0A%0A%20%20%20%20%20%20%20%20if(_leftSpeed%20%3E%20_rightSpeed)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20_leftSpeed%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%20if%20(_leftSpeed%20%3C%20_rightSpeed)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20_rightSpeed%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20move(_leftSpeed%2C%20_rightSpeed%2C%200%2C%200)%3B%20%2F%2FMove%20left%2C%20right%20or%20forwards%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20move(0%2C%200%2C%200%2C%200)%3B%0A%20%20%20%20%20%20%20%20neoPixels.clear()%3B%0A%20%20%20%20%20%20%20%20setColourRight(128%2C0%2C128)%3B%20%0A%20%20%20%20%20%20%20%20setColourLeft(128%2C0%2C128)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Auint32_t%20getAverageValues()%0A%7B%0A%20%20%20%20%2F%2FCalculates%20an%20ambient%20light%20value%0A%20%20%20%20uint32_t%20rightLeftVal%20%3D%20detectLight(%22LEFT%22)%3B%0A%20%20%20%20uint32_t%20rightRightVal%20%3DdetectLight(%22RIGHT%22)%3B%0A%20%20%20%20return%20uint32_t((rightLeftVal%20%2B%20rightRightVal)%2F2)%3B” message=”Complete Code Listing” highlight=”” provider=”manual”/]

I then called the startLightSensitive() method from the loop() method in the _00_programLoop tab.

Colours are the same Cyan = rooms too bright, Pink = waiting for light, Green = following light.

The code for this can be downloaded here Bitbot_Lib_LightSS.zip or the resources section.