Saturday, March 7, 2015

Arduino, Day 2


   

Our second day with our Arduino was more complex.

To begin with, we had to demo a program that functioned similarly to "Blink" but without the delay command.

Our demoed pattern:

This Arduino program featured 4 LED lights that alternated being on/off in pairs for a duration of 250 ms. The loop featured an if/else command: for a specific LED, IF it was on HIGH it would turn off, ELSE if it was on LOW it would turn on. This exercise introduced how specified periods of time can affect which subloop is run. 

Ours was a simple pattern. Other groups had more complex patterns that were fascinating to watch. We were curious to learn how they wrote their programs, as we had tried and failed to write a more interesting program ourselves. Professor Banzaert helpfully asked them to reveal what types of commands they used. We learned that commands such as && "and" and else if() were helpful tools to the other groups. We later tried to use these commands while attempting to better this Blink Without Delay program.

Sweep:
The next component of the Arduino challenge was to use Servo (a lego motor with a rotating disk included and controlled by the Sweep program. 

The baseline program:
The servo is included with the command #include <Servo.h>. We then name the Servo "myservo." We list, as an integer variable, the position, with the current position stored as a reference of 0. We attach the servo to the output terminal 9. In the loop, the first command limits the range of motion (degrees turned) by the servo's attached disk. The original was pos = 0, pos <=180; and the range was 180degrees of rotation. Here, we limited it to 90 by writing: pos = 0, pos <=90. The pos += 1; this tells the servo to increase the number of degrees turned by 1 until it gets to 90. This is all in the forward direction, clockwise. The delay affects the rate at which the servo changes. By increasing the delay to 30 from 15, we doubled the time it takes to get from 0 to 90 degrees of rotation. The next part of the loop reverses the direction, bringing the disk from 90 degrees of rotation back to 0, this time half as slow as that in the clockwise direction.

Knob:
The Knob program utilized an intermediate "knob" - a potentiometer - that controlled the rate of blinking of the LED light based on the amount of turn the knob was given. 

The Knob program:

We kept the servo active in this program. We first listed integer variables potpin = 0, val, val1, and val2. These are variables which are the results of specific functions.

The first step in the loop was to have the Arduino read the degree of rotation in the potentiometer (analotRead(potpin)) and store it as "val." As the potentiometer has a range of motion of 0 and 1023, we needed to convert the range to a smaller one, 0 and 180 by the means of scaling. The map function takes the value just stored in the 0-1023 range and converts it to a variable "val1" in the range 0-180. We then wrote another map command to link the LED to the turn of the potentiometer. We used map to convert the 0-1023 scale to a 0-2000 ms scale, and plugged in "val" to result in "val2." Now that both val1 and val2 were on the same scale, the reading from the potentiometer could regulate both the servo and the LED light at corresponding activities. The next steps with the servo takes the scaled val1 and tells the servo to turn to that position, with a delay of the standard baseline of 15 ms to get there. The LED pin in terminal 12 turns on, and delays for the scaled val2, then turns off with the scaled val2. A greater degree of turn in the potentiometer (greater val) corresponds to a greater val1 and val 2. Therefore the delay is greater. The LED light blinks slower with a greater turn in the potentiometer.

   

Looking Forward:
The challenge for our next class was to use a Lego Photocell's brightness readings to modulate the degree of turn in the servo and the frequency of the LED light. Our attempt at the program and our first test are below. 

The program worked! Our LED light blinked faster and the servo turned faster when the light was dim, and slower when the light was bright. This worked throughout a range of brightness readings consistently. There was a 2 second delay in between Photocell readings and the LED/Servo blinked/moved three times each reading as well. 

More will be said along with program analysis about this part in my next post!

2 comments:

  1. Wow! That's great that you guys are on the next part, and I like how your posts are by days so that it easier to see everything that is done over a span of time.

    ReplyDelete
  2. I think I remembered you pattern from class and it was really cool. However is there that anyway that you could check your video? I am having trouble viewing it.

    ReplyDelete