Pages

Friday, March 20, 2015

Arduinos: Day 3

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. 

Arduinos and Photocells


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.

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


In order to move on to the Bricktronics unit of our Arduino work, my partner and I needed to finish setting up a Sciborg provided by our instructor. Setup included:
  • 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


Our first task with the Sciborgs was to run and understand an example code for moving only one of the two motors on the Sciborg. After attaching both of the motors to the Arduino, my partner and I ran the sample program shown below. The program results in one of the wheels on the Sciborg moving forward at a speed of 75 and then 255, and then backwards at a speed of 75 and then 255.





Sciborg: Double Motor/Drive Straight


Using the single motor code, we created a new one that could control the movement of two motors simultaneously. The program below runs both motors at the same speed, allowing the Sciborg to travel in a straight line.




Sciborg: Minimum Motor Speed


Our net task was to find the minimum speed at which the sciborg could move. We already knew that the maximum speed of the Sciborg was 255, so we started at a speed of 100 and incrementally decreased the speed down until the device couldn't move, and then increased the speed until the Sciborg could move again. We determined the minimum speed to be 50.






Sciborg: Hard Turn


In order to make the Sciborg take a sharp turn we modified the two motor code such that one mother was turning at full speed in one direction, while the other was turning at full speed in the other direction. This resulted in the sciborg turning in place.





Sciborg: Gentle Turn


To make a more gentle turn we modified the above code in two ways. First we decreased the speed. We also made it so both wheels were turning in the same direction with one wheel slightly faster than the other.




Sciborg: Travel 10ft and Stop


The next task was to create a program that would allow the Sciborg to travel exactly 10ft and then stop. In order to do this, we first found the amount of time it took for the Sciborg to travel 10 ft at a certain speed (255) (9 seconds). After determining this time,  we programed the motors to stop after this time period (9 seconds) had elapsed. In our testing, the sciborg traveled the 10 feet exactly almost every time save for several times it was 2-3 cm short of the line.







Touch Switch


A touch switch is a type of sensor that can be operated by touch to induce a variety of responses. The Sciborgs we used in class have a touch switch on the front, as well as a detachable touch switch that can both be programmed using an Arduino.

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


The final project today was to program a touch sensor on the Sciborg that allows it to back up, turn, and then go forward again when it hits an obstacle.


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