Class this Friday was a continuation of working with Arduinos for our new unit: Feedback and Control. Today we learned how to use an Arduino to control the movement of a servo motor as well as another method of controlling LEDs.
A photocell is a sensor device that is used to detect levels of light. Today we modified a program that translates the light the photocell detects into several reading levels; "Dark", "Dim", "Light", "Bright" or "Very Bright." The original code shown below was modified such that a servo motor moves and an LED light blinks in accordance to the photocell readings.
Arduinos and Photocells
Original Code:
Calibrated Photocell, LED, and Motor:
In order to use the original code above, the photocell needed to be calibrated such that it could adjust its sensor reading levels to match the relative intensity of the in light in its surroundings. In order to do this, we programmed the Arduino to allow the user to manually calibrate the sensor's minimum and maximum readings each time the code is implemented.
We also added a section in the void loop of the code that would proportionally control the blink rate of an LED based on the reading levels of the photocell, as well as an additional section similiarly controlling the movement of a servo motor.
End Result:
Arduinos and S.O.S. Functions
An S.O.S. type function applies a morse code like strategy that is useful for shortening long codes that repeat a lot of the same functions. This is achieved by initially defining a function as something short such as "dot()" or "dash()," and then thereafter using the shorter abbreviation in the code. Below is a S.O.S function that we practiced on. This code was able to cause an LED to blink in a morse code pattern (SOS).
Setting up the Sciborg
- Assembling a battery pack by soldering its wires and attaching it to the cyborg using screws.
- Attaching an Arduino to the top of the Sciborg
- Attaching a Breadboard to the top of the Sciborg
- Attaching a Bricktronics shield to the top of the Arduino
Sciborg: Single Motor
Sciborg: Double Motor/Drive Straight
Sciborg: Minimum Motor Speed
Sciborg: Hard Turn
Sciborg: Gentle Turn
Sciborg: Travel 10ft and Stop
Touch Switch
Initially we were given the sample code below that turns on the LED at pin 9 once the touch switch is pressed. Thereafter, the LED stays on unless the code is reimplemented as there are no conditions specified for when the button is released.
Controlling LED
Part of our task for this section was to figure out how to modify the above code such that the LED at pin 9 would only turn on only while the touch switch was pressed, and otherwise be off. This was achieved by defining the conditions of the LED when the button was not pressed as off.
Sciborg: Motor Button
In order to program this, we used the touch switch codes from the previous days as a basis for the new one. First we modified one of our older codes such that the Sciborg motors turn on when the touch sensor button is not pressed, but turns off when the button is pressed. Then we added an additional component that makes the Sciborg change directions after the button has been pressed. The result can be seen below.
For our final code, however, we wanted the Sciborg to be able to move regardless of whether the button is pressed, and rather change directions instantaneously. In order to achieve this, we set the Sciborg's default movement to forwards and straight. Only when the touch switch gets activated does the Sciborg briefly move backward, turn, and then continue forward,
Reflection
Today I had a lot of fun working with the Sciborgs and LEDs. I felt it was very useful to have visual feedback from our devices as we modified our codes.
No comments:
Post a Comment