Arduino Day 3 (Sciborg Part 1)

On Day 3, we started building and programming sciborgs, which are small cars run by 6-AA battery packs.  We attached the battery pack, the Arduino, and the protoboard to the sciborg, and then put a cover on top of the Ardunio that allows us to attach motors and sensors.



SOS Example

We used this example to learn about functions in Arduino.  In this example, there are two functions that are later used in the loop.  Using functions saves time and code writing.




Single Motor

Then, we attached the motors to the Arduino and ran the example program Single Motor. This program runs only one motor and results in the wheel moving backward and forward. We took this program as a starting point and modified it to create our next programs.



Double Motor

This program runs both motors at the same time and speed, so that the wheels of the sciborg move together and it will travel in a (hopefully straight) line.  We modified the original Single Motor program by adding the second motor and setting its speeds exactly the same as the first motor.



Minimum Speed

Next, we tested out different speeds on the sciborg.  We determined the lowest possible speed that made our sciborg still move was around 50.


Here is our sciborg moving incredibly slow!



Hard Turn

Now that we had our sciborg moving in a straight line, we had to figure out how to turn it.  First, we made the sciborg do a hard turn, essentially spinning it around in place.  To do this, we have each motor running at the same speed in opposite directions.





Gentle Turns

Since we don't want it to turn so abruptly, we needed to find how to implement some more gentle turns.  First, we had the sciborg go forward, turn a little, go forward, turn a little, and repeat.  This was successful, but resulted in a somewhat jerky turn.




The next gentle turn we tried out was smoother than the first.  One motor was set slightly faster than the other.  The result was the at the sciborg turned in a small circle.





10 feet

Next, we programmed our sciborg to travel exactly 10 feet.  We initially did this using the delay, finding just the right amount of time for the vehicle to move 10 feet.  After this determined time, which we found to be 9 seconds, we stopped the motor.  In our testing, the sciborg traveled the 10 feet exactly almost every time.  If it was off, it usually was short of 10 ft by about 2-3 cm.




Motor Button

After creating and testing some basic programs, Olivia and I started work on our biggest task of the day: when the sciborg hits an obstacles it backs up, turns, and continues on its way.

First, we needed learn how the Lego motor button worked, so we ran the example program Motor Button on our sciborg.  When the button is pressed, the motors stop, and when it is released, the motors start again.  In the following video, the motors have been set to opposite speeds, so the sciborg turns one way direction, then when the button is pressed and let go of, it turns the other direction.



Hitting a Wall

Now that we had played around with the motor button, we started on the code for hitting the wall.  This code makes the sciborg move straight, then when the button is pushed (it hits the "wall"), the sciborg backs up and turns.  Then the loop restarts and the sciborg continues straight until it hits something else.  For testing, we attached the motor button to the front of the sciborg.





Arduino Day 2

The second day of Arduino work introduced us to Servo motors and potentiometers.  We learned two new example programs called Sweep and Knob and modified them to control the motor and an LED.


Sweep program


The sweep program is used to control a Servo motor.  First we have to include the Servo "language" so that we able to use a Servo motor.  There is a variable that creates a Servo called myservo and then a variable called pos that we can use later to store the Servo position.  In the void setup(); we attach the Servo to pin 9 on our Arduino board.  The loop then tells the motor that when the position is is at 0, move to 180 (degrees) and when the motor is at 180, move back to 0, and do these motions in steps of 1 degree.  The delays waits 15 ms for the Servo to reach the designated position. We don't want the delay to be smaller than 15ms because the motor needs enough time to reach its final position.










































Modified Sweep Program

We next modified the Sweep program to have to have to motor do a different motion.  This helped us learn how the programing for the Servo motors works.  For our modification, Olivia and I made the motor move only 90 degrees.  We also changed the delay and the steps, and thus made it move differently on the second loop.  If we increase the delay (as we did here with 3000ms) for the second for loop, you can really see how changing the 'steps' affects the motor.  It will move, in this case, 15 degrees at a time until it reaches 90.  Our video was taken before we made the change though, so the 'steps' aren't seen here.








































Knob program with Servo Motor

Another example program for a Servo motor is the Knob program.  For this program, we use a potentiometer to control the movement of the motor, i.e. when we turn the knob on the potentiometer, the motor moves at the same time.  We connected the potentiometer to the analog pin A0 on our Arduino and ran the program.  The value is read from the potentiometer pin (potpin) and then that value uses map(); to change the scale of the potentiometer to the scale of the motor.  This makes the motor have the same motion as the potentiometer.
















Knob with LED


Our final task of the day was to use the potentiometer to control the blink rate of the LED, meaning it blinks slower when you turn the knob one way and blinks faster when you turn it the other way. We took our program for the servo and changed it to fit the LED. We again used map to scale the potentiometer reading to the LED rate.














Arduino Day 1

For this next unit, our class is working with Arduinos!  I'm working with Olivia Gada for these projects.  On our first day of Arduinos, we learned what Arduinos were, how to use the software, and how to write code to modify the Blink program, which makes one or more LEDs blink on and off.


The Arduino Uno

Here is an Arduino Uno board, the ones we use in class.  The digital pins at the top are the ones we use for LEDs.  It is important to note that one of the pins  - digital pin 13 - has an LED built into the board itself.  We use this pin to begin with because we don't need to connect an LED to the separate protoboard.
































Blink program

Arduino is an open source software company, meaning anyone can create, share, and change programs used for the hardware.  There are hundreds of example programs, so we use many of them to learn the basics and then modify them to create new programs.  We started by looking at the basic Blink program in the Arduino examples, so we could learn how to write code for our Arduino.  The Blink program makes an LED blink on and off.  It seems simple, but it was important for us to completely understand the code and syntax.

Arduino programming basics: The void setup() runs when the Arduino is connected.  This includes things that you want to happen once, like initializing pins on the Arduino, as we see here.  It's telling the Arudino that we're going to connect something to pin 13 and make it do something.  Then we have the next section void loop().  A loop, as its name suggests, goes through the code again and again and again until you stop it.  We use a loop here because we want the blinking to keep going, not just turn on and off and stop.  First, we set pin 13 to HIGH.  This tell the LED to turn on.  Then delay(); takes an amount of time in milliseconds.  Here we tell it to wait 1000ms (1sec).  Then we write to pin 13 and tell it to turn LOW, which turns the LED off.  And because of the loop, it now returns to the beginning where we tell pin 13 to turn to HIGH.  The final result is the LED blinks at 1 second intervals.








































Modifying Blink program to change blink rate


Next, we wanted to change the LED to blink differently to learn how the delays and times work.  All we need to do here is change the delay time. After we turn the LED to HIGH, we delay 2000ms, or 2 seconds, before turning it to LOW, and then delaying 500ms, or 1/2 of a second.  So now the LED is on for 2 seconds, off for 1/2 second, and keeps going   Again, it seems elementary, but seeing this code for the first time can be confusing and it's important we learn the basics.






























Modifying Blink to create a pattern with 2 LEDs

We now wanted to add a second LED using the protoboard.  In the code, we must initialize the second pin as an output and for this program we used digital pin 12.  Then, we just add some code to make a pattern.  In our program, pin 13 is on for 500ms. There is a short delay, then pin 12 is on for 500ms, then 50ms, 50ms, 50ms, with short delays between each. This creates the effect of a long blink(on 13) and then long, short, short, short (on 12).  And of course, because of the loop it keeps going.



Here is an image of our Arduino and protoboard.  The LED on the protoboard is connected to digital pin 12 and we used the pin already on the Arduino itself for pin 13.  The red wire goes from pin 13 to the same row as the LED, then we have a resistor after the LED, and the black wire connects back to the ground pin.






Modifying Blink to create a pattern with 4 LEDs

Next, we wanted to make a pattern using 4 LEDs. We connected 4 LEDs onto the protoboard and connected them to digital pins 2, 4, 7, and 12. Again, we must first initialize these 4 pins as outputs in the void setup(); Then, we created a pattern that goes up the row of LEDs and back down. The LED in pin 2 is on for 500ms, then LEDs 4, 7, 12, 7, 4, in that order, are on for 50ms each.



















































Here is our protoboard and Arudino with 4 LEDs connected.  Each LED has a wire from its pin on the Arduino and a resistor after it.  Then all of the black wires go to the negative column on the protoboard and a final black wire connects that back to ground on the Arduino.




Blink Without Delay

Next, we learned about the Blink Without Delay program.  With this program, we don't use delay(); like we have previously.  This means that other code can run at the same time without being interrupted by the LED.  We use millis(); and count the number of milliseconds to check if it is time to blink the LED, instead of using delay();.  This program is where we learn how to write if/else statements to control the LEDs.  This program will be important because we might want to use other things with the Arduino that won't be affected by code for the LEDs.















Modifying Blink Without Delay to create a pattern with 4 LEDs

Our final task for the day was to create pattern using 4 LEDs by modifying the Blink Without Delay program  We needed to create variables for each LED in each pin and also for each LED state.  In void setup(); we initialize those four pins as outputs.  Now we come to our loop:  here we used else if statements to tell the LEDs to create our pattern.   Else if statements specify a new condition if the first condition is false.   If LED 1 is off, then LED 1 turns on.  Then if LED 2 is off, it turns on and LED 1 turns off. This continues on so that one LED is off while the three others are on, and the one LED that is off appears to travel down the line.









Lego Racer

