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.