Sunday, March 15, 2015

Continuing with Arduino

Within the past week my partner and I have completed several more tasks using Arduino, which I will detail below.

Task: Attach a photocell to your Arduino and adjust the code to get the correct readings.

My partner and I attached a photocell to our Arduino and loaded one of the example codes hat we would then manipulate. It turned out that on our first test of the photocell, the numbers given happened to correspond with the amount of light it was being exposed to. We were lucky that the readings came out so well, although they did vary some from trial to trial.

Task: Attach an LED and Servo to your Arduino and use the photocell to control them.

For this task, we originally tried to attach both the servo and the LED at once and incorporate their commands into the code we already had for the photocell. However this did not work so we worked backwards, deconstructing the different pieces until we got to a place that worked. This meant just attaching the LED and the photocell. Below is our code, a series of "if/else" statements that controlled the LED based on the photocell reading.

Next, we added on the servo. This required several iteration of our code in order to get both to work simultaneously. Below is a screen shot of our first code for LED/Servo combo:

For this, we tried directly incorporating the Servo code into what we already had with the photocell/LED code. However, the Servo still wouldn't move as a function of the photocell reading. For our second try, below, we took out the "map" command which converted values into a degree signal for the Servo. Yet again, it did not work correctly.


Finally, we decided to take the servo out of the "if/else" statements entirely and write the servo code beneath it. This way, we could have the LED/photocell code inside the "if/else" statements, which we knew worked, and try to get the servo to function separately.

 here you can see that the photocell controls the LED and the Servo separately, which ultimately worked. Below is a video of our photocell manipulating the blinking speed of the LED and the position of the Servo. The servo position may be difficult to see in the video but you can hear it move when the light reading changes.



Task: Write morse code for SOS to translate to blinking LED light

Below is a photo of the code we wrote. This task was to teach us how to use functions, such as the "dot()" and "dash()" function to simplify code writing. This allows us to define the function once and then just write in the function wherever it is needed instead of redefining it each time we want to use it. Under the "void loop" section is our code and below that are the individual dot and dash functions that we defined once. This code was successful and caused the LED built into the Arduino to blink in morse code.


Task: Set up sciborg

Moving forward with Arduino, we were challenged to assemble (partially) a sciborg to be independently controlled. Some of the things required for assembly were to solder the wires of the battery pack (which was so much fun!) and attach it onto the sciborg. We also had to screw in the Arduino and Bricktronics pack to the top and connect all the necessary cords. 
We then installed some of the Bricktronics example codes onto our Arduino software. Then, we were all set up to test various applications and functions of our sciborg.

Task: Run Single Motor test, then modify for two wheels

We then loaded the Single Motor example and tested it to make sure everything worked thus far. Then, we added a second motor, m2, which was connected to the other back wheel. We also modified our code to test several different speeds, both forwards and in reverse. We also manipulated the delay to see how it affected the motor.





 We also tried the speed at zero, and the program wouldn't upload, I assume because it does not support a zero speed command. Then we tried running the code with a speed of 1. This uploaded, but the wheels did not turn. This is because the motors did not have enough torque to run at such a slow speed. This implies that there is a minimum speed at which the sciborg will move. After a lot of guesswork and trials, we found the minimum speed to be 20. However, this was just running in the air, not on the ground. In hindsight, the maximum ground speed is probably higher.

Task: Write codes for sharp and gentle turns.

Below is a photo of our codes for both sharp and gentle turns. We did this by setting the motors m1 and m2 to opposite speeds, meaning one was negative. For the gentler turn, we reduced the speed of one wheel, which made the arc of the turn wider and the net speed of the car slower.
 Here is a video of the gentler turn:

Task: Write a code so your sciborg travels 10 ft. and then stops.

This task was actually the most difficult for me and my partner. Because we had the code running in a loop, it was difficult for us to figure out how to get the sciborg to eventually stop. We first tried using the currentMillis() function without a stop function and the sciborg just kept running. Then we tried adding the stop function, but ti was in the wrong place so the sciborg wouldn't move at all. We finally figured out the right formatting, which meant understanding that the stop function had to be placed as the "else" statement, and used a time estimate to run the sciborg. We ran into many issues when dealing with time intervals. Firstly, we did not account for the time it would take to unplug our sciborg and set it up on the start line. This accounts for the 10000 milliseconds where the sciborg is stopped before we give it an speed. Our second issue was that we greatly overestimated the time we might need to run 10 feet. This ended up being thirteen seconds. Below is our final code.
Below is a video of our sciborg running approximately 10 feet. The sciborg runs pretty straight in this video because we had already adjusted the motors to correct the crooked path of our sciborg, but originally it curved dramatically to the right.

Task: Adjust your code so that the sciborg runs straight

This is the task my partner and I completed just before the 10 feet task. Our sciborg originally curved to the right, especially at high speeds. To fix this, we increased the speed of the second motor. Originally, we increased its speed by 50 which then made it curve left. After many trials and errors we figured out that if the first motor was set at 200, the second should be at 211 for a straight run. However, we also learned that how you set the motor down affects its initial direction a lot, so often our sciborg needed some initial hand guiding to get on a straight path. We also set the default speed to be in the negative direction, which helped our sciborg move in a straight line. Below is a video of our adjusted sciborg.

Task: Attach a touch sensor to your sciborg and run the "motor button" sketch.

For this task, we had to remove some of the lego parts on the front of our sciborg and add new ones to properly attach the Lego sensor. We also used tape to keep it parallel with the floor, as it had a tendency to droop down. This would have made it difficult for the sensor to be pushed against a vertical surface. We then successfully ran the motor button sketch which stopped or started the wheels based on the state of the sensor (pressed or released).
In the loop section of the code, there are two delays. These are to give the wheels time to slow down before running the next command and to "debounce" or to double-check within a short amount of time that the command within the sketch was completed.

Task: Write a code so that your sciborg will travel forward at moderate speed, hit a wall, turn in reverse and continue on its way.

I had the most fun writing this code, because I felt that all the skills I have learned up until now were being combined into a cool and useful task. I also felt the results were the most rewarding. T write the code, we started off with our initial sketch of the sciborg traveling forward. Then, we added in the touch sensor sketch so that the speed could be controlled by the sensor. After our initial sketch of traveling forward, we wrote that while the button is pressed, there will be a delay for half a second and the the wheels will reverse, one with a greater speed than the other (the *2 in our sketch) so that the sciborg will change direction. This will happen for 2 seconds, so as to make a large enough turn, and then the direction with switch again so that the sciborg will travel forward in its new direction. Notice the +5 in the sketch. This was added to keep the sciborg driving straight throughout the process.

Below is a video showing our sciborg running the sketch:

This completes all the tasks thus far. Now my partner and I are working with encoders, touch switches, and ultrasonic sensors on the sciborg, which I will post about soon!



1 comment:

  1. I like how organized your posts are, it is helpful to outline the task before addressing it because it helps me to focus on what is important about the new modified code. Keep it up:)

    ReplyDelete