Our next project was to design a Lego vehicle with a single motor, powered by a PicoCricket, that can carry a 1.0 kg weight as fast as possible on a 4 meter course.  For our motors we had to use the old Lego rectangular motors that do not have internal gearing, so that we could learn how to make our own gear train.  For this project, my classmate Angel and I worked together! (Here's her blog: http://angelengr.blogspot.com/)

1. Motors and Gears
2. Our Lego Racer and Engineering Analysis
3. Reflection

Motors and Gears


Motors

We first learned about the different types of motors.  The one we had to use for our project was the 9V Lego Motor 2328c01 (which are the "old grey motors").  We compared this to the "new grey motors".  The old motor has a very high speed and low torque because there are no internal gears, and thus it is easier to stall the old one.  On the other hand the new motor is a little slower and has a bit more torque because it has internal gears, so it's harder to stall.

Torque is an important concept when working with motors and gears.   As you can see on the graph, torque and speed are inversely related.
 At one end, you get high torque, but a very slow speed.  And on the other end, you have high speed, but little torque.  So on either end, the vehicle will barely move. Thus, our goal for the project was to find the middle ground.

Also, consider the equation for power: Power = Torque * angular velocity.  We can see on the graph that this middle ground produces the maximum power.

Gears

We then learned about gears and gear trains, which is putting multiple gears together so they move and work together.  Adding gear trains increases the torque of the motor.  There are four types of Lego gears we use in this project:  40, 24, 16, and 8-tooth gears.  To start with a simple example, if we have an 8-tooth driving a 24-tooth gear, the 8-tooth will have to turn 3 times for the 24-tooth to turn 1 time.  These ratios can give us the gear ratio.  In our example, the gear ratio is 3:1 because of 3 turns:1 turn.

This brings up an important point:  Going from a small gear to a big gear lowers the speed and going from a big gear to a small gear increases the speed by a factor equal to the gear ratio.  Not only does the gear ratio affect speed, but it also affects torque.  Going from a small gear to a big gear increases the torque and from a big gear to a small gear decreases the torque.

We can also make longer gear trains, which is our goal for the project.  By starting with big gears and going to small gears, we can make the axle turn very fast.  But we have to think about torque at the same time.  Going from big to small gears will decrease the torque, and then our car wouldn't move!  This is why we need to find the right tradeoff of torque and speed.


Our Lego Car and Engineering Analysis


Angel and I started by experimenting with some different gear trains and measuring the speeds.  We made the body of the car with a simple gear train and the our iterations involved changing the gears to try to make it go as fast as possible.  Each time we changed the gear ratio, we timed the car to see how fast it went.  We went through 4 iterations. 


Gear Ratio:  2.5*3*5 = 37.5
Gear Ratio: 1.67*3*5 = 25

Gear Ratio: 1.67*3*3 = 15
 These gear trains gave us the following times:

      37.5:1     13.6 sec
         25:1     10.2 sec
         15:1     10.8 sec

We saw that as we got lower the time was going to go back up, so we wanted to make a gear train that was in between 25:1 and 15:1.



We took the 15:1 gear train and we changed the first gear to a 16-tooth.  This made the gear ratio 22.5:1.  When we timed the car it went significantly faster than our first tries, at 9.02 seconds.  We decided to keep this gear train for our final car.


Gear Ratio:  2.5*3*3 = 22.5    Time = 9.0 sec

Here is our final gear train on our finished Lego Racer.
We used two 24-tooth, one 40-tooth, one 16-tooth,
and two 8-tooth gears, for a total of six gears

A final change we made to our car was decreasing the amount of Legos we used as much as possible because wanted it to be light.  Angel and I tested it several more times and it finished the course in 9-10 seconds.  When we did a trial run with other students on Tuesday, it took 12 seconds which was longer than usual.  We think it was from testing it and the batteries were low, so we decided to change the batteries for our final race.

The final race took place on Friday.  We raced in pairs and Professor Banzaert recorded everyone's gear ratios and times.  Here is a video of our car:


Afterwards, the class looked at the results of all of our cars. The times are put in order from fastest to slowest.

          Gear Ratio  Time (sec)
              15:1            8.42
           22.5:1            8.92
           15.6:1            9.63
           20.8:1            9.68
           24.7:1          10.57
           20.8:1          10.93

So why isn't there a pattern to these times and gear ratios? We discussed several reasons in class why they is not, such as the size of the wheel, friction from gears and wheels, the number of wheels of the car, the distances of the wheels, and the weight of the car and the wheels.  Overall, it just the fact that we're doing this in real life, not as a computer simulation, and just like in all engineering, other factors come into play.





Reflection


We really enjoyed constructing our Lego racer for this project and are proud of our final car, which was the second fastest in the class!  There were a few interesting things I noticed when looking at the other team's times.  One was that the fastest car had a gear ratio of 15:1 and when we tested our car with a 15:1 ratio it was 2.5 seconds slower!  This really shows how the design of the car matters, like the wheels, the weight, and other aspects.  I think some improvements to our car could be using a larger third wheel instead of a small one.  We talked in class about how using larger wheels increases the angular velocity and this would probably make our car finish faster.  Another improvement would be trying to use the same gear train, but make the rest of the car as light as possible, by using less Legos, or lighter wheels. 

Our next project is very different than what we've been doing so far this semester.  We got out the Arduinos and will be doing some cool projects with them!