Playing with analog meters update: video with some code

After getting the analog meter up and running last time I took the standard Arduino “Fade” example and modified it to make sense with a meter instead of an LED.

I modified the “fade” sketch with more variables (for easy alteration) and playing around with quite a few settings. I added variables for the initial needle position on the gauge, how far to swing the needle and how fast to swing the needle. I also added some minor logic to derive maximum/minimum swing values for ease of alteration.

I found that swinging the needle about 10 points (out of 255) to either side of the initial value (so a “sweep” of 20, or just under 10% the total available distance) at a speed of 15 gave a pretty good “jitter” effect:


The code I made is below if anyone is vaguely interested or would like to laugh at my programming skills.

 Basic Analog Gauge Needle Fluctuate
 Based on Arduino sketch "Fade"

int agauge = 3;        // the pin that the Analog Gauge attached
int needlepos = 145;     // initial needle position
int needleswing = 10;   // how far the needle should swing either side of needlepos
int needlespeed = 15;   // how fast the needle moves

// Derived values, no need to modify
int needlelowerlimit = needlepos - needleswing;   // minimum needle position
int needleupperlimit = needlepos + needleswing;   // max needle position

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin to be an output:
  pinMode(agauge, OUTPUT);

// the loop routine runs over and over again forever:
void loop()  { 
  // set the position of needle:
  analogWrite(agauge, needlepos);    

  // change the needle position:
  needlepos = needlepos + needlespeed;

  // reverse the direction of the needle's movement at the ends: 
  if (needlepos < needlelowerlimit || needlepos > needleupperlimit) {
    needlespeed = -needlespeed ; 
  // wait for 30 milliseconds to see the dimming effect