Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Saturday, January 12, 2013

Day 7: Photoresistors and Mapping Functions

There's nothing new in terms of setting up this circuit. You simply create one circuit with an LED and another circuit with a photoresistor that you use as a light sensor. The photoresistor is set up in a voltage divider in the same way as the potentiometer in Day 2. It's neat to see that you can use a single setup like a voltage divider for a range of different sensors.
Voltage Divider
Even though this is fundamentally identical to the potentiometer circuit, it feels a lot cooler. You can get the LED to increase and decrease in brightness just by waving your hand in front of it. Here's a terrible video by me of the circuit in action.

The other awesomely useful thing that I learned building this circuit was the map function. In short, this easily solves the problem that I faced in my potentiometer controlled multicolored LED side project in Day 3. In that one, the potentiometer read in an analog signal between 1-1023 and the LED pins read between 0 and 255 and so I had to invent an adjustment factor to move between the two. The map function does that for me. So in this case I could do the function

lightLevel=map(lightLevel, 0, 1023, 0, 255);

which is exactly the same map function as required in this circuit as well. Cool stuff that will definitely save me time in the future!

Wednesday, January 9, 2013

Day 5: Analog Write and Pulse Width Modulation (~)

Circuit #4 in the Sparkfun Inventor's Kit isn't anything too exciting in terms of introducing you to new stuff. It basically teaches you how to create a circuit with eight LEDs which you can program to turn on and off in various patterns. 

Like I said, there is almost nothing new introduced in this project, and yet, it brings a lot of things together. In fact, this is probably the first circuit that really gave me a view into a small fraction of the things that the Arduino is capable of. You see, even with just eight LEDs, you can make them light up one after another, light up all at once, or light up completely randomly. The program provided with the circuit provides a nice array of functions that allow you to generate some fun patterns with the LEDs. It's a great exercise, and a great opportunity to play around with programming a simple circuit like this one to create some fun results.

I decided that for my own project related to circuit 4, I would replace the digitalWrite function that is used in all of the various patterns with the analogWrite function. This means that instead of having the lights simply turn on and off in succession, the lights would pulse on and off slowly. I thought it would be simple enough. Basically, I took the for loops that had the lights turn on and off in succession, and added another for loop within it to gradually raise the brightness of the LED. See code below.

void glowOneAfterAnother()
{
  int index;
  int x;
  int delayTime = 10; // milliseconds to pause between LEDs

  for(index = 0; index <= 5; index++) // this loop goes through each LED
  {
    for(x=0; x <=255; x++) //this loop gradually raises the level of analogWrite from 0 to 255
    {
      analogWrite(ledPins[index], x);
      delay(delayTime);
    }      
  }

You may notice that I mentioned 8 LEDs before, but only 6 are mentioned in this code. This is due to a small hitch I encountered along the way.

When I first ran my program with the original 8 LEDs, I encountered the strange situation where about half of my LEDs gradually turned on as expected, and the other half simply turned on as if I had used digitalWrite. After double checking my code and double checking all my circuit connections, I was left scratching my head. After staring at my board for a while, I realized that all of the pins that were working as expected were plugged into digital pins with a tilde(~) next to the pin number. Apparently, only these pins are capable of what is called Pulse Width Modulation. While I don't know the exact details, pulse width modulation is basically the rapid flickering on and off of the pin that allows for the simulation of analog output required to use analogWrite. Apparently this is also useful for doing things like controlling the speed of motors. 

In the end, to get the circuit to work properly, I had to restrict the pins I used to the ones with a ~ beside them. Unfortunately there were only 6 so I had to remove two of my LEDs.

So that was my adventure with analogWrite. I'm glad it gave me the chance to understand the Arduino a little better. Now I can sit and bask in the glory of my 6 gently pulsing LED lights.

Friday, December 28, 2012

Day 2: Getting to know the Potentiometer

I think the last time I actually built any kind of circuit was in my freshman year physics class in college (8 years ago). Unsurprisingly I've forgotten almost everything. Today as I started circuit number 2 in my SparkFun Inventor's Kit I was faced with a component called a potentiometer (or variable resistor).


This little fella here basically has three pins and a little knob on the top that you can rotate and is described in the SparkFun guide as a knob that can raise and lower resistance, like a dimmer switch or a volume knob. The two outer pins connect to the power and the ground, and the central pin which is the output. It seems that in general, the way that these things work is that a connection is made between some sort of resistive material (apparently graphite in cheap potentiometers) organized in an arc and the power. By turning the knob on the potentiometer, you adjust the amount of resistive material between the power and the output by moving the second contact to a position further on the arc. This site explains it far more clearly than me. It's a surprisingly simple and elegant design... and I always thought that the inner workings of any of these things would be impossible to understand!

The second project in the kit involves setting up the potentiometer so that it can adjust the speed of the blinking of an LED, and getting this to work was surprisingly simple. Basically I made two circuits. One of the circuits is between the arduino and the LED, and the other is between the arduino and the potentiometer.


It's at this point that the manual discusses some crucial aspects of the arduino board: analog and digital pins. Basically, the analog pins take a value between 0 and 5 volts and translates them into a number between 0 and 1023 (more on this later). The digital pin is for dealing with values of things that are either off (0 volts) or on (5 volts) like say a blinking LED.

Now the code:

int sensorPin = 0;
int ledPin = 13;

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
int sensorValue;
sensorValue = analogRead(sensorPin);
digitalWrite(ledPin, HIGH);
delay(sensorValue);
digitalWrite(ledPin, LOW);
delay(sensorValue);
}

If you are unfamiliar with Arduino code, this might look complicated but it's actually pretty straightforward, especially if you know some programming. Here are the key lines:


sensorValue = analogRead(sensorPin);
This reads the value from the potentiometer and sets it to a variable called sensorValue

digitalWrite(ledPin, HIGH); This turns on the LED
delay(sensorValue);  This keeps the next line from running for sensorValue milliseconds 
digitalWrite(ledPin, LOW); This turns off the LED
delay(sensorValue);

That's it. Just read in the value of the potentiometer and turn the light on and off delaying by the returned value from the potentiometer. It's really surprising to me that such a simple set of code could get this done. I was expecting it to be much more complicated, but perhaps that is why arduino is so popular.