Sunday, March 8, 2015

Ardunio Day 2

On our second day of programming with the Arduino Uno, we started off by attaching a Servos motor to the Arduino board. The example program used to control the motor was called Sweep. The circular plate on the motor rotates from 0 to 180 degrees at different speeds, depending on the program. 

The program starts off by indicating that the servo is being used, and that it is named "my servo". Its initial position is at 0 degrees, and it is connected to pin 9. 

Then, a loop is created so that the motor rotates from 0 to 180 degrees, and then back. The speed of the rotation can be controlled by specifying by how large of a step should be taken during the rotation. For example, with the code pos +=2, it will rotate at twice the speed with pos +=1.

The delay(15) is needed to give the motor enough time to reach the desired position.




Next, we attached a potentiometer and an LED light to the Arduino board. The LED was defined as ledPin as an output at pin 7, with an initial setting of LOW -- or off. The potentiometer was attached at A0. 

The goal was to have the blink rate of the LED to depend on the position of the potentiometer. In the loop, the integer val was defined as the analog reading of the potentiometer, whose values go from 0 to 1023. The range of values was scales to 0 to 180. 

The light was turned on and off by digitalWrite. We set the delay of the LED to be val, so that as the potentiometer was turned from 0 to 180, the light would blink more quickly. 


In the Knob program, the servos did not do anything -- we only kept it there because it had been used in the previous program.

The next project involves using Arduino with a photocell. Look forward to posting about that!

No comments:

Post a Comment