Arduino – further experiments

arduino set up with 8 LEDs

Arduino with 8 LEDs

While I had my arduino set up with eight LEDs, I thought it was the ideal opportunity to write my own function to take in a decimal number and display the binary equivalent.

This proved extremely straightforward to do, leaving me with a pleasant sense of having got the idea with output code and basic C structure.

void displayBinary(int number)
 {
   allOff();
   int delayTime=5000;
   int num=number;
   if (num>=128){
     digitalWrite(ledPins[7],HIGH);
     num=num-128;
   }
   if (num>=64){
     digitalWrite(ledPins[6],HIGH);
     num=num-64;
   }
   if (num>=32){
     digitalWrite(ledPins[5],HIGH);
     num=num-32;
   }
   if (num>=16){
     digitalWrite(ledPins[4],HIGH);
     num=num-16;
   }
   if (num>=8){
     digitalWrite(ledPins[3],HIGH);
     num=num-8;
   }
   if (num>=4){
     digitalWrite(ledPins[2],HIGH);
     num=num-4;
   }
   if (num>=2){
     digitalWrite(ledPins[1],HIGH);
     num=num-2;
   }
   if (num>=1){
     digitalWrite(ledPins[0],HIGH);
   }
   delay(delayTime);
 }

Then I just called the function in the loop section, with different integers.  Something to aim for in future is to input a number on the computer and have that number pass to the arduino, which then displays it!

all the assembled parts for testing the motor

The parts for testing the motor

Then it was time to test a different output device, so I collected the pieces for circuit 3, the motor test.  This introduced some new pieces: a transistor, a different resistor and a diode, as well as the motor itself.  I still don’t have much of an idea yet what they do, but it was still straightforward to clip the pieces together to build the circuit. In a way, though, I think the breadboard hinders learning, as although it makes it very easy to connect the pieces together, it’s not obvious what’s going on underneath, and where circuits are being completed.  I also need to learn how to read an electronic circuit diagram sometime!  It’s been years since the batteries, switch and bulb days of my childhood science.

Testing the motor

Testing the motor

Still, the motor and other parts were soon connected (about 10 minutes max), and I downloaded the program from the web and uploaded it to the arduino. There were three sample functions, one to run the motor, one to accelerate it slowly and one to decelerate it.  All seemed to run very smoothly, using the same principle as the code to dim the lights, of sending different values of i in a loop in turn to analogWrite(motorPin, i) .  Now I need to figure out what sort of things I could connect the motor to.  This is where something like lego or k’nex might come in handy, to build a housing and purpose for the circuit.  There is a 9v adapter supplied with the kit, so that it doesn’t have to run connected to the computer.

So I’ve seen some of the outputs in action so far, lights and motor.  Still to come are the servo for finer control of movement and using a shift register, which apparently will reveal how to control a group of LEDs with less than one pin each.  Then there’s sound to add, and then I’m on to the input options.  That’s when things should start getting really interesting as it will then be possible to write interactive programs.

I have now also bought a book on programming the arduino, so there will be plenty more to come on this topic.

 

Advertisements

About emmyleigh
Writer/editor/proofreader who loves technology

5 Responses to Arduino – further experiments

  1. Pingback: Displaying binary numbers in Python « ICT in Action

  2. Rob St. Amant says:

    I’m enjoying your blog posts, which I’ve just discovered. I hope you don’t mind a well-intended critique of your code:

    One of the interesting things about the representation of numbers in a computer is that different encodings can be equivalent to each other (e.g., a binary or decimal representation of a number, as your conversion function shows); another thing is that languages often let us apply functions to data that assume one encoding rather than another.

    Here’s a different way to think about turning on your LEDs. Consider the rightmost LED. If your number were encoded in binary, you would check to see if the rightmost bit was a 1 and if so turn on the LED. (You could alternatively check to see if the entire number was odd.) What about the next bit? You could figure out how to get at just that bit, or you could use a shift operator to move all the bits to the right, discarding the one you just looked at. Now you can run the identical test on the rightmost bit to see whether it’s a 1, and turn on the second LED. You continue until you’ve covered all eight LEDs. You may know all this already, but I mention it because the code to do this is pretty concise, using a for loop with an index for the LED number.

    • emmyleigh says:

      Thanks for the comment! Yes, I love the way that there are different ways to solve any problem, but I didn’t know about the way you suggest – I’ll dig around and see what I can find, thanks. Is this a way to take a binary number and encode it in lights, or a way to convert the number in the first place as well?

      • Rob St. Amant says:

        That’s the fun (but dangerous) part of programming in C and some other languages; some of the functions you use will automatically assume a specific encoding of their input data, so that you don’t need to do it explicitly. It’s been forever since I’ve programmed in C, so I’ll do this in pseudocode (pardon the formatting if it gets screwed up):

        Turn all LEDs off.
        Loop for place from 0 to 7
        If the rightmost bit of num is 1
        digitalWrite(ledPins[place],HIGH);
        Logical shift num to the right by one, dropping the rightmost bit.

        The bitwise operations can be tricky, because some may have implementation-dependent results, based on how the numbers are represented. But I think it’s the conceptual side that’s important, that you can work from right to left instead of the reverse, and this helps you think about all the tests being in a single place, with new data being brought in as needed.

  3. Rob St. Amant says:

    Oh, and sorry if my comments seem disconnected; this is the kind of thing that I think needs a continuous explanation…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: