Category Archives: Arduino

Smooth Analog Input Class for Arduino

In the above photo, you see my Arduino Uno with a mini-breadboard ProtoShield.

Here, I wired it up to accept data from a potentiometer on analog input A0.  I also have a standard servo connected to digital pin 7.  The point of this is, you turn the potentiometer, it produces a corresponding motion in the servo.

Here’s the initial code that I used for making this happen:

#include <Servo.h>
#include <SmoothAnalogInput.h>

Servo servo;
int last;
void setup() {
    servo.attach(7);
    Serial.begin(9600);
    last = -1;
}

void loop() {
  int current = analogRead(A0);
  int scaled = map(current, 0, 1024, 0, 180);
  if (scaled != last) {
    last = scaled;
    Serial.println(scaled);
    servo.write(scaled);
  }
}

Pretty simple stuff.  You see I attach he Servo object to pin 7 and within the loop() section, read from A0, scale the value, then write it to the servo! It works!

One kind of annoying thing that I noticed was… the input from the potentiometer wasn’t terribly stable.  Even when it was just sitting there, it would output values that were up to plus/minus 5.  Because of this, my servo was constantly twitching.  Not only is that probably not good for the servo, you could constantly hear it too… so it was annoying.

It turns out that in one of the Arduino Tutorials, they talk about analog input smoothing.  Essentially, you take 10 readings and average them.  That’ll give you a more stable value.  The problem with using the code from the tutorial is, it’s ungainly.  It would be nice if we could create an object, attach it to A0 and have it automatically smooth incoming values for us… well, that’s what I wrote!

You can grab the actual code from here: https://github.com/rl337/Arduino/blob/master/libraries/SmoothAnalogInput

The prototype for the object looks like this:

#define SMOOTH_ANALOG_INPUT_SIZE 32

class SmoothAnalogInput {
    public:
        SmoothAnalogInput();
        void attach(int pin);
        void scale(int min, int max);
        int read();
        int raw();

};

The object smooths data over 32 samples. You attach() the object to whatever pin you want it to smooth input for, in our case, A0. the raw() method takes a reading and returns the raw value from the analog input. the read() method returns the smoothed result which can be automatically scaled between a min/max value if you call the scale() method. Here’s the program reworked to use the SmoothAnalogInput object

#include 
#include 

Servo servo;
SmoothAnalogInput ai;
int last;
void setup() {
  Serial.begin(9600);
  servo.attach(7);
  ai.attach(A0);
  last = -1;
}

void loop() {
  int sensorReading = ai.read();

  int scaled = map(sensorReading, 0, 1024, 0, 180);
  if (scaled != last) {
    last = scaled;
    Serial.println(scaled);
    servo.write(scaled);
  }
}

Looking at the Serial Monitor, there are still a few cases where the servo fidgets, but by and large, the values become stable quickly after you stop moving the potentiometer.

An Arduino general purpose detector!

I’ve been spending some time getting acquainted with my new Arduino Uno.

I did the first Blinking LED tutorial which went exactly as expected.  This particular board has a built-in LED connected to pin 13, so I didn’t even need to do any wiring!

Having forced the on-board LED to blink, I decided to get a little more ambitious.  Along with my Arduino, I bought a bag of LEDs which came with the appropriate resistors for building simple circuits with the board I have.  I attached a 2nd LED to a small Bread Board and wired it up to pin 12.  After modifying the program a little bit, I got the two LEDs to alternate blinking.

Not wanting to stop at using only 2 LEDs, I strung up 5 more to the Bread Board and connected each to a corresponding pin from 7 to 11.  With everything wired up, I coded up a simple function that would take a number from 0-6 and light up an appropriate number of LEDs.  0 leds if 0 were passed and 6 if six was passed.  You can see that segment of the code here:

void light(int i) {

  if(i < 0) { i = 0; }
  if (i > 6) {  i = 6; }

  for(int j = 0; j < 6; j++) {
     int reg = 12 - j;
     if (j < i) {
        digitalWrite(reg, HIGH);
     } else {
       digitalWrite(reg, LOW);
     }
  }  
}

The first time running it, I simply cycled through numbers from 0 to 7 which resulted in the LEDs lighting up one at a time then resetting.

Finally, I decided to mess around with the Arduino’s Analog Input capabilities.  The input seems quite sensitive because as I touched the wire that I connected to the input, it immediately began registering a voltage.   In my program’s main loop, I scaled the analog input to the 0-7 range of my LED array.  Now when I touch the wire, the more fingers I touch the wire with… the more LEDs light up!  Here’s the pic!

Here, I was holding the wire with three fingers; my thumb, pointer  and middle finger… four of the LEDs lit up.  Presumably, if I connect up any random sensor (light, sound, etc), the intensity will light up a corresponding set of LEDs.  That aught to be more useful than detecting fingers touching a wire.

Spherical Robots and Arduino Uno!

Back in November, I was at the Academy of Science for Nightlife and found nestled in the creepy taxidermy (African Hall), I ran into Spherical Robots rolling around. Check out the video:

This particular set of robots were originally created for Burning Man, but it’s no surprise to me that they would tour around the more sciency venues of San Francisco.  I’d never really considered a spherical robot before… but I guess it’s a “thing”.  Go ahead! Google it!

Now to me, the easiest way to get a sphere moving would be to have a rod running through an axis of the ball and you roll around it using either weight or a gyroscope on whatever internal mechanism you’ve got driving.  In this design you effectively have one HUGE wheel. These robots, however did not seem to do that.  They were able to roll in arbitrary directions.  They didn’t have to turn to change direction.

My initial gut feeling was, it used a pair of gyroscopes offset from each other by some angle.  When the orientation of the two gyroscopes change, the sphere’s effective center of gravity would shift causing it to move.  After doing some reading, though, it seems like that is not the case. A more likely scenario is… you have a large pendulum style structure which is moved around with a pair of solenoids.  This apparatus is dangled from a stable platform.  When the pendulum is pushed in a direction by the solenoids, the center of gravity shifts and the ball rolls.  The amount that the pendulum can move the ball is proportional to the weight at the end of the pendulum.

All of this conjecture has lead me to conclude that I need to try it out.  So… I’m off to learn a little bit of robotics.  So… how does one get started in robotics?  One dives right in!

I’ve been wanting to work with the Arduino family of microcontrollers, and this seems like an ideal project.  I ordered myself an Arduino Uno, a couple of add-on boards and am ready to rock!

My first goal will be to familiarize myself with the Arduino programming language and generally make myself comfortable with the tools involved.

There is some soldering involved.  You can see in the above image, my new toys.  The blue circuit board is the actual Arduino itself.  Below the magnifier is an add-on board that will allow me to control two motors.  (some assembly required).   I have a 2nd add-on board coming which will have add a small LCD display. I’m sure that board will be the one I play with the most (At least at first).

Naturally, I had to try to put together the motor controls, but it seems like fate would have other plans… Look at what happened to my soldering pencil:

Yeah. So… a new one is on the way. I guess soldering will have to wait a few days.