Monday, March 9, 2015

Arduino Day 1 & 2

It sounds like I talk about this at the beginning of each blog, but I must say that this is another super exciting projects for me! I'm really enjoying this class and wish that I had access to these opportunities earlier in my life.

Day 1


Anyhow, we are now learning Arduino, which would help us understand the control and response process. It also exposes us to computer programming.

Our very first task was to understand the program that makes the LED light blink. We connected out Arduino Uno to the computer, and the LED light on the board that is connected to port 13 started blinking on an 1-second interval. To further our understanding, we deleted the delay in the code, later finding out that to human eyes, the LED would look like it remains on the whole time, even though it was in fact blinking at an extremely high speed that is undetectable.We also altered delay into a few different values, to see how the timing changes.
Code for Blink
Here, int led = 13 refers to the port that the information is uploaded to. The HIGH and LOW represents the voltage at the port, with HIGH representing on and LOW representing off. The void loop function allows the program to run over and over again. The Arduino program runs in milli-seconds. Thus a delay of 1000 milli-seconds is in fact only 1 second.

As we understood the use of delay and the general idea of setting up Arduino programs, my partner, Meredith, and I started working on a program that controls four LED's with delay. Here is our COOL pattern. The second part looks like a little bug made of two lights is running through the row of LED's!
While the pattern looks cool, the program is actually nothing more than an elaboration of the blink program with changes in delays and ports for outputs. We used port 12, 8, 7, and 4. Here is the code for the second part of the pattern, i.e. the part where only two lights are on at the same time.

  void setup () {
    pinMode(12, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(4, OUTPUT);
  }

  void loop () {
    digitalWrite(12, HIGH);
    delay(500);
    digitalWrite(8, HIGH);
    delay(500);
    digitalWrite(7, HIGH);
    digitalWrite(12, LOW);
    delay(500);
    digitalWrite(4, HIGH);
    digitalWrite(8, LOW);
    delay(500);
    digitalWrite(7, LOW);
    delay(500);
    digitalWrite(4, LOW);
    delay(500);
  }

After understanding the delay function, we then moved onto a different way to achieve blinking without delay but with interval. It is necessary to do so, because sometimes the program must command other parts connected to Arduino, but having delay in the code will delay/affect other commands made to other parts.

The interval function is much harder to understand, since it incorporates variables that the computer would compute, as opposed to constants that we just plug in like in delay. It basically allows the program to check the status of the lights after each interval, and update the status of parts connected and other variables based on the code.

Blink without Delay


We wanted to do a similar pattern we did before, which required different patterns. However, we could not find a way to make the pattern go back to the loop. We tried
      if(currentMillis > 3000)
        currentMillis = 0
and
      if(currentMillis = 3000)
        currentMillis = 0
However, it turned out that the first one makes each milli-second after 3000 become the 0th milli-second, and the latter one makes only the 3000th milli-second become the 0th milli-second. Neither code resets the timer. We may be using the wrong code to say what we want.

We ended up with only one pattern, alternating the lights on and off.
We would like to go back and revisit our code to see if there is a better way to return the loop back to the beginning and have multiple patterns loop.

Day 2

As I said before, the main point of studying Arduino is to understand response and control. In the previous class we were able to control the LED's, but we did not get to the response stage. In our second day with Arduino, we worked with the servo motor, the potentiometer and the photocell.

We first started with the servo motor. This is much more precise than PicoCricket, which we used for our Lego racer. We can control the servo by commanding it to move to certain positions. We may make it stop or move continuously. The example "Sweep" familiarizes us with controlling the motor. We changed the position and the rate at which the motor moves to understand the language better.
Modified "Sweep"
Note that the program does not automatically run servo, so we must specify at the beginning. We also learnt the "for" statement here. Another thing I noticed was that I wanted to see if the servo would turn clock-wise for 90 degrees and counter clock-wise for 180 degrees and repeat the process. It turned out that if I simply change the number, the servo would only turn quickly to the position written as the start.

The servo is in fact used to demonstrate as an output. We then started attaching the potentiometer on the proto-board and the potentiometer would be our first instrument to receive signals and ask the servo to respond. The first example was "Knob". The program uses the "map()" function to correspond the potentiometer's setting with the position of the servo. We then combined this with the flashing LED, which also became part of the response.

In general, when the potentiometer has a lower value, the interval where the LED blink becomes smaller and thus the LED blinks faster. The coding for this program is quite straight-forward, and combines with other languages we learned in previous examples.

The second input control device we used was the Photocell. It detects the brightness of light and the Arduino program and read it and also produce a result as commanded by the program. Here, to detect the data, we used the following code.
Note that in this case, the photocell would check the brightness every second.

To use the reading to control the servo and the LED, we used similar functions as we did in "Blink" and "Knob". Here is the code.
When we were running the initial program to look at the reading, we noticed that the highest value we got by shining a phone flashlight to the photocell was in the 900's. We thus set the value of the photocell as 0-1000. We also used a "for()" statement here to let the LED light blinks three times before the next time the photocell detects again. The LED blinks faster when the light is brighter and slower when the light it dimmer.

2 comments:

  1. I couldn't view your first video, where you discuss the alternating on and off pattern. I would really love to see the pattern that you started with.

    ReplyDelete
    Replies
    1. I just checked it and it was working! Maybe reload? Sorry for the inconvenience and thanks for your comment. :)

      Delete